using laba3.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgLab1.GUI
{
    public partial class ArraySortGUI : Form
    {
        private long time1;
        private long time2;
        private Label lblArraySize;
        private TextBox txtArraySize;
        private Button btnRun;
        private TextBox txtOutput;

        public ArraySortGUI(Form onCloseForm)
        {
            // Initialize components
            lblArraySize = new Label
            {
                Location = new System.Drawing.Point(12, 12),
                Name = "lblArraySize",
                Size = new System.Drawing.Size(100, 22),
                Text = "Размер массива:"
            };

            txtArraySize = new TextBox
            {
                Location = new System.Drawing.Point(150, 12),
                Name = "txtArraySize",
                Size = new System.Drawing.Size(150, 22),
                PlaceholderText = "Размер массива"
            };

            btnRun = new Button
            {
                Location = new System.Drawing.Point(310, 10),
                Name = "btnRun",
                Size = new System.Drawing.Size(100, 25),
                Text = "Запустить",
                UseVisualStyleBackColor = true
            };
            btnRun.Click += new EventHandler(this.btnRun_Click);

            txtOutput = new TextBox
            {
                Location = new System.Drawing.Point(12, 50),
                Multiline = true,
                Name = "txtOutput",
                ReadOnly = true,
                ScrollBars = ScrollBars.Vertical,
                Size = new System.Drawing.Size(400, 300),
                WordWrap = true
            };

            // Set up form
            this.ClientSize = new System.Drawing.Size(424, 361);
            this.Controls.Add(lblArraySize);
            this.Controls.Add(txtArraySize);
            this.Controls.Add(btnRun);
            this.Controls.Add(txtOutput);
            this.Name = "ArraySortGUI";
            this.Text = "Array Sort Demo";
            this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => {onCloseForm.Show(); });
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            int size;
            if (!int.TryParse(txtArraySize.Text, out size) || size < 1)
            {
                MessageBox.Show("Размер массива должен быть числом больше 0!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            txtOutput.Clear();
            Utils.Arrays array1 = new Utils.Arrays(size, 100);
            Utils.Arrays array2 = new Utils.Arrays(array1);

            txtOutput.AppendText("Для Массива 1 будет использована \"гномья сортировка\"" + Environment.NewLine);
            txtOutput.AppendText("Для Массива 2 будет использована \"сортировка шелла\"" + Environment.NewLine);

            if (size <= 10)
            {
                txtOutput.AppendText("Массив 1 (с начальными значениями): " + Environment.NewLine + array1.ToString() + Environment.NewLine);
                txtOutput.AppendText("Массив 2 (с начальными значениями): " + Environment.NewLine + array2.ToString() + Environment.NewLine);
            }
            else
            {
                txtOutput.AppendText("Массивы не могут быть выведены на экран так как их размер больше 10" + Environment.NewLine);
            }

            txtOutput.AppendText("Сортируем..." + Environment.NewLine);

            time1 = array1.gnomeSort();
            time2 = array2.shellSort();

            txtOutput.AppendText("Готово!" + Environment.NewLine);

            if (size <= 10)
            {
                txtOutput.AppendText("Массив 1 (после гномьей сортировки): " + Environment.NewLine + array1.ToString() + Environment.NewLine);
                txtOutput.AppendText("Массив 2 (после сортировки шелла): " + Environment.NewLine + array2.ToString() + Environment.NewLine);
            }

            txtOutput.AppendText($"Для сортировки 1 потребовалось {time1}мс" + Environment.NewLine);
            txtOutput.AppendText($"Для сортировки 2 потребовалось {time2}мс" + Environment.NewLine);
        }
    }
}