using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace EgtBEAMWALL.StressTest
{
    public partial class StressTest : Form
    {
        #region Public Constructors

        public StressTest()
        {
            InitializeComponent();
            // init cablato sul mio DB
            DataLayer.DbConfig.InitDb("127.0.0.1", "000470", "36311", "000470");
            //// warm up DB
            //CurrSim.initDb();
        }

        #endregion Public Constructors

        #region Protected Fields

        /// <summary>
        /// Controller dati simulati x provare procedure
        /// </summary>
        protected Simulator CurrSim = new Simulator();

        #endregion Protected Fields

        #region Protected Properties

        protected int numProj
        {
            get
            {
                int answ = 1;
                int.TryParse(txtNumProj.Text, out answ);
                return answ;
            }
            set
            {
                txtNumProj.Text = $"{value}";
            }
        }

        #endregion Protected Properties

        #region Private Methods

        private void btnDbDump_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Sicuro di voler generare il dump del DB corrente?", "DB Dump", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (result == DialogResult.Yes)
            {
                labelResult.Text = "...";
                Stopwatch sw = new Stopwatch();
                sw.Start();
                //string binPath = "C:\\Program Files\\MariaDB 10.5\\bin";
                //string myDumpPath = Path.Combine(binPath, "mysqldump.exe");
                string outPath = txtDumpFile.Text.Trim();
                string dbName = txtDbName.Text.Trim();
                if (!string.IsNullOrEmpty(outPath))
                {
                    // effettua dump
                    //DataLayer.DbConfig.DataBaseDumpToFile(outPath,  "mysqldump");
                    //DataLayer.DbConfig.DataBaseDumpToFile(outPath, "mysqldump", "--skip-extended-insert");
                    DataLayer.DbConfig.DataBaseDumpToFile(outPath, dbName);
                    sw.Stop();
                    var elapsed = sw.Elapsed;
                    labelResult.Text = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} | Dump Done in {elapsed.TotalSeconds:N3} sec!";
                }
            }
        }

        private void btnDbRestore_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Sicuro di voler ripristinare il file dump sul DB corrente?", "DB Restore", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (result == DialogResult.Yes)
            {
                labelResult.Text = "...";
                string inPath = txtDumpFile.Text.Trim();
                string dbName = txtDbName.Text.Trim();
                string tempFolder = Path.Combine(Path.GetDirectoryName(inPath), "DbRestore");
                Stopwatch sw = new Stopwatch();
                sw.Start();
                // effettua restore
                DataLayer.DbConfig.DataBaseRestoreFromFile(inPath, dbName, tempFolder); sw.Stop();
                //string binPath = "C:\\Program Files\\MariaDB 10.5\\bin";
                //string mysqlPath = Path.Combine(binPath, "mysql.exe");
                //DataLayer.DbConfig.DataBaseRestoreFromFile(inPath, "mysql");
                var elapsed = sw.Elapsed;
                labelResult.Text = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} | Restore Done in {elapsed.TotalSeconds:N3} sec!";
            }
        }

        private void buttonDataSeed_Click(object sender, EventArgs e)
        {
            // genero dati col SIM
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            CurrSim.GenerateData(numProj);
            sw.Stop();
            labelResult.Text = $"Execution time: {sw.ElapsedMilliseconds}msec";
        }

        private void buttonResetDb_Click(object sender, EventArgs e)
        {
            // resetta DB
            DataLayer.Controllers.DbController.man.ResetDb();
        }

        #endregion Private Methods
    }
}