using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace PuzzlePlayer_Namespace
{
    // form to display the settings in
    internal class SettingForm : Form
    {
        // Public variables for all settings
        // Currently they are set to their default values
        public static Color primaryColor = Color.FromArgb(66, 69, 73);
        public static Color secondaryColor = Color.FromArgb(54, 57, 62);
        public static Color tertiaryColor = Color.FromArgb(0, 0, 0);
        public static int fontSize = 16;
        public static string mainFontName = "Gotham";
        public static Font mainFont = new Font(mainFontName, fontSize);
        public static FlowLayoutPanel settingsPanel;
        
        // hardcoded default settings
        public static UserData defaultSettings = new UserData("Dark", mainFontName, fontSize);

        // For storing user settings
        private UserData settings;

        public SettingForm()
        {
            // TODO OF ZOIETS HELP
            SetUpUI();

            this.BackColor = primaryColor;

            foreach (Control control in Controls)
            {
                UpdateControl(control);
            }
            Invalidate();
        }

        private void SetUpUI()
        {
            this.ClientSize = new Size(1115, 755);
            this.BackColor = SettingForm.primaryColor;
            this.Text = "PuzzlePlayer";

            MenuStrip menuStrip = new MenuStrip
            {
                BackColor = secondaryColor,
                ForeColor = secondaryColor,
                Name = "Main menu",
                Text = "Main Menu",
                Dock = DockStyle.Top
            };

            ToolStripMenuItem menuBack = new ToolStripMenuItem
            {
                BackColor = secondaryColor,
                ForeColor = Color.Black,
                Text = "Back",
                TextAlign = ContentAlignment.BottomRight
            };

            menuBack.Click += (object o, EventArgs e) =>
            {
                this.Close();
            };

            //
            ComboBox themeSelector = new ComboBox
            {
                Text = "Theme",
                Name = "Theme",
                Height = 25,
                Width = 250,
                BackColor = SettingForm.secondaryColor,
                ForeColor = Color.Black,
                Margin = new Padding(20)
            };
            themeSelector.Items.Add("Dark");
            themeSelector.Items.Add("Light");
            themeSelector.Items.Add("Pink");

            themeSelector.SelectedIndexChanged += (object o, EventArgs e) =>
            {
                switch (themeSelector.Text)
                {
                    case "Dark":
                        SettingForm.primaryColor = Color.FromArgb(66, 69, 73);
                        SettingForm.secondaryColor = Color.FromArgb(54, 57, 62);
                        SettingForm.tertiaryColor = Color.FromArgb(0, 0, 0);
                        break;
                    case "Light":
                        SettingForm.primaryColor = Color.FromArgb(228, 229, 241);
                        SettingForm.secondaryColor = Color.FromArgb(210, 211, 219);
                        SettingForm.tertiaryColor = Color.FromArgb(72, 75, 106);
                        break;
                    case "Pink":
                        SettingForm.primaryColor = Color.FromArgb(255, 209, 220);
                        SettingForm.secondaryColor = Color.FromArgb(252, 178, 197);
                        SettingForm.tertiaryColor = Color.FromArgb(251, 199, 255);
                        break;
                }
            };

            ComboBox sizeSelector = new ComboBox
            {
                Text = "Size",
                Name = "Size",
                Height = 25,
                Width = 250,
                BackColor = SettingForm.secondaryColor,
                ForeColor = Color.Black,
                Margin = new Padding(20),
            };
            sizeSelector.Items.Add("Small");
            sizeSelector.Items.Add("Medium");
            sizeSelector.Items.Add("Large");

            sizeSelector.SelectedIndexChanged += (object o, EventArgs e) =>
            {
                switch (sizeSelector.Text)
                {
                    case "Small":
                        fontSize = 8;
                        break;
                    case "Medium":
                        fontSize = 16;
                        break;
                    case "Large":
                        fontSize = 24;
                        break;
                }
                mainFont = new Font("Gotham", fontSize);
                settingsPanel.PerformLayout();
            };

            settingsPanel = new FlowLayoutPanel
            {
                Location = new Point(0, menuStrip.Height),
                Size = new Size(ClientSize.Width, ClientSize.Height - menuStrip.Height - 100),
                Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
                FlowDirection = FlowDirection.TopDown,
                WrapContents = true,
                AutoScroll = true,
                Padding = new Padding(20),
            };

            //
            RoundedButton saveButton = new RoundedButton
            {
                Text = "Save",
                Name = "Save",
                Height = 100,
                Width = 350,
                BackColor = SettingForm.secondaryColor,
                ForeColor = Color.Black,
                Location = new Point(10, ClientSize.Height - 110),
                Anchor = AnchorStyles.Left | AnchorStyles.Bottom,
                FlatStyle = FlatStyle.Flat,
                Margin = new Padding(10)
            };

            saveButton.Click += (object o, EventArgs e) =>
            {
                this.BackColor = primaryColor;

                foreach (Control control in Controls)
                {
                    UpdateControl(control);
                }

                settingsPanel.BackColor = primaryColor;
                Invalidate();
            };

            //
            this.Controls.Add(menuStrip);
            this.Controls.Add(settingsPanel);
            this.Controls.Add(saveButton);

            settingsPanel.Controls.Add(themeSelector);
            settingsPanel.Controls.Add(sizeSelector);

            menuStrip.Items.Add(menuBack);
            
            menuStrip.BringToFront();
            themeSelector.BringToFront();
            saveButton.BringToFront();
        }

        public static void UpdateControl(Control control)
        {
            if (control is not FlowLayoutPanel)
            {
                control.BackColor = secondaryColor;
                control.ForeColor = tertiaryColor;
            }
            else
            {
                control.BackColor = primaryColor;
            }

            control.Font = mainFont;

            foreach (Control childControl in control.Controls)
            {
                UpdateControl(childControl);
            }

            if (control is MenuStrip menuStrip)
            {
                foreach (ToolStripItem item in menuStrip.Items)
                {
                    UpdateToolStripSettings(item);
                }
            }
        }

        public static void UpdateToolStripSettings(ToolStripItem item)
        {
            item.BackColor = secondaryColor;
            item.ForeColor = tertiaryColor;
            item.Font = mainFont;

            if (item is ToolStripMenuItem menuItem)
            {
                foreach (ToolStripItem subItem in menuItem.DropDownItems)
                {
                    UpdateToolStripSettings(subItem);
                }
            }
        }

        public static Image GetEmbeddedImage(string imageWithExtention)
        {
            string imagePath = "PuzzlePlayer_Namespace.Resources." + imageWithExtention;
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(imagePath);
            Image img = Image.FromStream(stream);
            stream.Dispose();
            return img;
        }
    }
}