using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Controls;

namespace Elfisa.UI.Theming
{
    public sealed class ThemeRenderer
    {
        public void Apply(Control root, ThemeColors colors)
        {
            ApplyRecursive(root, colors);
            root.Invalidate(true);
        }

        private static void ApplyRecursive(Control control, ThemeColors colors)
        {
            if (control.Tag as string == "keep-bg")
            {
                ApplyChildren(control, colors);
                return;
            }

            if (control.Tag as string == "chrome-child")
            {
                control.BackColor = colors.BackgroundPrimary;
                control.ForeColor = colors.TextPrimary;
                ApplyChildren(control, colors);
                return;
            }

            if (control is Form form)
            {
                form.BackColor = colors.BackgroundPrimary;
                form.Font = colors.FontRegular13;
            }
            else if (control is ModernPanel panel)
            {
                panel.ApplyTheme(colors);
            }
            else if (control is ModernButton button)
            {
                button.ApplyTheme(colors);
                if (!button.HeaderNavMode && !button.TopNavMode && !button.SidebarMode)
                {
                    ModernButtonStyles.ApplyAuto(button);
                }
            }
            else if (control is ModernDataGridView grid)
            {
                grid.ApplyTheme(colors);
            }
            else if (control is ModernTextBox textBox)
            {
                textBox.ApplyTheme(colors);
            }
            else if (control is ModernComboBox comboBox)
            {
                comboBox.ApplyTheme(colors);
            }
            else if (control is ModernNumericUpDown numeric)
            {
                numeric.ApplyTheme(colors);
            }
            else if (control is ModernDateTimePicker dateTimePicker)
            {
                dateTimePicker.ApplyTheme(colors);
            }
            else if (control is ModernSearchBox searchBox)
            {
                searchBox.ApplyTheme(colors);
            }
            else if (control is ModernSidebar sidebar)
            {
                sidebar.ApplyTheme(colors);
            }
            else if (control is ModernTopNav topNav)
            {
                topNav.ApplyTheme(colors);
            }
            else if (control is ModernAppHeader appHeader)
            {
                appHeader.ApplyTheme(colors);
            }
            else if (control is ModernViewTab viewTab)
            {
                viewTab.ApplyTheme(colors);
            }
            else if (control is ModernDocumentTabStrip tabStrip)
            {
                tabStrip.ApplyTheme(colors);
            }
            else if (control is ModernBrandHeader brandHeader)
            {
                brandHeader.ApplyTheme(colors);
            }
            else if (control is ModernSectionHeader sectionHeader)
            {
                sectionHeader.ApplyTheme(colors, sectionHeader.Tag as string == "cart");
            }
            else if (control is ModernTabControl modernTabControl)
            {
                modernTabControl.ApplyTheme(colors);
            }
            else if (control is ModernSplitContainer modernSplitContainer)
            {
                modernSplitContainer.ApplyTheme(colors);
            }
            else if (control is ModernUploadZone uploadZone)
            {
                uploadZone.ApplyTheme(colors);
            }
            else if (control is ModernUploadPanel uploadPanel)
            {
                uploadPanel.ApplyTheme(colors);
            }
            else if (control is DataGridView gridView)
            {
                gridView.EnableHeadersVisualStyles = false;
                gridView.BackgroundColor = colors.GridRowBackground;
                gridView.GridColor = colors.GridBorder;
                gridView.ColumnHeadersDefaultCellStyle.BackColor = colors.GridHeaderBackground;
                gridView.ColumnHeadersDefaultCellStyle.ForeColor = colors.GridHeaderForeground;
                gridView.DefaultCellStyle.BackColor = colors.GridRowBackground;
                gridView.DefaultCellStyle.ForeColor = colors.TextPrimary;
                gridView.DefaultCellStyle.SelectionBackColor = colors.GridRowSelected;
                gridView.DefaultCellStyle.SelectionForeColor = colors.GridRowSelectedText;
                gridView.AlternatingRowsDefaultCellStyle.BackColor = colors.GridRowAlternate;
            }
            else if (control is CheckBox checkBox)
            {
                checkBox.ForeColor = colors.TextPrimary;
                checkBox.BackColor = Color.Transparent;
            }
            else if (control is NumericUpDown numericUpDown)
            {
                numericUpDown.BackColor = colors.InputBackground;
                numericUpDown.ForeColor = colors.TextPrimary;
            }
            else if (control is TextBox legacyTextBox)
            {
                legacyTextBox.BackColor = colors.InputBackground;
                legacyTextBox.ForeColor = colors.TextPrimary;
            }
            else if (control is TabControl tabControl)
            {
                tabControl.BackColor = colors.BackgroundSecondary;
                tabControl.ForeColor = colors.TextPrimary;
            }
            else if (control is SplitContainer splitContainer)
            {
                splitContainer.BackColor = colors.Mode == ThemeMode.Dark ? colors.BorderSubtle : colors.Border;
                splitContainer.Panel1.BackColor = colors.BackgroundPrimary;
                splitContainer.Panel2.BackColor = colors.BackgroundPrimary;
            }
            else if (control.Tag as string == "toolbar")
            {
                control.BackColor = colors.ToolbarBackground;
            }
            else if (control.Tag as string == "status-footer")
            {
                control.BackColor = colors.Surface;
            }
            else if (control is Panel || control is UserControl || control is TableLayoutPanel || control is FlowLayoutPanel)
            {
                control.BackColor = colors.BackgroundSecondary;
                control.ForeColor = colors.TextPrimary;
            }
            else if (control is Label label)
            {
                label.ForeColor = colors.TextPrimary;
                if (label.Tag as string == "muted")
                {
                    label.ForeColor = colors.TextMuted;
                }
            }

            ApplyChildren(control, colors);
        }

        private static void ApplyChildren(Control control, ThemeColors colors)
        {
            foreach (Control child in control.Controls)
            {
                ApplyRecursive(child, colors);
            }
        }
    }
}
