using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using FizzyLauncher.Models;
using FizzyLauncher.Windows.Forms;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using RyzStudio.Windows.ThemedForms.ButtonTextBox;
using static RyzStudio.Windows.ThemedForms.ButtonTextBox.ThKeyCodeTextBox;

namespace FizzyLauncher
{
    public class EditTileFolderForm : TDialog
    {
        private Label label1;
        private ThButton button1;
        private THorizontalSeparator horizontalSeparator2;
        private ThClearableTextBox textBox1;
        private ThListBox listBox1;
        private THorizontalSeparator tHorizontalSeparator1;
        private Label label2;

        private TileModel result = null;


        public EditTileFolderForm(TileModel model = null)
        {
            InitializeComponent();

            StartPosition = FormStartPosition.WindowsDefaultLocation;
            OkButton = button1;

            result = model;

            if (result != null)
            {
                Text = "Edit Folder";

                textBox1.Text = result?.Title ?? string.Empty;

                foreach (TileModel item in result.Items ?? new List<TileModel>())
                {
                    if (item.IsGroup)
                    {
                        continue;
                    }

                    listBox1.ListBox.Items.Add(item);
                }
            }
            else
            {
                Text = "Add Folder";
            }
        }

        private void InitializeComponent()
        {
            textBox1 = new ThClearableTextBox();
            label1 = new Label();
            button1 = new ThButton();
            horizontalSeparator2 = new THorizontalSeparator();
            listBox1 = new ThListBox();
            label2 = new Label();
            tHorizontalSeparator1 = new THorizontalSeparator();
            SuspendLayout();
            //
            // textBox1
            //
            textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            textBox1.BackColor = Color.Transparent;
            textBox1.EnableReactiveVisual = true;
            textBox1.Font = new Font("Microsoft Sans Serif", 8.25F);
            textBox1.Location = new Point(109, 20);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(260, 35);
            textBox1.TabIndex = 152;
            textBox1.TabStop = false;
            textBox1.UseSystemPasswordChar = false;
            //
            // label1
            //
            label1.AutoSize = true;
            label1.BackColor = Color.Transparent;
            label1.ForeColor = SystemColors.ControlText;
            label1.Location = new Point(10, 21);
            label1.Margin = new Padding(0);
            label1.Name = "label1";
            label1.Padding = new Padding(0, 9, 0, 10);
            label1.Size = new Size(29, 34);
            label1.TabIndex = 153;
            label1.Text = "Title";
            label1.TextAlign = ContentAlignment.MiddleLeft;
            //
            // button1
            //
            button1.AcceptButton = null;
            button1.ActiveImage = null;
            button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            button1.BackColor = Color.Transparent;
            button1.EnableMenuOnClick = false;
            button1.EnableReactiveVisual = true;
            button1.HoverImage = null;
            button1.IdleImage = null;
            button1.LabelText = "&Save";
            button1.Location = new Point(241, 469);
            button1.Margin = new Padding(10, 10, 10, 0);
            button1.Name = "button1";
            button1.Padding = new Padding(4, 4, 3, 3);
            button1.Size = new Size(128, 32);
            button1.TabIndex = 173;
            button1.TabStop = false;
            //
            // horizontalSeparator2
            //
            horizontalSeparator2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            horizontalSeparator2.AutoScrollMargin = new Size(0, 0);
            horizontalSeparator2.AutoScrollMinSize = new Size(0, 0);
            horizontalSeparator2.BackColor = Color.Transparent;
            horizontalSeparator2.Location = new Point(10, 61);
            horizontalSeparator2.Margin = new Padding(0, 10, 0, 0);
            horizontalSeparator2.MaximumSize = new Size(4920, 2);
            horizontalSeparator2.MinimumSize = new Size(0, 22);
            horizontalSeparator2.Name = "horizontalSeparator2";
            horizontalSeparator2.Size = new Size(364, 22);
            horizontalSeparator2.TabIndex = 177;
            horizontalSeparator2.TabStop = false;
            //
            // listBox1
            //
            listBox1.AcceptButton = null;
            listBox1.AllowDrop = true;
            listBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            listBox1.BackColor = Color.Transparent;
            listBox1.EnableReactiveVisual = true;
            listBox1.Font = new Font("Microsoft Sans Serif", 8.25F);
            listBox1.Location = new Point(109, 87);
            listBox1.Margin = new Padding(10, 10, 10, 0);
            listBox1.Name = "listBox1";
            listBox1.Padding = new Padding(4, 4, 3, 3);
            listBox1.Size = new Size(260, 346);
            listBox1.TabIndex = 180;
            listBox1.OnAdd += listBox1_OnAdd;
            listBox1.OnEdit += listBox1_OnEdit;
            listBox1.DragDrop += listBox1_DragDrop;
            listBox1.DragOver += listBox1_DragOver;
            //
            // label2
            //
            label2.AutoSize = true;
            label2.BackColor = Color.Transparent;
            label2.ForeColor = SystemColors.ControlText;
            label2.Location = new Point(8, 87);
            label2.Name = "label2";
            label2.Padding = new Padding(0, 9, 0, 10);
            label2.Size = new Size(30, 34);
            label2.TabIndex = 181;
            label2.Text = "Tiles";
            label2.TextAlign = ContentAlignment.MiddleLeft;
            //
            // tHorizontalSeparator1
            //
            tHorizontalSeparator1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            tHorizontalSeparator1.AutoScrollMargin = new Size(0, 0);
            tHorizontalSeparator1.AutoScrollMinSize = new Size(0, 0);
            tHorizontalSeparator1.BackColor = Color.Transparent;
            tHorizontalSeparator1.Location = new Point(10, 437);
            tHorizontalSeparator1.Margin = new Padding(0, 10, 0, 0);
            tHorizontalSeparator1.MaximumSize = new Size(4920, 2);
            tHorizontalSeparator1.MinimumSize = new Size(0, 22);
            tHorizontalSeparator1.Name = "tHorizontalSeparator1";
            tHorizontalSeparator1.Size = new Size(364, 22);
            tHorizontalSeparator1.TabIndex = 182;
            tHorizontalSeparator1.TabStop = false;
            //
            // EditTileFolderForm
            //
            ClientSize = new Size(384, 521);
            Controls.Add(tHorizontalSeparator1);
            Controls.Add(label2);
            Controls.Add(listBox1);
            Controls.Add(horizontalSeparator2);
            Controls.Add(button1);
            Controls.Add(label1);
            Controls.Add(textBox1);
            MinimumSize = new Size(400, 560);
            Name = "EditTileFolderForm";
            Text = "Add List Tile";
            ResumeLayout(false);
            PerformLayout();
        }


        public TileModel Result
        {
            get
            {
                if (result == null)
                {
                    result = new TileModel();
                }

                result.Title = textBox1.Text?.Trim() ?? string.Empty;
                result.IsGroup = true;
                result.Items = new List<TileModel>();

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

                    result.Items.Add(item);
                }

                return result;
            }
        }


        private void listBox1_OnAdd(object sender, EventArgs e)
        {
            var form = new EditTileForm();
            if (form.ShowDialog() == DialogResult.OK)
            {
                listBox1.ListBox.Items.Add(form.Result);
            };
        }

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

            var n = listBox1.ListBox.SelectedIndex;
            if (n < 0)
            {
                return;
            }

            if (listBox1.ListBox.SelectedItem == null)
            {
                return;
            }

            var form = new EditTileForm((TileModel)listBox1.ListBox.SelectedItem);
            if (form.ShowDialog() == DialogResult.OK)
            {
                listBox1.ListBox.Items.RemoveAt(n);
                listBox1.ListBox.Items.Insert(n, form.Result);
            };
        }

        private void listBox1_DragOver(object sender, 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)
        {
            var fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (fileList == null)
            {
                return;
            }

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

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

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

    }
}