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

namespace BettingPredictor.UI
{
    /// <summary>
    /// Pulsante di navigazione per la sidebar
    /// </summary>
    internal class NavButton : Button
    {
        private bool _isActive;
        private bool _isHovered;
        private readonly string _icon;

        public bool IsActive
        {
            get => _isActive;
            set { _isActive = value; Invalidate(); }
        }

        public NavButton(string text, string icon)
        {
            _icon = icon;
            Text = text;
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0;
            FlatAppearance.MouseOverBackColor = Color.Transparent;
            FlatAppearance.MouseDownBackColor = Color.Transparent;
            Font = ModernTheme.NavFont;
            ForeColor = ModernTheme.SidebarText;
            BackColor = ModernTheme.SidebarBackground;
            Cursor = Cursors.Hand;
            Height = 44;
            Dock = DockStyle.Top;
            Padding = new Padding(0);
            Margin = new Padding(0);
            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;

            var rect = ClientRectangle;

            // Sfondo pieno sidebar
            using (var bg = new SolidBrush(ModernTheme.SidebarBackground))
                g.FillRectangle(bg, rect);

            // Stato attivo: rettangolo arrotondato + indicatore
            if (_isActive)
            {
                var activeRect = new Rectangle(8, 4, rect.Width - 16, rect.Height - 8);
                using (var path = ModernTheme.GetRoundedRectPath(activeRect, 6))
                using (var brush = new SolidBrush(ModernTheme.SidebarActive))
                    g.FillPath(brush, path);

                using (var bar = new SolidBrush(ModernTheme.SidebarActive))
                    g.FillRectangle(bar, 0, 10, 3, rect.Height - 20);
            }
            // Stato hover (solo se non attivo)
            else if (_isHovered)
            {
                var hoverRect = new Rectangle(8, 4, rect.Width - 16, rect.Height - 8);
                using (var path = ModernTheme.GetRoundedRectPath(hoverRect, 6))
                using (var brush = new SolidBrush(ModernTheme.SidebarHover))
                    g.FillPath(brush, path);
            }

            // Colori testo/icona
            var fg = _isActive ? ModernTheme.SidebarActiveText : ModernTheme.SidebarText;

            // Icona
            using (var iconFont = new Font("Segoe UI", 11F, FontStyle.Bold))
            using (var brush = new SolidBrush(fg))
            {
                var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
                g.DrawString(_icon, iconFont, brush, new RectangleF(14, 0, 28, rect.Height), sf);
            }

            // Testo
            using (var brush = new SolidBrush(fg))
            {
                var sf = new StringFormat
                {
                    Alignment = StringAlignment.Near,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.NoWrap
                };
                g.DrawString(Text, Font, brush, new RectangleF(46, 0, rect.Width - 54, rect.Height), sf);
            }
        }

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

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