using Quasar.Server.Models;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Quasar.Server.Utilities
{
    public static class DarkModeHelper
    {
        private static readonly Color DarkBg = Color.FromArgb(45, 45, 48);
        private static readonly Color DarkControlBg = Color.FromArgb(37, 37, 38);
        private static readonly Color DarkFg = Color.White;
        private static readonly Color DarkBorder = Color.FromArgb(63, 63, 70);

        public static void Initialize()
        {
            // Set ThemeManager's dark mode flag
            ThemeManager.DarkModeEnabled = Settings.DarkMode;
        }

        public static void ApplyToForm(Form form)
        {
            if (!Settings.DarkMode) return;

            // Apply immediately
            ApplyToControl(form);

            // Reapply after form is shown to override any designer settings
            EventHandler shownHandler = null;
            shownHandler = (s, e) =>
            {
                ApplyToControl(form);
                form.Invalidate(true);
                form.Shown -= shownHandler;
            };
            form.Shown += shownHandler;
            
            // Also hook into Load event
            EventHandler loadHandler = null;
            loadHandler = (s, e) =>
            {
                ApplyToControl(form);
                form.Load -= loadHandler;
            };
            form.Load += loadHandler;
        }

        private static void ApplyToControl(Control control)
        {
            try
            {
                if (control is Form)
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }
                else if (control is TabPage)
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }
                else if (control is TabControl)
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }
                else if (control is Panel)
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }
                else if (control is GroupBox)
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }
                else if (control is Label)
                {
                    control.ForeColor = DarkFg;
                    control.BackColor = Color.Transparent;
                }
                else if (control is Button btn)
                {
                    btn.BackColor = DarkControlBg;
                    btn.ForeColor = DarkFg;
                    btn.FlatStyle = FlatStyle.Flat;
                    btn.FlatAppearance.BorderColor = DarkBorder;
                }
                else if (control is TextBox txt)
                {
                    txt.BackColor = DarkControlBg;
                    txt.ForeColor = DarkFg;
                    txt.BorderStyle = BorderStyle.FixedSingle;
                }
                else if (control is RichTextBox rtxt)
                {
                    rtxt.BackColor = DarkControlBg;
                    rtxt.ForeColor = DarkFg;
                    rtxt.BorderStyle = BorderStyle.FixedSingle;
                }
                else if (control is ListBox lst)
                {
                    lst.BackColor = DarkControlBg;
                    lst.ForeColor = DarkFg;
                }
                else if (control is ListView lv)
                {
                    lv.BackColor = DarkControlBg;
                    lv.ForeColor = DarkFg;
                    lv.BorderStyle = BorderStyle.FixedSingle;
                }
                else if (control is TreeView tv)
                {
                    tv.BackColor = DarkControlBg;
                    tv.ForeColor = DarkFg;
                }
                else if (control is CheckBox)
                {
                    control.ForeColor = DarkFg;
                }
                else if (control is RadioButton)
                {
                    control.ForeColor = DarkFg;
                }
                else if (control is NumericUpDown num)
                {
                    num.BackColor = DarkControlBg;
                    num.ForeColor = DarkFg;
                }
                else if (control is ComboBox cmb)
                {
                    cmb.BackColor = DarkControlBg;
                    cmb.ForeColor = DarkFg;
                }
                else if (control is DataGridView dgv)
                {
                    dgv.BackgroundColor = DarkControlBg;
                    dgv.ForeColor = DarkFg;
                    dgv.GridColor = DarkBorder;
                    dgv.DefaultCellStyle.BackColor = DarkControlBg;
                    dgv.DefaultCellStyle.ForeColor = DarkFg;
                    dgv.DefaultCellStyle.SelectionBackColor = Color.FromArgb(62, 62, 64);
                    dgv.DefaultCellStyle.SelectionForeColor = DarkFg;
                    dgv.ColumnHeadersDefaultCellStyle.BackColor = DarkBg;
                    dgv.ColumnHeadersDefaultCellStyle.ForeColor = DarkFg;
                    dgv.RowHeadersDefaultCellStyle.BackColor = DarkBg;
                    dgv.RowHeadersDefaultCellStyle.ForeColor = DarkFg;
                    dgv.EnableHeadersVisualStyles = false;
                    dgv.BorderStyle = BorderStyle.FixedSingle;
                }
                else if (control is PictureBox)
                {
                    control.BackColor = Color.Transparent;
                }
                else if (control.GetType().Name == "Line")
                {
                    control.BackColor = DarkBorder;
                }
                else if (control.GetType().Name == "DotNetBarTabControl")
                {
                    control.BackColor = DarkBg;
                    control.ForeColor = DarkFg;
                }

                // Apply to MenuStrip and ContextMenuStrip
                if (control is MenuStrip ms)
                {
                    ms.BackColor = DarkControlBg;
                    ms.ForeColor = DarkFg;
                    ms.Renderer = new DarkMenuRenderer();
                }
                else if (control is ContextMenuStrip cms)
                {
                    cms.BackColor = DarkControlBg;
                    cms.ForeColor = DarkFg;
                    cms.Renderer = new DarkMenuRenderer();
                }
                else if (control is StatusStrip ss)
                {
                    ss.BackColor = DarkControlBg;
                    ss.ForeColor = DarkFg;
                    ss.Renderer = new DarkMenuRenderer();
                }

                // Recursively apply to child controls
                foreach (Control child in control.Controls)
                {
                    ApplyToControl(child);
                }
            }
            catch
            {
                // Ignore errors
            }
        }

        private class DarkMenuRenderer : ToolStripProfessionalRenderer
        {
            public DarkMenuRenderer() : base(new DarkColorTable()) { }

            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (e.Item.Selected)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(62, 62, 64)), e.Item.ContentRectangle);
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(DarkControlBg), e.Item.ContentRectangle);
                }
            }

            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                e.TextColor = DarkFg;
                base.OnRenderItemText(e);
            }
        }

        private class DarkColorTable : ProfessionalColorTable
        {
            public override Color MenuItemSelected => Color.FromArgb(62, 62, 64);
            public override Color MenuItemSelectedGradientBegin => Color.FromArgb(62, 62, 64);
            public override Color MenuItemSelectedGradientEnd => Color.FromArgb(62, 62, 64);
            public override Color MenuItemBorder => DarkBorder;
            public override Color MenuBorder => DarkBorder;
            public override Color MenuItemPressedGradientBegin => DarkControlBg;
            public override Color MenuItemPressedGradientEnd => DarkControlBg;
            public override Color ToolStripDropDownBackground => DarkControlBg;
            public override Color ImageMarginGradientBegin => DarkControlBg;
            public override Color ImageMarginGradientMiddle => DarkControlBg;
            public override Color ImageMarginGradientEnd => DarkControlBg;
        }
    }
}
