using FizzyLauncher.Models;
using RyzStudio.Windows.ThemedForms;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

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

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


        private System.Windows.Forms.Label label1;
        private TButton button1;
        private RyzStudio.Windows.Forms.THorizontalSeparator horizontalSeparator2;
        private TTextBox textBox1;
        private TListBox listBox1;
        private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator1;
        private System.Windows.Forms.Label label2;


        public enum DialogModeType
        {
            Add = 0,
            Edit
        }


        protected DialogModeType dialogMode = DialogModeType.Add;


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

            this.DialogMode = dialogMode;
        }

        private void InitializeComponent()
        {
            this.textBox1 = new RyzStudio.Windows.ThemedForms.TTextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new RyzStudio.Windows.ThemedForms.TButton();
            this.horizontalSeparator2 = new RyzStudio.Windows.Forms.THorizontalSeparator();
            this.listBox1 = new RyzStudio.Windows.ThemedForms.TListBox();
            this.label2 = new System.Windows.Forms.Label();
            this.tHorizontalSeparator1 = 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("Microsoft Sans Serif", 8.25F, 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.SubmitButton = null;
            this.textBox1.TabIndex = 152;
            this.textBox1.UseSystemPasswordChar = false;
            // 
            // 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;
            // 
            // 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.DefaultImage = null;
            this.button1.DownImage = null;
            this.button1.IsSelected = false;
            this.button1.LabelText = "&OK";
            this.button1.Location = new System.Drawing.Point(241, 469);
            this.button1.Margin = new System.Windows.Forms.Padding(10);
            this.button1.Name = "button1";
            this.button1.OverImage = 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);
            // 
            // horizontalSeparator2
            // 
            this.horizontalSeparator2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | 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, 61);
            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;
            // 
            // listBox1
            // 
            this.listBox1.AllowDrop = true;
            this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.listBox1.BackColor = System.Drawing.Color.Transparent;
            this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.listBox1.Location = new System.Drawing.Point(109, 87);
            this.listBox1.Margin = new System.Windows.Forms.Padding(10, 4, 10, 4);
            this.listBox1.Name = "listBox1";
            this.listBox1.Padding = new System.Windows.Forms.Padding(10, 6, 7, 5);
            this.listBox1.Size = new System.Drawing.Size(260, 346);
            this.listBox1.SubmitButton = null;
            this.listBox1.TabIndex = 180;
            this.listBox1.OnAdd += new System.EventHandler(this.listBox1_OnAdd);
            this.listBox1.OnEdit += new System.EventHandler(this.listBox1_OnEdit);
            this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
            this.listBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox1_DragOver);
            // 
            // 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(8, 87);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label2.Size = new System.Drawing.Size(25, 34);
            this.label2.TabIndex = 181;
            this.label2.Text = "List";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // tHorizontalSeparator1
            // 
            this.tHorizontalSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tHorizontalSeparator1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator1.BackColor = System.Drawing.Color.Transparent;
            this.tHorizontalSeparator1.Location = new System.Drawing.Point(10, 437);
            this.tHorizontalSeparator1.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.tHorizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2);
            this.tHorizontalSeparator1.MinimumSize = new System.Drawing.Size(0, 22);
            this.tHorizontalSeparator1.Name = "tHorizontalSeparator1";
            this.tHorizontalSeparator1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
            this.tHorizontalSeparator1.Size = new System.Drawing.Size(364, 22);
            this.tHorizontalSeparator1.TabIndex = 182;
            // 
            // AddTileFolderForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.ClientSize = new System.Drawing.Size(384, 521);
            this.Controls.Add(this.tHorizontalSeparator1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.horizontalSeparator2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.MinimumSize = new System.Drawing.Size(400, 560);
            this.Name = "AddTileFolderForm";
            this.Text = "Add List Tile";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

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

            if (this.DialogMode == DialogModeType.Edit)
            {
                listBox1.ListBox.Items.Clear();

                if (this.TilePanel == null) return;
                if (this.TilePanel.ModelInfo == null) return;

                textBox1.Text = this.TilePanel.ModelInfo.Title;

                if (this.TilePanel.ModelInfo.Items != null)
                {
                    foreach (TileModel item in this.TilePanel.ModelInfo.Items)
                    {
                        if (item.IsGroup)
                        {
                            continue;
                        }

                        listBox1.ListBox.Items.Add(item);
                    }
                }
            }
        }


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

        public TilePanel TilePanel { 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 List";
                        break;
                    case DialogModeType.Edit:
                        this.Text = "Edit Tile List";
                        break;
                    default: break;
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            TileModel model = new TileModel()
            {
                Title = textBox1.Text?.Trim(),
                IsGroup = true
            };

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

                    model.Items = new System.Collections.Generic.List<TileModel>();

                    foreach (TileModel item in listBox1.ListBox.Items.OfType<TileModel>())
                    {
                        if (item.IsGroup)
                        {
                            continue;
                        }

                        model.Items.Add(item);
                    }

                    this.TilePanelLayout.AddTile(model);

                    break;
                case DialogModeType.Edit:
                    model.Items = new System.Collections.Generic.List<TileModel>();

                    foreach (TileModel item in listBox1.ListBox.Items.OfType<TileModel>())
                    {
                        if (item.IsGroup)
                        {
                            continue;
                        }

                        model.Items.Add(item);
                    }

                    this.TilePanel.LoadInfo(model);

                    break;
                default: break;
            }

            this.Close();
        }

        private void listBox1_OnAdd(object sender, EventArgs e) => EditTileForm.ShowAddDialog(listBox1);

        private void listBox1_OnEdit(object sender, EventArgs e)
        {
            if (listBox1.ListBox.Items.Count <= 0)
            {
                return;
            }

            EditTileForm.ShowEditDialog(listBox1);
        }

        private void listBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (fileList == null)
            {
                return;
            }

            if (fileList.Length <= 0)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(fileList[0]))
            {
                return;
            }

            TileModel model = new TileModel()
            {
                ProcessFilename = fileList[0],
                Title = Path.GetFileName(fileList[0])
            };

            // exe
            if (Path.GetExtension(fileList[0]).Equals(".exe", StringComparison.CurrentCultureIgnoreCase))
            {
                if (File.Exists(fileList[0]))
                {
                    try
                    {
                        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileList[0]);
                        if (fvi != null)
                        {
                            model.Title = fvi.ProductName;
                        }
                    }
                    catch
                    {
                        // do nothing
                    }
                }


                if (string.IsNullOrWhiteSpace(model.Title))
                {
                    model.Title = Path.GetFileNameWithoutExtension(fileList[0]);
                }
            }

            listBox1.ListBox.Items.Add(model);
        }

    }
}