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

namespace Электронная_Фармация.Classes
{
    public static class UiThemeHelper
    {
        public static void ApplyToControlTree(Control root)
        {
            if (root == null)
            {
                return;
            }

            new ThemeService().Apply(root);
            ApplyLegacyControls(root, ThemeManager.Colors);
        }

        private static void ApplyLegacyControls(Control control, ThemeColors theme)
        {
            if (control is ModernDataGridView modernGrid)
            {
                modernGrid.ApplyTheme(theme);
            }
            else if (control is ModernButton modernButton)
            {
                modernButton.ApplyTheme(theme);
                if (!modernButton.HeaderNavMode && !modernButton.TopNavMode && !modernButton.SidebarMode)
                {
                    modernButton.AllowTextEllipsis = false;
                    ModernButtonStyles.ApplyAuto(modernButton);
                }
            }
            else if (control is ModernTextBox modernTextBox)
            {
                modernTextBox.ApplyTheme(theme);
            }
            else if (control is ModernNumericUpDown modernNumeric)
            {
                modernNumeric.ApplyTheme(theme);
            }
            else if (control is ModernSearchBox modernSearch)
            {
                modernSearch.ApplyTheme(theme);
            }
            else if (control is DataGridView grid)
            {
                grid.EnableHeadersVisualStyles = false;
                grid.BackgroundColor = theme.GridRowBackground;
                grid.GridColor = theme.GridBorder;
                grid.ColumnHeadersDefaultCellStyle.BackColor = theme.GridHeaderBackground;
                grid.ColumnHeadersDefaultCellStyle.ForeColor = theme.GridHeaderForeground;
                grid.ColumnHeadersDefaultCellStyle.Font = theme.FontSemibold14;
                grid.DefaultCellStyle.BackColor = theme.GridRowBackground;
                grid.DefaultCellStyle.ForeColor = theme.TextPrimary;
                grid.DefaultCellStyle.SelectionBackColor = theme.GridRowSelected;
                grid.DefaultCellStyle.SelectionForeColor = theme.GridRowSelectedText;
                grid.DefaultCellStyle.Font = theme.FontRegular13;
                grid.AlternatingRowsDefaultCellStyle.BackColor = theme.GridRowAlternate;
            }
            else if (control is Button button)
            {
                button.FlatStyle = FlatStyle.Flat;
                button.FlatAppearance.BorderColor = theme.Border;
                button.BackColor = theme.Surface;
                button.ForeColor = theme.TextPrimary;
                button.Font = theme.FontRegular13;
            }
            else if (control is TextBox textBox)
            {
                textBox.BackColor = theme.InputBackground;
                textBox.ForeColor = theme.TextPrimary;
                textBox.BorderStyle = BorderStyle.FixedSingle;
                textBox.Font = theme.FontRegular13;
            }
            else if (control is RichTextBox richTextBox)
            {
                richTextBox.BackColor = theme.BackgroundPrimary;
                richTextBox.ForeColor = theme.TextPrimary;
                richTextBox.BorderStyle = BorderStyle.None;
                richTextBox.Font = theme.FontRegular13;
                richTextBox.ReadOnly = true;
            }
            else if (control is NumericUpDown numeric)
            {
                numeric.BackColor = theme.InputBackground;
                numeric.ForeColor = theme.TextPrimary;
                numeric.Font = theme.FontRegular13;
            }
            else if (control is CheckBox checkBox)
            {
                checkBox.ForeColor = theme.TextPrimary;
                checkBox.Font = theme.FontRegular13;
            }
            else if (control is Label label)
            {
                label.ForeColor = theme.TextPrimary;
                label.Font = theme.FontRegular13;
            }
            else if (control is ModernTabControl modernTabControl)
            {
                modernTabControl.ApplyTheme(theme);
            }
            else if (control is ModernSplitContainer modernSplitContainer)
            {
                modernSplitContainer.ApplyTheme(theme);
            }
            else if (control is TabControl tabControl)
            {
                tabControl.BackColor = theme.BackgroundSecondary;
                tabControl.Font = theme.FontRegular13;
            }
            else if (control is ToolStrip toolStrip)
            {
                toolStrip.BackColor = theme.TitleBarBottom;
                toolStrip.ForeColor = Color.White;
            }
            else if (control is SplitContainer splitContainer)
            {
                splitContainer.BackColor = theme.Mode == ThemeMode.Dark ? theme.BorderSubtle : theme.Border;
                splitContainer.Panel1.BackColor = theme.BackgroundPrimary;
                splitContainer.Panel2.BackColor = theme.BackgroundPrimary;
            }
            else if (control is Panel || control is UserControl || control is TableLayoutPanel)
            {
                var tag = control.Tag as string;
                if (tag == "keep-bg")
                {
                    // preserve custom background
                }
                else if (tag == "chrome-child")
                {
                    control.BackColor = theme.BackgroundPrimary;
                    control.ForeColor = theme.TextPrimary;
                }
                else if (tag == "toolbar")
                {
                    control.BackColor = theme.Surface;
                    control.ForeColor = theme.TextPrimary;
                }
                else if (tag == "status-footer")
                {
                    control.BackColor = theme.CartHeaderBackground;
                    control.ForeColor = theme.TextPrimary;
                }
                else if (tag == "surface-bar")
                {
                    control.BackColor = theme.SectionHeaderBackground;
                    control.ForeColor = theme.TextPrimary;
                }
                else if (tag == "markup-bar")
                {
                    control.BackColor = theme.AccentPrimarySoft;
                    control.ForeColor = theme.TextPrimary;
                }
                else
                {
                    control.BackColor = theme.BackgroundSecondary;
                    control.ForeColor = theme.TextPrimary;
                }
            }

            foreach (Control child in control.Controls)
            {
                ApplyLegacyControls(child, theme);
            }
        }
    }
}
