using System.Diagnostics;

namespace RVIP_Lab5
{
    public partial class Form1 : Form
    {
        public Controller service;
        public Alg1 Alg1;
        public Alg2 Alg2;
        public Stopwatch stopwatch;
        public int[,] matrixA;
        public int[,] matrixB;

        public Form1()
        {
            InitializeComponent();
            this.service = new Controller();
            this.Alg1 = new Alg1();
            this.Alg2 = new Alg2();
            this.stopwatch = new Stopwatch();
        }

        private void buttonAlg1_Click(object sender, EventArgs e)
        {
            try
            {
                stopwatch.Start();
                int[,] matrixResult = Alg1.Begin(matrixA, matrixB);
                stopwatch.Stop();

                labelResultTime.Text = "" + stopwatch.Elapsed;
                matrixResIsFull.Checked = true;
                if (checkBoxIsTyping.Checked)
                {
                    textBoxResult.Text = service.PrintResultMatrix(matrixResult);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            stopwatch.Reset();
        }

        private void buttonAlg2_Click(object sender, EventArgs e)
        {
            try
            {
                stopwatch.Start();
                int[,] matrixResult = Alg2.Begin(matrixA, matrixB, (int)countStream.Value);
                stopwatch.Stop();

                labelResultTime.Text = "" + stopwatch.Elapsed;
                matrixResIsFull.Checked = true;
                if (checkBoxIsTyping.Checked)
                {
                    textBoxResult.Text = service.PrintResultMatrix(matrixResult);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            stopwatch.Reset();
        }

        private void buttonLoadMatrix1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string filePath = openFileDialog1.FileName;
            string result = service.GetTextFromFile(filePath);
            matrixResIsFull.Checked = true;
            if (checkBoxIsTyping.Checked)
            {
                textBoxMatrix1.Text = result;
            }            
            matrixA = service.GetMatrixFromTextbox(result);
            matrix1IsFull.Checked = true;
        }

        private void buttonLoadMatrix2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string filePath = openFileDialog1.FileName;
            string result = service.GetTextFromFile(filePath);
            if (checkBoxIsTyping.Checked)
            {
                textBoxMatrix2.Text = result;
            }
            matrixB = service.GetMatrixFromTextbox(result);
            matrix2IsFull.Checked = true;
        }

        private void buttonGenerateMatrix1_Click(object sender, EventArgs e)
        {
            matrixA = service.GenerateNewMatrix((int)genCountRowCol.Value);
            matrix1IsFull.Checked = true;
            if (checkBoxIsTyping.Checked)
            {
                textBoxMatrix1.Text = service.PrintResultMatrix(matrixA);
            }            
        }

        private void buttonGenerateMatrix2_Click(object sender, EventArgs e)
        {
            matrixB = service.GenerateNewMatrix((int)genCountRowCol.Value);
            matrix2IsFull.Checked = true;
            if (checkBoxIsTyping.Checked)
            {
                textBoxMatrix2.Text = service.PrintResultMatrix(matrixB);
            }            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBoxMatrix1.Text = "";
            textBoxMatrix2.Text = "";
            textBoxResult.Text = "";
            matrixA = null;
            matrixB = null;

            matrix1IsFull.Checked = false;
            matrix2IsFull.Checked = false;
            matrixResIsFull.Checked = false;
        }
    }
}