using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using PolyTrader.Core.Modularity;

namespace PolyTrader.App.Avalonia.Shell
{
    /// <summary>
    /// Löst die toolkit-neutralen <see cref="ModuleView.IconKey"/>-Schlüssel gegen die
    /// Avalonia-Bildressourcen auf. Gegenstück zum gleichnamigen Helfer der WinForms-Shell –
    /// <b>dieselben Schlüssel, dieselben PNG-Dateien</b>, sodass Core und Module unverändert bleiben.
    /// </summary>
    public static class ViewIcons
    {
        /// <summary>Symbol-Schlüssel → Dateiname in <c>Resources/</c> (als Avalonia-Asset eingebettet).</summary>
        private static readonly Dictionary<string, string> FileByKey = new()
        {
            ["start"] = "dashboard.png",
            ["dashboard"] = "token_quantifier.png",
            ["settings"] = "setting_tools.png",
            ["terminal"] = "error_log.png",
            ["jobs"] = "system_time.png",
            ["accounting"] = "coins_in_hand.png",
            ["copytrading"] = "cross_reference.png",
            ["resolutionfarming"] = "file_start_workflow.png",
            ["supervisor"] = "emotion_batman.png",
        };

        /// <summary>Standard-Symbolschlüssel je View-ID – identisch zur WinForms-Shell.</summary>
        private static readonly Dictionary<string, string> DefaultKeyByViewId = new()
        {
            [AvaloniaUiHost.StartViewId] = "start",
            ["core.dashboard"] = "dashboard",
            ["core.settings"] = "settings",
            ["core.terminal"] = "terminal",
            ["core.jobs"] = "jobs",
            ["accounting.main"] = "accounting",
            ["copytrading.main"] = "copytrading",
            ["resolutionfarming.main"] = "resolutionfarming",
            ["supervisor.main"] = "supervisor",
        };

        private static readonly ConcurrentDictionary<string, Bitmap?> Cache = new();

        /// <summary>Bild zum Schlüssel, oder <c>null</c> (kein Symbol / Datei fehlt).</summary>
        public static Bitmap? Resolve(string? iconKey)
        {
            if (iconKey == null || !FileByKey.TryGetValue(iconKey, out var file)) return null;

            return Cache.GetOrAdd(iconKey, _ =>
            {
                try
                {
                    var uri = new Uri($"avares://PolyTrader.App.Avalonia/Assets/{file}");
                    using var stream = AssetLoader.Open(uri);
                    return new Bitmap(stream);
                }
                catch
                {
                    // Ein fehlendes Symbol darf die Oberflaeche nie aufhalten – Menueeintrag bleibt Text-only.
                    return null;
                }
            });
        }

        /// <summary>Setzt bei allen registrierten Views den Standard-Symbolschlüssel, falls noch keiner gesetzt ist.</summary>
        public static void AssignDefaults(IModuleUiHost uiHost)
        {
            foreach (var view in uiHost.Views)
                if (view.IconKey == null && DefaultKeyByViewId.TryGetValue(view.Id, out var key))
                    view.IconKey = key;
        }
    }
}
