using System.Drawing;
using System.Drawing.Drawing2D;
using Elfisa.UI.Controls;
using Elfisa.UI.Theming;

namespace Elfisa.UI.Helpers
{
    /// <summary>
    /// Градиент для Primary/Danger в тон активной палитры (Clinical Pro teal, не «Google blue»).
    /// </summary>
    internal static class SkillGradientButtonPaint
    {
        public static bool UsesGradient(ModernButtonStyle style, bool compactIconMode)
        {
            return style == ModernButtonStyle.Primary || style == ModernButtonStyle.Danger;
        }

        public static int GetCornerRadius(ModernButtonStyle style, bool compactIconMode)
        {
            return compactIconMode ? 6 : style == ModernButtonStyle.Primary ? 10 : 8;
        }

        public static void GetGradient(
            ModernButtonStyle style,
            bool hover,
            bool pressed,
            out Color top,
            out Color bottom)
        {
            var theme = ThemeManager.Colors;

            if (style == ModernButtonStyle.Danger)
            {
                var danger = theme.Danger;
                if (pressed)
                {
                    top = GraphicsExtensions.Blend(danger, Color.Black, 0.22f);
                    bottom = GraphicsExtensions.Blend(danger, Color.Black, 0.32f);
                }
                else if (hover)
                {
                    top = GraphicsExtensions.Blend(danger, Color.White, 0.18f);
                    bottom = danger;
                }
                else
                {
                    top = GraphicsExtensions.Blend(danger, Color.White, 0.08f);
                    bottom = GraphicsExtensions.Blend(danger, Color.Black, 0.12f);
                }

                return;
            }

            // Primary — мягкий teal шапки, без синего «крика».
            if (pressed)
            {
                top = theme.AccentPrimaryPressed;
                bottom = GraphicsExtensions.Blend(theme.AccentPrimaryPressed, Color.Black, 0.12f);
            }
            else if (hover)
            {
                top = theme.AccentPrimaryHover;
                bottom = theme.AccentPrimary;
            }
            else
            {
                top = theme.TitleBarBottom;
                bottom = theme.TitleBarTop;
            }
        }

        public static void Paint(
            Graphics g,
            Rectangle clientRect,
            ModernButtonStyle style,
            bool compactIconMode,
            bool hover,
            bool pressed,
            bool enabled)
        {
            var theme = ThemeManager.Colors;
            var radius = GetCornerRadius(style, compactIconMode);
            var buttonRect = new Rectangle(
                clientRect.X,
                clientRect.Y,
                clientRect.Width - 1,
                clientRect.Height - 1);

            using (var clipPath = GraphicsExtensions.CreateRoundedRectangle(buttonRect, radius))
            {
                g.SetClip(clipPath);

                if (!enabled)
                {
                    using (var brush = new SolidBrush(GraphicsExtensions.Blend(theme.Border, theme.Surface, 0.35f)))
                    {
                        GraphicsExtensions.FillRoundedRectangle(g, brush, buttonRect, radius);
                    }

                    g.ResetClip();
                    return;
                }

                GetGradient(style, hover, pressed, out var top, out var bottom);

                using (var path = GraphicsExtensions.CreateRoundedRectangle(buttonRect, radius))
                using (var brush = new LinearGradientBrush(buttonRect, top, bottom, LinearGradientMode.Vertical))
                {
                    g.FillPath(brush, path);
                }

                g.ResetClip();
            }
        }

        public static Rectangle GetContentRect(Rectangle clientRect, ModernButtonStyle style, bool compactIconMode, bool enabled)
        {
            return new Rectangle(
                clientRect.X,
                clientRect.Y,
                clientRect.Width - 1,
                clientRect.Height - 1);
        }
    }
}
