using System;
using System.Collections.Generic;

namespace GravCoin;

public static class UiIcon
{
    private static readonly Dictionary<string, string> GlyphMap = new( StringComparer.OrdinalIgnoreCase )
    {
        ["mdi:home-outline"] = "\uD83C\uDFE0",
        ["mdi:home"] = "\uD83C\uDFE0",
        ["mdi:home-variant"] = "\uD83C\uDFD8\uFE0F",
        ["mdi:chart-line"] = "\uD83D\uDCC8",
        ["mdi:trending-up"] = "\uD83D\uDCB9",
        ["mdi:finance"] = "\u20BF",
        ["mdi:candlestick-chart"] = "\uD83D\uDCC8",
        ["mdi:chart-line-variant"] = "\uD83D\uDCC9",
        ["mdi:chart-bar"] = "\uD83D\uDCCA",
        ["mdi:chart-box-outline"] = "\uD83D\uDCCA",
        ["mdi:chart-bell-curve"] = "\uD83D\uDCC8",
        ["mdi:robot-outline"] = "\uD83E\uDD16",
        ["mdi:robot"] = "\uD83E\uDD16",
        ["mdi:robot-industrial"] = "\uD83E\uDDBE",
        ["mdi:robot-automation"] = "\uD83E\uDD16",
        ["mdi:cash-multiple"] = "\uD83D\uDCB5",
        ["mdi:cash-fast"] = "\uD83D\uDCB8",
        ["mdi:cash-remove"] = "\uD83D\uDCB8",
        ["mdi:desktop-classic"] = "\uD83D\uDDA5\uFE0F",
        ["mdi:server-network"] = "\uD83D\uDDA7",
        ["mdi:server"] = "\uD83D\uDDA5",
        ["mdi:database"] = "\uD83D\uDDD2",
        ["mdi:gpu"] = "\uD83E\uDDE0",
        ["mdi:raspberry-pi"] = "\uD83C\uDF53",
        ["mdi:dns"] = "\uD83D\uDDC2",
        ["mdi:memory"] = "\uD83D\uDD0C",
        ["mdi:flash"] = "\u26A1\uFE0F",
        ["mdi:star-outline"] = "\u2B50\uFE0F",
        ["mdi:star"] = "\u2B50\uFE0F",
        ["mdi:star-four-points"] = "\u2728",
        ["mdi:trophy-outline"] = "\uD83C\uDFC6",
        ["mdi:arrow-up-bold-hexagon-outline"] = "\u2B06\uFE0F",
        ["mdi:view-dashboard"] = "\uD83D\uDCCA",
        ["mdi:chart-line"] = "\uD83D\uDCC8",
        ["mdi:robot-outline"] = "\uD83E\uDD16",
        ["mdi:cash-fast"] = "\uD83D\uDCB8",
        ["mdi:server-network"] = "\uD83D\uDDA7",
        ["mdi:star-outline"] = "\u2B50\uFE0F",
        ["mdi:account-outline"] = "\uD83D\uDC64",
        ["mdi:cog-outline"] = "\u2699\uFE0F",
        ["mdi:account-outline"] = "\uD83D\uDC64",
        ["mdi:account-group-outline"] = "\uD83D\uDC65",
        ["mdi:cog-outline"] = "\u2699\uFE0F",
        ["mdi:construction"] = "\uD83D\uDEA7",
        ["mdi:alert-octagon-outline"] = "\u26D4\uFE0F",
        ["mdi:alert-octagon"] = "\u26D4\uFE0F",
        ["mdi:alert-outline"] = "\u26A0\uFE0F",
        ["mdi:gift-outline"] = "\uD83C\uDF81",
        ["mdi:gift-open-outline"] = "\uD83E\uDDE7",
        ["mdi:check-circle"] = "\u2705",
        ["mdi:circle-outline"] = "\u26AA\uFE0F",
        ["mdi:circle"] = "\uD83D\uDD35",
        ["mdi:clock-outline"] = "\uD83D\uDD52",
        ["mdi:pickaxe"] = "\u26CF\uFE0F",
        ["mdi:diamond-stone"] = "\uD83D\uDC8E",
        ["mdi:slot-machine-outline"] = "\uD83C\uDFB0",
        ["mdi:triangle-small-up"] = "\u25B2",
        ["mdi:triangle-small-down"] = "\u25BC",
        ["mdi:close"] = "\u274C",
        ["mdi:send"] = "\u27A4",
        ["mdi:chat-outline"] = "\uD83D\uDCAC",
        ["mdi:earth"] = "\uD83C\uDF0D",
        ["mdi:rocket-launch"] = "\uD83D\uDE80",
        ["mdi:target"] = "\uD83C\uDFAF",
        ["mdi:rocket"] = "\uD83D\uDE80",
        ["mdi:arrow-up-bold"] = "\u2B06\uFE0F",
        ["mdi:arrow-up-bold-circle-outline"] = "\uD83D\uDD3C",
        ["mdi:cards-playing-spade-outline"] = "\uD83C\uDCCF",
        ["mdi:satellite-variant"] = "\uD83D\uDEF0",
        ["mdi:treasure-chest-outline"] = "\uD83E\uDDF0",
        ["mdi:treasure-chest-premium"] = "\uD83C\uDF81",
        ["mdi:bank-outline"] = "\uD83C\uDFE6",
        ["mdi:bank"] = "\uD83C\uDFE6",
        ["mdi:bitcoin"] = "\u20BF",
        ["mdi:file-document-outline"] = "\uD83D\uDCC4",
        ["mdi:office-building-outline"] = "\uD83C\uDFE2",
        ["mdi:office-building"] = "\uD83C\uDFE2",
        ["mdi:tree-outline"] = "\uD83C\uDF32",
        ["mdi:heart-outline"] = "\uD83E\uDD0D",
        ["mdi:information-outline"] = "\u2139",
        ["mdi:restore"] = "\uD83D\uDD04",
        ["mdi:star-circle-outline"] = "\u2B50\uFE0F",
        ["mdi:lightbulb-outline"] = "\uD83D\uDCA1",
        ["mdi:link-variant"] = "\uD83D\uDD17",
        ["mdi:key-variant"] = "\uD83D\uDD11",
        ["mdi:content-save-outline"] = "\uD83D\uDCBE",
        ["mdi:loading"] = "\u23F3",
        ["mdi:gas-station-outline"] = "\u26FD\uFE0F",
        ["mdi:spy"] = "\uD83D\uDD75\uFE0F",
        ["mdi:hand-wave-outline"] = "\uD83D\uDC4B",
        ["mdi:lock-outline"] = "\uD83D\uDD12",
        ["mdi:web"] = "\uD83C\uDF10",
        ["mdi:book-open-page-variant"] = "\uD83D\uDCD6",
        ["mdi:brain"] = "\uD83E\uDDE0",
        ["mdi:briefcase-outline"] = "\uD83D\uDCBC",
        ["mdi:clipboard-text-outline"] = "\uD83D\uDCCB",
        ["mdi:crown"] = "\uD83D\uDC51",
        ["mdi:crown-outline"] = "\uD83D\uDC51",
        ["mdi:explosion"] = "\uD83D\uDCA5",
        ["mdi:eye-outline"] = "\uD83D\uDC41",
        ["mdi:fire"] = "\uD83D\uDD25",
        ["mdi:infinity"] = "\u267E\uFE0F",
        ["mdi:magnify"] = "\uD83D\uDD0D",
        ["mdi:medal-outline"] = "\uD83C\uDFC5",
        ["mdi:rhombus-outline"] = "\uD83D\uDD37",
        ["mdi:shield-check-outline"] = "\uD83D\uDEE1",
        ["mdi:shoe-print"] = "\uD83D\uDC63",
        ["mdi:sword-cross"] = "\u2694\uFE0F",
        ["mdi:source-fork"] = "\uD83D\uDD00",
        ["mdi:gavel"] = "\uD83D\uDD28",
        ["mdi:tie"] = "\uD83D\uDC54",
        ["mdi:tools"] = "\uD83D\uDEE0\uFE0F",
        ["mdi:ufo-outline"] = "\uD83D\uDEF8",
        ["mdi:wolf"] = "\uD83D\uDC3A",
        ["mdi:atom"] = "\u269B\uFE0F",
        ["mdi:numeric-7"] = "7",
        ["mdi:leaf"] = "\uD83C\uDF43",
        ["mdi:bell-outline"] = "\uD83D\uDD14",
        ["mdi:fruit-cherries"] = "\uD83C\uDF52",
        ["mdi:minus"] = "\u2796",
        ["mdi:triangle-outline"] = "\uD83D\uDD3A",
        ["mdi:square-outline"] = "\uD83D\uDFE6",
        ["mdi:help-circle-outline"] = "\u2754",
        ["mdi:chip"] = "\uD83D\uDCDF",
        ["mdi:cpu"] = "\uD83D\uDCDF",
        ["mdi:network"] = "\uD83C\uDF10",
        ["mdi:hammer-screwdriver"] = "\uD83D\uDEE0\uFE0F",
        ["mdi:shield-key-outline"] = "\uD83D\uDD10",
        ["mdi:shopping-outline"] = "\uD83D\uDECD\uFE0F",
        ["mdi:timer-sand"] = "\u23F3",
        ["mdi:update"] = "\uD83D\uDD04",
        ["mdi:earth-box"] = "\uD83C\uDF0D",
        ["mdi:account-star"] = "\uD83C\uDF1F",
        ["mdi:api"] = "\uD83D\uDD17",
        ["mdi:connection"] = "\uD83D\uDDA7",
        ["mdi:lan"] = "\uD83D\uDD17",
        ["mdi:auto-fix"] = "\u2728",
        ["mdi:chip-variant"] = "\uD83D\uDDA5",
        ["mdi:desktop-tower"] = "\uD83D\uDDA5\uFE0F",
        ["mdi:speedometer"] = "\uD83C\uDFCE\uFE0F",
        ["mdi:view-dashboard"] = "\uD83D\uDCCA",
        ["mdi:monitor-dashboard"] = "\uD83D\uDCCA",
        ["mdi:chart-line"] = "\uD83D\uDCC8",
        ["mdi:chart-areaspline"] = "\uD83D\uDCC8",
        ["mdi:finance"] = "\uD83D\uDCC8",
        ["mdi:cash-sync"] = "\uD83D\uDCB8",
        ["mdi:hammer-wrench"] = "\uD83D\uDEE0\uFE0F",
        ["mdi:medal"] = "\uD83C\uDFC5",
        ["mdi:view-quilt"] = "\uD83D\uDDA5\uFE0F",
        ["mdi:lan-connect"] = "\uD83D\uDD17",
        ["mdi:database-search"] = "\uD83D\uDD0D",
        ["mdi:factory"] = "\uD83C\uDFED",
        ["mdi:cpu-variant"] = "\uD83D\uDDA5",
        ["mdi:account-badge-outline"] = "\uD83E\uDEAA",
        ["mdi:account"] = "\uD83D\uDC64",
        ["mdi:account-outline"] = "\uD83D\uDC64",
        ["mdi:account-circle"] = "\uD83D\uDC64",
        ["mdi:account-tie"] = "\uD83D\uDC64",
        ["mdi:account-tie-voice"] = "\uD83E\uDD35",
        ["mdi:account-cog"] = "\uD83D\uDC64\u2699\uFE0F",
        ["mdi:account-lock"] = "\uD83D\uDD12",
        ["mdi:alert-box"] = "\u26A0\uFE0F",
        ["mdi:alert-circle"] = "\u2757",
        ["mdi:arrow-down-bold"] = "\u2B07\uFE0F",
        ["mdi:arrow-left-bold"] = "\u2B05\uFE0F",
        ["mdi:arrow-right-bold"] = "\u27A1\uFE0F",
        ["mdi:backspace"] = "\u232B",
        ["mdi:bell-ring"] = "\uD83D\uDD14",
        ["mdi:bookmark"] = "\uD83D\uDD16",
        ["mdi:bullhorn"] = "\uD83D\uDCE2",
        ["mdi:calculator"] = "\uD83D\uDDA9",
        ["mdi:calendar"] = "\uD83D\uDCC5",
        ["mdi:camera"] = "\uD83D\uDCF7",
        ["mdi:cart"] = "\uD83D\uDED2",
        ["mdi:cellphone-link"] = "\uD83D\uDCBB",
        ["mdi:chart-areaspline"] = "\uD83D\uDCC8",
        ["mdi:chart-bubble"] = "\uD83D\uDCC8",
        ["mdi:chart-donut"] = "\uD83D\uDCCA",
        ["mdi:chart-multiline"] = "\uD83D\uDCC8",
        ["mdi:chart-pie"] = "\uD83D\uDCCA",
        ["mdi:chart-scatterplot-hexbin"] = "\uD83D\uDCCA",
        ["mdi:check-bold"] = "\u2705",
        ["mdi:check-decagram"] = "\u2705",
        ["mdi:chevron-left"] = "\u276E",
        ["mdi:chevron-right"] = "\u276F",
        ["mdi:chevron-up"] = "\u2303",
        ["mdi:chevron-down"] = "\u2304",
        ["mdi:city"] = "\uD83C\uDFD9",
        ["mdi:clipboard-check"] = "\uD83D\uDCC5",
        ["mdi:cloud-download"] = "\u2601\uFE0F\u2B07\uFE0F",
        ["mdi:cloud-upload"] = "\u2601\uFE0F\u2B06\uFE0F",
        ["mdi:comment-text"] = "\uD83D\uDCAC",
        ["mdi:compass"] = "\uD83E\uDDE4",
        ["mdi:content-copy"] = "\u2312",
        ["mdi:content-cut"] = "\u2702\uFE0F",
        ["mdi:content-paste"] = "\uD83D\uDCCB",
        ["mdi:controller-classic"] = "\uD83C\uDFAE",
        ["mdi:credit-card"] = "\uD83D\uDCB3",
        ["mdi:delete-forever"] = "\uD83D\uDDD1\uFE0F",
        ["mdi:discord"] = "\uD83D\uDCAC",
        ["mdi:dots-horizontal"] = "\u2022\u2022\u2022",
        ["mdi:dots-vertical"] = "\u22EE",
        ["mdi:download"] = "\u2B07\uFE0F",
        ["mdi:email"] = "\u2709\uFE0F",
        ["mdi:exit-run"] = "\uD83D\uDEAA",
        ["mdi:eye-off"] = "\uD83D\uDE48",
        ["mdi:file-find"] = "\uD83D\uDD0D",
        ["mdi:folder-open"] = "\uD83D\uDCC2",
        ["mdi:folder"] = "\uD83D\uDCC1",
        ["mdi:format-list-bulleted"] = "\uD83D\uDCC4",
        ["mdi:gauge"] = "\u23F2",
        ["mdi:glass-stange"] = "\uD83E\uDD14",
        ["mdi:hand-pointing-right"] = "\uD83D\uDC49",
        ["mdi:help"] = "\u2753",
        ["mdi:history"] = "\u23F3",
        ["mdi:incognito"] = "\uD83D\uDD75\uFE0F",
        ["mdi:information"] = "\u2139\uFE0F",
        ["mdi:key"] = "\uD83D\uDD11",
        ["mdi:lightbulb"] = "\uD83D\uDCA1",
        ["mdi:logout"] = "\uD83D\uDEAA",
        ["mdi:map-marker"] = "\uD83D\uDCCD",
        ["mdi:menu"] = "\u2630\uFE0F",
        ["mdi:message"] = "\uD83D\uDCAC",
        ["mdi:microphone"] = "\uD83C\uDFA4",
        ["mdi:minus-circle"] = "\u26D4\uFE0F",
        ["mdi:monitor"] = "\uD83D\uDDA5\uFE0F",
        ["mdi:music"] = "\uD83C\uDFA7",
        ["mdi:navigation"] = "\u27A4",
        ["mdi:numeric"] = "0-9",
        ["mdi:package-variant"] = "\uD83D\uDCE6",
        ["mdi:pause"] = "\u23F8",
        ["mdi:phone"] = "\uD83D\uDCDE",
        ["mdi:play"] = "\u25B6\uFE0F",
        ["mdi:plus-circle"] = "\u2795",
        ["mdi:power"] = "\u23FB",
        ["mdi:printer"] = "\uD83D\uDDA8\uFE0F",
        ["mdi:radio"] = "\uD83D\uDCFB",
        ["mdi:record"] = "\u23FA",
        ["mdi:redo"] = "\u21AA\uFE0F",
        ["mdi:shield"] = "\uD83D\uDEE1",
        ["mdi:shopping"] = "\uD83D\uDECD",
        ["mdi:shuffle"] = "\uD83D\uDD00",
        ["mdi:skip-next"] = "\u23ED",
        ["mdi:skip-previous"] = "\u23EE",
        ["mdi:skull"] = "\uD83D\uDC80",
        ["mdi:speaker"] = "\uD83D\uDD0A",
        ["mdi:stop"] = "\u23F9",
        ["mdi:sync-alert"] = "\u26A0\uFE0F",
        ["mdi:sync"] = "\uD83D\uDD04",
        ["mdi:table-settings"] = "\uD83D\uDDD2",
        ["mdi:tag"] = "\uD83C\uDFF7\uFE0F",
        ["mdi:thumb-down"] = "\uD83D\uDC4E",
        ["mdi:thumb-up"] = "\uD83D\uDC4D",
        ["mdi:timer"] = "\u23F2",
        ["mdi:treasure-chest"] = "\uD83E\uDDF0",
        ["mdi:trending-down"] = "\uD83D\uDCC9",
        ["mdi:trophy"] = "\uD83C\uDFC6",
        ["mdi:tune"] = "\u23F2",
        ["mdi:undo"] = "\u21A9\uFE0F",
        ["mdi:unlocked"] = "\uD83D\uDD13",
        ["mdi:video-vintage"] = "\uD83D\uDCF9",
        ["mdi:video"] = "\uD83D\uDCF9",
        ["mdi:volume-low"] = "\uD83D\uDD09",
        ["mdi:volume-medium"] = "\uD83D\uDD09",
        ["mdi:volume-off"] = "\uD83D\uDD07",
        ["mdi:wallet-giftcard"] = "\uD83C\uDF81",
        ["mdi:wallet"] = "\uD83D\uDCB0",
        ["mdi:weather-cloudy"] = "\u2601\uFE0F",
        ["mdi:weather-night"] = "\uD83C\uDF19",
        ["mdi:weather-sunny"] = "\u2600\uFE0F",
        ["mdi:wrench"] = "\uD83D\uDD27",
        ["mdi:zip-box"] = "\uD83D\uDCE6",
        ["mdi:arrow-up-circle"] = "\uD83D\uDD3C",
        ["mdi:arrow-down-circle"] = "\uD83D\uDD3D",
        ["mdi:arrow-down-bold-circle-outline"] = "\uD83D\uDD3D",
        ["mdi:account-plus"] = "\uD83D\uDC4B",
        ["mdi:account-minus"] = "\uD83D\uDEAA",
        ["mdi:login"] = "\uD83D\uDC4B"
    };

    private static bool LooksLikeMdiIdentifier( string key )
    {
        if ( string.IsNullOrWhiteSpace( key ) )
            return false;

        if ( key.StartsWith( "mdi:", StringComparison.OrdinalIgnoreCase ) ||
             key.StartsWith( "mdi-", StringComparison.OrdinalIgnoreCase ) )
            return true;

        return key.Contains( "-" ) || key.Contains( "_" );
    }

    private static bool TryResolveLegacyMdiKey( string key, out string normalizedKey )
    {
        normalizedKey = null;
        if ( !LooksLikeMdiIdentifier( key ) )
            return false;

        string mdiKey = key.Trim();
        if ( mdiKey.StartsWith( "mdi-", StringComparison.OrdinalIgnoreCase ) )
            mdiKey = "mdi:" + mdiKey.Substring( 4 );
        else if ( !mdiKey.StartsWith( "mdi:", StringComparison.OrdinalIgnoreCase ) )
            mdiKey = "mdi:" + mdiKey;

        mdiKey = mdiKey.Replace( "_", "-" );
        if ( GlyphMap.ContainsKey( mdiKey ) )
        {
            normalizedKey = mdiKey;
            return true;
        }

        return false;
    }

    private static string FallbackForUnknownMdi( string mdiKey )
    {
        string k = mdiKey.ToLowerInvariant();
        if ( k.Contains( "chart" ) || k.Contains( "trend" ) )
            return "\uD83D\uDCC8";
        if ( k.Contains( "cash" ) || k.Contains( "coin" ) || k.Contains( "bank" ) || k.Contains( "wallet" ) )
            return "\uD83D\uDCB0";
        if ( k.Contains( "alert" ) || k.Contains( "warning" ) || k.Contains( "danger" ) )
            return "\u26A0\uFE0F";
        if ( k.Contains( "chat" ) || k.Contains( "message" ) )
            return "\uD83D\uDCAC";
        if ( k.Contains( "star" ) || k.Contains( "rank" ) || k.Contains( "crown" ) )
            return "\u2B50\uFE0F";
        if ( k.Contains( "robot" ) || k.Contains( "bot" ) )
            return "\uD83E\uDD16";
        if ( k.Contains( "pickaxe" ) || k.Contains( "mine" ) )
            return "\u26CF\uFE0F";
        if ( k.Contains( "card" ) || k.Contains( "blackjack" ) )
            return "\uD83C\uDCCF";
        if ( k.Contains( "slot" ) )
            return "\uD83C\uDFB0";
        if ( k.Contains( "rocket" ) )
            return "\uD83D\uDE80";
        if ( k.Contains( "clock" ) || k.Contains( "time" ) )
            return "\uD83D\uDD52";

        return "\u2754";
    }

    public static string Glyph( string icon )
    {
        if ( string.IsNullOrWhiteSpace( icon ) )
            return "\u2022";

        string key = icon.Trim();
        if ( key.Equals( "none", StringComparison.OrdinalIgnoreCase ) ||
             key.Equals( "null", StringComparison.OrdinalIgnoreCase ) ||
             key.Equals( "undefined", StringComparison.OrdinalIgnoreCase ) )
            key = "mdi:account-outline";

        if ( GlyphMap.TryGetValue( key, out var glyph ) )
            return glyph;

        if ( TryResolveLegacyMdiKey( key, out var normalized ) &&
             GlyphMap.TryGetValue( normalized, out var normalizedGlyph ) )
            return normalizedGlyph;

        // If this isn't an mdi id, keep original text (legacy emoji/runes).
        if ( !key.Contains( ":" ) )
        {
            if ( LooksLikeMdiIdentifier( key ) )
                return FallbackForUnknownMdi( "mdi:" + key.Replace( "_", "-" ) );

            return key;
        }

        if ( key.StartsWith( "mdi:", StringComparison.OrdinalIgnoreCase ) )
            return FallbackForUnknownMdi( key );

        return "\u2022";
    }
}
