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 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 ModernUploadZone uploadZone)
            {
                uploadZone.ApplyTheme(colors);
            }
            else if (control is ModernUploadPanel uploadPanel)
            {
                uploadPanel.ApplyTheme(colors);
            }
            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)
            {
                if (control.Tag as string == "chrome-child" || control.Tag as string == "keep-bg")
                {
                    return;
                }

                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;
                }
            }

            foreach (Control child in control.Controls)
            {
                ApplyRecursive(child, colors);
            }
        }
    }
}
