using System.Collections.Frozen;

namespace GmRelay.Shared.Domain;

public enum GameSystem
{
    Dnd5e,
    Pathfinder2e,
    CallOfCthulhu7e,
    Shadowdark,
    OldSchoolEssentials,
    Dragonbane,
    BladesInTheDark,
    Daggerheart,
    CyberpunkRed,
    Mothership,
    AlienRpg,
    WarhammerFantasy,
    VampireMasquerade5e,
    StarWarsFfg,
    Genesys,
    SavageWorlds,
    GURPS,
    Fate,
    DungeonWorld,
    Ironsworn,
    Other
}

public static class GameSystemExtensions
{
    private static readonly FrozenDictionary<GameSystem, string> DisplayNames =
        new Dictionary<GameSystem, string>
        {
            [GameSystem.Dnd5e] = "D&D 5e",
            [GameSystem.Pathfinder2e] = "Pathfinder 2e",
            [GameSystem.CallOfCthulhu7e] = "Call of Cthulhu 7e",
            [GameSystem.Shadowdark] = "Shadowdark",
            [GameSystem.OldSchoolEssentials] = "Old School Essentials",
            [GameSystem.Dragonbane] = "Dragonbane",
            [GameSystem.BladesInTheDark] = "Blades in the Dark",
            [GameSystem.Daggerheart] = "Daggerheart",
            [GameSystem.CyberpunkRed] = "Cyberpunk RED",
            [GameSystem.Mothership] = "Mothership",
            [GameSystem.AlienRpg] = "Alien RPG",
            [GameSystem.WarhammerFantasy] = "Warhammer Fantasy",
            [GameSystem.VampireMasquerade5e] = "Vampire: The Masquerade 5e",
            [GameSystem.StarWarsFfg] = "Star Wars (FFG)",
            [GameSystem.Genesys] = "Genesys",
            [GameSystem.SavageWorlds] = "Savage Worlds",
            [GameSystem.GURPS] = "GURPS",
            [GameSystem.Fate] = "Fate",
            [GameSystem.DungeonWorld] = "Dungeon World",
            [GameSystem.Ironsworn] = "Ironsworn",
            [GameSystem.Other] = "Другое"
        }.ToFrozenDictionary();

    public static string ToDisplayName(this GameSystem system) =>
        DisplayNames.TryGetValue(system, out var name) ? name : "Другое";

    public static GameSystem? TryParseFuzzy(string input)
    {
        if (string.IsNullOrWhiteSpace(input))
            return null;

        var normalized = input.Trim().ToLowerInvariant();

        if (Enum.TryParse<GameSystem>(normalized, true, out var exact))
            return exact;

        foreach (var value in Enum.GetValues<GameSystem>())
        {
            if (value == GameSystem.Other)
                continue;

            var display = value.ToDisplayName().ToLowerInvariant();
            if (display == normalized || display.Contains(normalized) || normalized.Contains(display))
                return value;
        }

        return GameSystem.Other;
    }
}
