using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Elfisa.ButtonSkillDemo;

/// <summary>
/// Кнопка из skill «Custom Modern Button» (отдельно от Elfisa.UI.ModernButton).
/// </summary>
public sealed class SkillModernButton : Button
{
    public Color BorderColor { get; set; } = Color.FromArgb(0, 122, 204);
    public Color HoverColor { get; set; } = Color.FromArgb(0, 140, 230);
    public Color PressedColor { get; set; } = Color.FromArgb(0, 100, 180);
    public int BorderRadius { get; set; } = 12;
    public int BorderSize { get; set; } = 2;

    private bool _isHover;
    private bool _isPressed;

    public SkillModernButton()
    {
        FlatStyle = FlatStyle.Flat;
        FlatAppearance.BorderSize = 0;
        BackColor = Color.FromArgb(30, 30, 30);
        ForeColor = Color.White;
        Font = new Font("Segoe UI", 10.5f, FontStyle.Bold);
        Cursor = Cursors.Hand;
        Size = new Size(160, 45);

        SetStyle(ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.UserPaint |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.ResizeRedraw, true);
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

        var rect = new Rectangle(0, 0, Width - 1, Height - 1);

        var currentColor = BackColor;
        if (_isPressed)
        {
            currentColor = PressedColor;
        }
        else if (_isHover)
        {
            currentColor = HoverColor;
        }

        using (var path = GetRoundedPath(rect, BorderRadius))
        {
            using (var brush = new SolidBrush(currentColor))
            {
                g.FillPath(brush, path);
            }

            if (BorderSize > 0)
            {
                using (var pen = new Pen(BorderColor, BorderSize))
                {
                    g.DrawPath(pen, path);
                }
            }
        }

        var flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine;
        TextRenderer.DrawText(g, Text, Font, rect, ForeColor, flags);
    }

    private static GraphicsPath GetRoundedPath(Rectangle rect, int radius)
    {
        var path = new GraphicsPath();
        var r = Math.Max(0, Math.Min(radius, Math.Min(rect.Width, rect.Height) / 2));
        if (r == 0)
        {
            path.AddRectangle(rect);
            return path;
        }

        var diameter = r * 2;
        path.StartFigure();
        path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
        path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
        path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
        path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
        path.CloseFigure();
        return path;
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        _isHover = true;
        Invalidate();
        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        _isHover = false;
        _isPressed = false;
        Invalidate();
        base.OnMouseLeave(e);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _isPressed = true;
            Invalidate();
        }
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        _isPressed = false;
        Invalidate();
        base.OnMouseUp(e);
    }
}
