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

namespace Elfisa.UI.Helpers
{
    /// <summary>
    /// Градиент из skill winforms-modern-buttons для ModernButton (без тяжёлой тени — она давала артефакты на toolbar).
    /// </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)
        {
            if (style == ModernButtonStyle.Danger)
            {
                if (pressed)
                {
                    top = Color.FromArgb(175, 30, 30);
                    bottom = Color.FromArgb(140, 20, 20);
                }
                else if (hover)
                {
                    top = Color.FromArgb(245, 110, 110);
                    bottom = Color.FromArgb(215, 55, 55);
                }
                else
                {
                    top = Color.FromArgb(235, 87, 87);
                    bottom = Color.FromArgb(200, 40, 40);
                }

                return;
            }

            if (pressed)
            {
                top = Color.FromArgb(25, 70, 160);
                bottom = Color.FromArgb(15, 55, 130);
            }
            else if (hover)
            {
                top = Color.FromArgb(90, 150, 255);
                bottom = Color.FromArgb(45, 105, 220);
            }
            else
            {
                top = Color.FromArgb(66, 133, 244);
                bottom = Color.FromArgb(30, 87, 194);
            }
        }

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

            g.SetClip(clientRect);

            if (!enabled)
            {
                using (var brush = new SolidBrush(Color.FromArgb(220, 230, 235)))
                {
                    GraphicsExtensions.FillRoundedRectangle(g, brush, buttonRect, radius);
                }

                return;
            }

            // Лёгкая тень — один слой, без чёрной полосы снизу
            var shadowRect = new Rectangle(buttonRect.X, buttonRect.Y + 1, buttonRect.Width, buttonRect.Height);
            using (var shadowPath = GraphicsExtensions.CreateRoundedRectangle(shadowRect, radius))
            using (var shadowBrush = new SolidBrush(Color.FromArgb(28, 0, 0, 0)))
            {
                g.FillPath(shadowBrush, shadowPath);
            }

            var faceRect = new Rectangle(buttonRect.X, buttonRect.Y, buttonRect.Width, buttonRect.Height - 1);
            GetGradient(style, hover, pressed, out var top, out var bottom);

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

                using (var highlightPen = new Pen(Color.FromArgb(36, Color.White), 1f))
                {
                    g.DrawArc(highlightPen, faceRect.X, faceRect.Y, radius * 2, radius * 2, 180, 90);
                }
            }

            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);
        }
    }
}
