using FizzyLauncher.Models;
using RyzStudio.Windows.ThemedForms;
using RyzStudio.Windows.ThemedForms.ButtonTextBox;
using System;
using System.Drawing;

namespace FizzyLauncher.Windows.Forms
{
    public class EditTileForm : TDialogForm
    {
        public static void ShowAddDialog(TilePanelLayout control, Point coord)
        {
            EditTileForm form = new EditTileForm(DialogModeType.Add);
            form.TilePanelLayout = control;
            form.AimCoord = coord;
            form.ShowDialog();
        }

        public static void ShowAddDialog(TListBox control)
        {
            EditTileForm form = new EditTileForm(DialogModeType.Add);
            form.ListBox = control;
            form.ShowDialog();
        }

        public static void ShowEditDialog(TilePanel control)
        {
            EditTileForm form = new EditTileForm(DialogModeType.Edit);
            form.TilePanel = control;
            form.ShowDialog();
        }

        public static void ShowEditDialog(TListBox control)
        {
            EditTileForm form = new EditTileForm(DialogModeType.Edit);
            form.ListBox = control;
            form.ShowDialog();
        }


        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private TOpenFileTextBox textBox2;
        private TTextBox textBox3;
        private TFolderTextBox textBox4;
        private TButton button1;
        private TPickerBox pickerBox1;
        private TPickerBox pickerBox2;
        private RyzStudio.Windows.Forms.THorizontalSeparator horizontalSeparator1;
        private RyzStudio.Windows.Forms.THorizontalSeparator horizontalSeparator2;
        private TTextBox textBox1;


        public enum DialogModeType
        {
            Add = 0,
            Edit
        }


        protected DialogModeType dialogMode = DialogModeType.Add;


        public EditTileForm(DialogModeType dialogMode) : base()
        {
            InitializeComponent();

            this.DialogMode = dialogMode;

            pickerBox1.ComboBox.Items.Clear();
            pickerBox1.ComboBox.Items.AddRange(new string[] { "Normal", "Hidden", "Minimized", "Maximized" });
            if (pickerBox1.ComboBox.Items.Count > 0) pickerBox1.ComboBox.SelectedIndex = 0;

            pickerBox2.ComboBox.Items.Clear();
            pickerBox2.ComboBox.Items.AddRange(new string[] { "No", "Yes" });
            if (pickerBox2.ComboBox.Items.Count > 0) pickerBox2.ComboBox.SelectedIndex = 0;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EditTileForm));
            this.textBox1 = new TTextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new TOpenFileTextBox();
            this.textBox3 = new TTextBox();
            this.textBox4 = new TFolderTextBox();
            this.button1 = new TButton();
            this.pickerBox1 = new TPickerBox();
            this.pickerBox2 = new TPickerBox();
            this.horizontalSeparator1 = new RyzStudio.Windows.Forms.THorizontalSeparator();
            this.horizontalSeparator2 = new RyzStudio.Windows.Forms.THorizontalSeparator();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.BackColor = System.Drawing.Color.Transparent;
            this.textBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox1.Location = new System.Drawing.Point(192, 20);
            this.textBox1.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.textBox1.Name = "textBox1";
            this.textBox1.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
            this.textBox1.Size = new System.Drawing.Size(177, 35);
            this.textBox1.AcceptButton = null;
            this.textBox1.TabIndex = 152;
            this.textBox1.UseSystemPasswordChar = false;
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.BackColor = System.Drawing.Color.Transparent;
            this.label6.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label6.Location = new System.Drawing.Point(10, 250);
            this.label6.Name = "label6";
            this.label6.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label6.Size = new System.Drawing.Size(83, 34);
            this.label6.TabIndex = 163;
            this.label6.Text = "Run As Admin";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label7
            //
            this.label7.AutoSize = true;
            this.label7.BackColor = System.Drawing.Color.Transparent;
            this.label7.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label7.Location = new System.Drawing.Point(10, 209);
            this.label7.Name = "label7";
            this.label7.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label7.Size = new System.Drawing.Size(79, 34);
            this.label7.TabIndex = 161;
            this.label7.Text = "Window Style";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.BackColor = System.Drawing.Color.Transparent;
            this.label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label4.Location = new System.Drawing.Point(10, 144);
            this.label4.Name = "label4";
            this.label4.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label4.Size = new System.Drawing.Size(103, 34);
            this.label4.TabIndex = 159;
            this.label4.Text = "Working Directory";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label3.Location = new System.Drawing.Point(10, 103);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label3.Size = new System.Drawing.Size(61, 34);
            this.label3.TabIndex = 157;
            this.label3.Text = "Argument";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label2.Location = new System.Drawing.Point(10, 62);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label2.Size = new System.Drawing.Size(55, 34);
            this.label2.TabIndex = 155;
            this.label2.Text = "Filename";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label1.Location = new System.Drawing.Point(10, 21);
            this.label1.Margin = new System.Windows.Forms.Padding(0);
            this.label1.Name = "label1";
            this.label1.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label1.Size = new System.Drawing.Size(29, 34);
            this.label1.TabIndex = 153;
            this.label1.Text = "Title";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // textBox2
            //
            this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox2.BackColor = System.Drawing.Color.Transparent;
            this.textBox2.FileDialog = null;
            this.textBox2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox2.HighlightImage = ((System.Drawing.Image)(resources.GetObject("textBox2.HighlightImage")));
            this.textBox2.Location = new System.Drawing.Point(192, 61);
            this.textBox2.Margin = new System.Windows.Forms.Padding(10, 3, 3, 3);
            this.textBox2.Name = "textBox2";
            this.textBox2.NormalImage = ((System.Drawing.Image)(resources.GetObject("textBox2.NormalImage")));
            this.textBox2.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
            this.textBox2.Size = new System.Drawing.Size(177, 35);
            this.textBox2.AcceptButton = null;
            this.textBox2.TabIndex = 170;
            this.textBox2.UseSystemPasswordChar = false;
            //
            // textBox3
            //
            this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox3.BackColor = System.Drawing.Color.Transparent;
            this.textBox3.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox3.Location = new System.Drawing.Point(192, 102);
            this.textBox3.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.textBox3.Name = "textBox3";
            this.textBox3.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
            this.textBox3.Size = new System.Drawing.Size(177, 35);
            this.textBox3.AcceptButton = null;
            this.textBox3.TabIndex = 171;
            this.textBox3.UseSystemPasswordChar = false;
            //
            // textBox4
            //
            this.textBox4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox4.BackColor = System.Drawing.Color.Transparent;
            this.textBox4.FolderDialog = null;
            this.textBox4.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox4.HighlightImage = ((System.Drawing.Image)(resources.GetObject("textBox4.HighlightImage")));
            this.textBox4.Location = new System.Drawing.Point(192, 143);
            this.textBox4.Margin = new System.Windows.Forms.Padding(10, 3, 3, 3);
            this.textBox4.Name = "textBox4";
            this.textBox4.NormalImage = ((System.Drawing.Image)(resources.GetObject("textBox4.NormalImage")));
            this.textBox4.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
            this.textBox4.Size = new System.Drawing.Size(177, 35);
            this.textBox4.AcceptButton = null;
            this.textBox4.TabIndex = 172;
            this.textBox4.UseSystemPasswordChar = false;
            //
            // button1
            //
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.BackColor = System.Drawing.Color.Transparent;
            this.button1.IdleImage = null;
            this.button1.ActiveImage = null;
            this.button1.LabelText = "&Save";
            this.button1.Location = new System.Drawing.Point(241, 469);
            this.button1.Margin = new System.Windows.Forms.Padding(10);
            this.button1.Name = "button1";
            this.button1.HoverImage = null;
            this.button1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.button1.Size = new System.Drawing.Size(128, 32);
            this.button1.TabIndex = 173;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // pickerBox1
            //
            this.pickerBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pickerBox1.BackColor = System.Drawing.Color.Transparent;
            this.pickerBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.pickerBox1.Location = new System.Drawing.Point(241, 209);
            this.pickerBox1.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.pickerBox1.Name = "pickerBox1";
            this.pickerBox1.Padding = new System.Windows.Forms.Padding(10, 6, 7, 5);
            this.pickerBox1.Size = new System.Drawing.Size(128, 34);
            this.pickerBox1.AcceptButton = null;
            this.pickerBox1.TabIndex = 174;
            //
            // pickerBox2
            //
            this.pickerBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pickerBox2.BackColor = System.Drawing.Color.Transparent;
            this.pickerBox2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.pickerBox2.Location = new System.Drawing.Point(285, 250);
            this.pickerBox2.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.pickerBox2.Name = "pickerBox2";
            this.pickerBox2.Padding = new System.Windows.Forms.Padding(10, 6, 7, 5);
            this.pickerBox2.Size = new System.Drawing.Size(84, 34);
            this.pickerBox2.AcceptButton = null;
            this.pickerBox2.TabIndex = 175;
            //
            // horizontalSeparator1
            //
            this.horizontalSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.horizontalSeparator1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.horizontalSeparator1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.horizontalSeparator1.BackColor = System.Drawing.Color.Transparent;
            this.horizontalSeparator1.Location = new System.Drawing.Point(10, 181);
            this.horizontalSeparator1.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.horizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2);
            this.horizontalSeparator1.MinimumSize = new System.Drawing.Size(0, 22);
            this.horizontalSeparator1.Name = "horizontalSeparator1";
            this.horizontalSeparator1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
            this.horizontalSeparator1.Size = new System.Drawing.Size(364, 22);
            this.horizontalSeparator1.TabIndex = 176;
            //
            // horizontalSeparator2
            //
            this.horizontalSeparator2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.horizontalSeparator2.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.horizontalSeparator2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.horizontalSeparator2.BackColor = System.Drawing.Color.Transparent;
            this.horizontalSeparator2.Location = new System.Drawing.Point(10, 437);
            this.horizontalSeparator2.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.horizontalSeparator2.MaximumSize = new System.Drawing.Size(4920, 2);
            this.horizontalSeparator2.MinimumSize = new System.Drawing.Size(0, 22);
            this.horizontalSeparator2.Name = "horizontalSeparator2";
            this.horizontalSeparator2.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
            this.horizontalSeparator2.Size = new System.Drawing.Size(364, 22);
            this.horizontalSeparator2.TabIndex = 177;
            //
            // EditTileForm
            //
            this.ClientSize = new System.Drawing.Size(384, 521);
            this.Controls.Add(this.horizontalSeparator2);
            this.Controls.Add(this.horizontalSeparator1);
            this.Controls.Add(this.pickerBox2);
            this.Controls.Add(this.pickerBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.MinimumSize = new System.Drawing.Size(400, 560);
            this.Name = "EditTileForm";
            this.Text = "Add Tile";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (this.DialogMode == DialogModeType.Edit)
            {
                if (this.TilePanel != null)
                {
                    updateModel(this.TilePanel.ModelInfo);
                }
                else if (this.ListBox != null)
                {
                    if (this.ListBox.ListBox.SelectedItem != null)
                    {
                        TileModel model = (this.ListBox.ListBox.SelectedItem as TileModel);
                        if (model != null)
                        {
                            updateModel(model);
                        }
                    }
                }
            }
        }


        public TilePanelLayout TilePanelLayout { get; set; } = null;

        public TilePanel TilePanel { get; set; } = null;

        public TListBox ListBox { get; set; } = null;

        public Point AimCoord { get; set; } = new Point(-1, -1);

        public DialogModeType DialogMode
        {
            get => dialogMode;
            set
            {
                dialogMode = value;

                switch (dialogMode)
                {
                    case DialogModeType.Add:
                        this.Text = "Add Tile";
                        break;
                    case DialogModeType.Edit:
                        this.Text = "Edit Tile";
                        break;
                    default: break;
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            TileModel model = buildModel();

            switch (this.DialogMode)
            {
                case DialogModeType.Add:
                    if (this.TilePanelLayout != null)
                    {
                        this.TilePanelLayout.AddTile(model);
                    }
                    else if (this.ListBox != null)
                    {
                        int n = this.ListBox.ListBox.Items.Add(model);
                        this.ListBox.ListBox.SelectedIndex = n;
                    }

                    break;
                case DialogModeType.Edit:
                    if (this.TilePanel != null)
                    {
                        this.TilePanel.LoadInfo(model);
                    }
                    else if (this.ListBox != null)
                    {
                        int n = this.ListBox.ListBox.SelectedIndex;
                        if (n >= 0)
                        {
                            this.ListBox.ListBox.Items.RemoveAt(n);
                            this.ListBox.ListBox.Items.Insert(n, model);
                            this.ListBox.ListBox.SelectedIndex = n;
                        }
                    }

                    break;
                default: break;
            }

            this.Close();
        }

        protected TileModel buildModel()
        {
            TileModel rs = new TileModel()
            {
                Title = textBox1.Text?.Trim(),
                ProcessFilename = textBox2.Text?.Trim(),
                ProcessArgument = textBox3.Text?.Trim(),
                ProcessWorkingDirectory = textBox4.Text?.Trim(),
                ProcessWindowStyle = (System.Diagnostics.ProcessWindowStyle)pickerBox1.ComboBox.SelectedIndex,
                ProcessAsAdmin = (pickerBox2.ComboBox.SelectedIndex == 1)
            };

            if (this.DialogMode == DialogModeType.Add)
            {
                if (!this.AimCoord.Equals(new Point(-1, -1)))
                {
                    rs.Position = this.AimCoord;
                }
            }

            return rs;
        }

        protected void updateModel(TileModel model)
        {
            if (model == null)
            {
                return;
            }

            textBox1.Text = model.Title;
            textBox2.Text = model.ProcessFilename;
            textBox3.Text = model.ProcessArgument;
            textBox4.Text = model.ProcessWorkingDirectory;
            pickerBox1.ComboBox.SelectedIndex = (int)model.ProcessWindowStyle;
            pickerBox2.ComboBox.SelectedIndex = (model.ProcessAsAdmin ? 1 : 0);
        }

    }
}
