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

namespace Quasar.Server.Controls
{
    public class ModernSidebar : Panel
    {
        public event EventHandler ClientsClicked;
        public event EventHandler SettingsClicked;
        public event EventHandler BuilderClicked;
        public event EventHandler NotificationCentreClicked;
        public event EventHandler GraphViewClicked;
        public event EventHandler AutoTasksClicked;
        public event EventHandler AboutClicked;

        private Button btnClients;
        private Button btnSettings;
        private Button btnBuilder;
        private Button btnNotificationCentre;
        private Button btnGraphView;
        private Button btnAutoTasks;
        private Button btnAbout;
        private Label lblTitle;
        private Panel pnlLogo;

        public ModernSidebar()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.Width = 180;
            this.BackColor = Color.FromArgb(15, 15, 15);
            this.Dock = DockStyle.Left;

            // Logo/Title area
            pnlLogo = new Panel
            {
                Height = 60,
                Dock = DockStyle.Top,
                BackColor = Color.FromArgb(15, 15, 15)
            };

            lblTitle = new Label
            {
                Text = "Velane",
                Font = new Font("Segoe UI", 16, FontStyle.Bold),
                ForeColor = Color.FromArgb(0, 188, 212),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleLeft,
                Dock = DockStyle.Fill,
                Padding = new Padding(20, 0, 0, 0)
            };

            pnlLogo.Controls.Add(lblTitle);
            this.Controls.Add(pnlLogo);

            // Navigation buttons
            int yPos = 70;
            btnClients = CreateNavButton("Clients", yPos);
            btnClients.Click += (s, e) => ClientsClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            btnSettings = CreateNavButton("Settings", yPos);
            btnSettings.Click += (s, e) => SettingsClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            btnBuilder = CreateNavButton("Builder", yPos);
            btnBuilder.Click += (s, e) => BuilderClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            btnNotificationCentre = CreateNavButton("Notification Centre", yPos);
            btnNotificationCentre.Click += (s, e) => NotificationCentreClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            btnGraphView = CreateNavButton("Graph View", yPos);
            btnGraphView.Click += (s, e) => GraphViewClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            btnAutoTasks = CreateNavButton("Auto Tasks", yPos);
            btnAutoTasks.Click += (s, e) => AutoTasksClicked?.Invoke(this, EventArgs.Empty);
            yPos += 45;

            // About at bottom
            btnAbout = CreateNavButton("About", 0);
            btnAbout.Dock = DockStyle.Bottom;
            btnAbout.Click += (s, e) => AboutClicked?.Invoke(this, EventArgs.Empty);

            this.Controls.Add(btnClients);
            this.Controls.Add(btnSettings);
            this.Controls.Add(btnBuilder);
            this.Controls.Add(btnNotificationCentre);
            this.Controls.Add(btnGraphView);
            this.Controls.Add(btnAutoTasks);
            this.Controls.Add(btnAbout);

            // Set Clients as selected by default
            SetSelectedButton(btnClients);
        }

        private Button CreateNavButton(string text, int yPos)
        {
            var btn = new Button
            {
                Text = text,
                Width = 180,
                Height = 40,
                Left = 0,
                Top = yPos,
                FlatStyle = FlatStyle.Flat,
                BackColor = Color.FromArgb(15, 15, 15),
                ForeColor = Color.FromArgb(180, 180, 180),
                Font = new Font("Segoe UI", 10),
                TextAlign = ContentAlignment.MiddleLeft,
                Padding = new Padding(20, 0, 0, 0),
                Cursor = Cursors.Hand
            };

            btn.FlatAppearance.BorderSize = 0;
            btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(25, 25, 25);
            btn.FlatAppearance.MouseDownBackColor = Color.FromArgb(30, 30, 30);

            btn.MouseEnter += (s, e) =>
            {
                if (btn.BackColor != Color.FromArgb(0, 188, 212))
                    btn.BackColor = Color.FromArgb(25, 25, 25);
            };

            btn.MouseLeave += (s, e) =>
            {
                if (btn.BackColor != Color.FromArgb(0, 188, 212))
                    btn.BackColor = Color.FromArgb(15, 15, 15);
            };

            btn.Click += (s, e) => SetSelectedButton(btn);

            return btn;
        }

        private void SetSelectedButton(Button selectedBtn)
        {
            // Reset all buttons
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is Button btn && btn != selectedBtn)
                {
                    btn.BackColor = Color.FromArgb(15, 15, 15);
                    btn.ForeColor = Color.FromArgb(180, 180, 180);
                }
            }

            // Highlight selected
            selectedBtn.BackColor = Color.FromArgb(0, 188, 212);
            selectedBtn.ForeColor = Color.White;
        }

        public void SelectClients() => SetSelectedButton(btnClients);
        public void SelectSettings() => SetSelectedButton(btnSettings);
        public void SelectBuilder() => SetSelectedButton(btnBuilder);
        public void SelectNotificationCentre() => SetSelectedButton(btnNotificationCentre);
        public void SelectGraphView() => SetSelectedButton(btnGraphView);
        public void SelectAutoTasks() => SetSelectedButton(btnAutoTasks);
        public void SelectAbout() => SetSelectedButton(btnAbout);
    }
}
