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

namespace BettingPredictor.UI
{
    /// <summary>
    /// Pulsante con stile moderno (arrotondato, con effetti hover)
    /// </summary>
    internal class ModernButton : Button
    {
        private bool _isHovered;
        private bool _isPressed;
        private Color _accentColor;

        public Color AccentColor
        {
            get => _accentColor;
            set { _accentColor = value; Invalidate(); }
        }

        public bool IsPrimary { get; set; } = true;

        public ModernButton()
        {
            _accentColor = ModernTheme.PrimaryColor;
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0;
            Font = new Font("Segoe UI Semibold", 10F);
            ForeColor = Color.White;
            Cursor = Cursors.Hand;
            Height = 36;
            MinimumSize = new Size(100, 36);
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.Opaque, true);
        }

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

            // Pulisci sfondo
            using (var bgBrush = new SolidBrush(Parent?.BackColor ?? ModernTheme.ContentBackground))
                g.FillRectangle(bgBrush, ClientRectangle);

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

            Color bgColor;
            if (!Enabled)
                bgColor = Color.FromArgb(100, _accentColor);
            else if (_isPressed)
                bgColor = ControlPaint.Dark(_accentColor, 0.15f);
            else if (_isHovered)
                bgColor = ControlPaint.Light(_accentColor, 0.15f);
            else
                bgColor = _accentColor;

            using (var path = ModernTheme.GetRoundedRectPath(rect, 6))
            {
                if (IsPrimary)
                {
                    using (var brush = new SolidBrush(bgColor))
                        g.FillPath(brush, path);
                }
                else
                {
                    using (var brush = new SolidBrush(ModernTheme.CardBackground))
                        g.FillPath(brush, path);
                    using (var pen = new Pen(bgColor, 2))
                        g.DrawPath(pen, path);
                }

                var textColor = IsPrimary ? Color.FromArgb(24, 24, 37) : ModernTheme.TextPrimary;
                using (var textBrush = new SolidBrush(Enabled ? textColor : Color.FromArgb(80, textColor)))
                {
                    var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
                    g.DrawString(Text, Font, textBrush, rect, sf);
                }
            }
        }

        protected override void OnMouseEnter(EventArgs e) { _isHovered = true; Invalidate(); base.OnMouseEnter(e); }
        protected override void OnMouseLeave(EventArgs e) { _isHovered = false; _isPressed = false; Invalidate(); base.OnMouseLeave(e); }
        protected override void OnMouseDown(MouseEventArgs e) { _isPressed = true; Invalidate(); base.OnMouseDown(e); }
        protected override void OnMouseUp(MouseEventArgs e) { _isPressed = false; Invalidate(); base.OnMouseUp(e); }
    }
}
