using System;
using System.Collections.Generic;

namespace Elfisa.UI.Theming
{
    public static class ThemeManager
    {
        private static readonly Dictionary<ThemeVariant, IThemePalette> Palettes = new Dictionary<ThemeVariant, IThemePalette>
        {
            { ThemeVariant.PharmaTrust, new PharmaTrustPalette() },
            { ThemeVariant.Apotheca, new ApothecaPalette() },
            { ThemeVariant.ClinicalPro, new ClinicalProPalette() },
            { ThemeVariant.LightOffice, new LightOfficePalette() },
            { ThemeVariant.DarkRider, new DarkRiderPalette() },
            { ThemeVariant.DarkFluent, new DarkFluentPalette() },
            { ThemeVariant.FluentWin11, new FluentWin11Palette() },
            { ThemeVariant.DarkProductivity, new DarkProductivityPalette() }
        };

        private static ThemeVariant _currentVariant = ThemeVariant.ClinicalPro;

        public static event EventHandler ThemeChanged;

        public static ThemeVariant CurrentVariant
        {
            get => _currentVariant;
            set
            {
                if (_currentVariant == value)
                {
                    return;
                }

                _currentVariant = value;
                ThemeChanged?.Invoke(null, EventArgs.Empty);
            }
        }

        public static ThemeColors Colors => Palettes[_currentVariant].Colors;

        public static ThemeMode CurrentMode => Colors.Mode;

        public static IReadOnlyList<ThemeVariant> AvailableVariants { get; } =
            new[]
            {
                ThemeVariant.PharmaTrust,
                ThemeVariant.Apotheca,
                ThemeVariant.ClinicalPro,
                ThemeVariant.LightOffice,
                ThemeVariant.DarkRider,
                ThemeVariant.DarkFluent,
                ThemeVariant.FluentWin11,
                ThemeVariant.DarkProductivity
            };

        public static string GetVariantDisplayName(ThemeVariant variant)
        {
            return Palettes[variant].Colors.Name;
        }

        public static void CycleVariant()
        {
            var next = ((int)CurrentVariant + 1) % AvailableVariants.Count;
            CurrentVariant = AvailableVariants[next];
        }

        public static bool IsDarkProductivityActive => CurrentVariant == ThemeVariant.DarkProductivity;

        public static void ToggleProductivityTheme()
        {
            CurrentVariant = IsDarkProductivityActive
                ? ThemeVariant.ClinicalPro
                : ThemeVariant.DarkProductivity;
        }
    }
}
