using Dalamud.Interface;

namespace HellionChat.Ui;

// Default icon mapping for tabs, used in top-tabs (icon prefix) and sidebar (icon-only with tooltip).
// Users can override per tab via Settings -> Tabs -> Tab.Icon.
// Pure string resolver logic lives in TabIconGlyphResolver (no Dalamud dependency) for testability.
internal static class TabIconMapping
{
    // Glyph name -> FontAwesomeIcon lookup for production resolve.
    // Every key must also exist in TabIconGlyphResolver.PickerOptions.
    // A missing key silently falls back to FontAwesomeIcon.Hashtag (degraded, no crash).
    private static readonly Dictionary<string, FontAwesomeIcon> GlyphLookup = new(
        StringComparer.OrdinalIgnoreCase
    )
    {
        ["comment"] = FontAwesomeIcon.Comment,
        ["comments"] = FontAwesomeIcon.Comments,
        ["cog"] = FontAwesomeIcon.Cog,
        ["users"] = FontAwesomeIcon.Users,
        ["user-friends"] = FontAwesomeIcon.UserFriends,
        ["link"] = FontAwesomeIcon.Link,
        ["envelope"] = FontAwesomeIcon.Envelope,
        ["clock"] = FontAwesomeIcon.Clock,
        ["hashtag"] = FontAwesomeIcon.Hashtag,
        ["star"] = FontAwesomeIcon.Star,
        ["heart"] = FontAwesomeIcon.Heart,
        ["bell"] = FontAwesomeIcon.Bell,
        ["bookmark"] = FontAwesomeIcon.Bookmark,
        ["flag"] = FontAwesomeIcon.Flag,
        ["fire"] = FontAwesomeIcon.Fire,
    };

    // Resolves the icon for a tab. Auto-tell tabs get a per-partner hashed icon
    // from the tell pool so parallel tells differ by glyph shape, not just colour.
    public static FontAwesomeIcon Resolve(Tab tab)
    {
        string? autoTellGlyph = null;
        if (tab.IsTempTab && tab.TellTarget != null && tab.TellTarget.IsSet())
            autoTellGlyph = TabTintCache.GetIcon(tab);

        var glyph = TabIconGlyphResolver.ResolveGlyphName(tab, autoTellGlyph);
        return GlyphLookup.TryGetValue(glyph, out var icon) ? icon : FontAwesomeIcon.Hashtag;
    }
}
