using Telerik.WinControls;

namespace Pilz.UI.Telerik.Theming;

public static class ThemeHelper
{
    [Obsolete()]
    public static void ApplyApplicationTheme(ApplicationTheme theme, Func<RadThemeComponentBase> getLightTheme, Func<RadThemeComponentBase> getDarkTheme)
    {
        ApplyApplicationTheme(theme, getLightTheme, getDarkTheme, getDarkTheme);
    }

    [Obsolete()]
    public static void ApplyApplicationTheme(ApplicationTheme theme, Func<RadThemeComponentBase> getLightTheme, Func<RadThemeComponentBase> getDarkTheme, Func<RadThemeComponentBase> getGrayTheme)
    {
        ApplyApplicationTheme(new(theme, HighContrastMode.Auto), def => def.Theme switch
        {
            ApplicationTheme.Light => getLightTheme(),
            ApplicationTheme.Dark => getDarkTheme(),
            ApplicationTheme.Gray => getGrayTheme(),
            _ => throw new NotImplementedException(),
        });
    }

    public static void ApplyApplicationTheme(ThemeDefinition themeDefinition, Func<ThemeDefinition, RadThemeComponentBase> getTheme)
    {
        ThemeResolutionService.ApplicationThemeName = GetThemeName(themeDefinition, getTheme).ThemeName;
    }

    public static RadThemeComponentBase GetThemeName(ThemeDefinition themeDefinition, Func<ThemeDefinition, RadThemeComponentBase> getTheme)
    {
        ApplicationTheme themeToUse;
        HighContrastMode highContrast;

        if (themeDefinition.HighContrast == HighContrastMode.Enable || themeDefinition.HighContrast == HighContrastMode.Auto && SystemInformation.HighContrast)
            highContrast = HighContrastMode.Enable;
        else
            highContrast = HighContrastMode.Disable;

        if (themeDefinition.Theme == ApplicationTheme.Light || themeDefinition.Theme == ApplicationTheme.Auto && WindowsSettings.AppsUseLightTheme)
            themeToUse = ApplicationTheme.Light;
        else if (themeDefinition.Theme == ApplicationTheme.Dark || themeDefinition.Theme == ApplicationTheme.Auto && !WindowsSettings.AppsUseLightTheme)
            themeToUse = ApplicationTheme.Dark;
        else
            themeToUse = ApplicationTheme.Gray;

        return getTheme(new(themeToUse, highContrast));
    }

    public static bool IsDarkTheme(ApplicationTheme theme)
    {
        return theme == ApplicationTheme.Dark || theme == ApplicationTheme.Gray || theme == ApplicationTheme.Auto && !WindowsSettings.AppsUseLightTheme;
    }

    public static bool IsHighContrast(HighContrastMode theme)
    {
        return theme == HighContrastMode.Enable || theme == HighContrastMode.Auto && SystemInformation.HighContrast;
    }
}
