using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace final_project
{
    public partial class AlgorithmDone : Form
    {
        /*************************************DATA MEMBERS*******************************************/
        //NAME: filePath
        //TYPE: string
        //PURPOSE: 'filePath' stores the file path of the user selecte target image
        private string filePath;
        public string FilePath
        { set { filePath = value; } get { return filePath; } }
        //NAME: individuals
        //TYPE: int
        //PURPOSE: 'individuals' stores the number of individuals the algorithm will run on
        private int individuals;
        public int Individuals
        { set { individuals = value; } get { return individuals; } }
        //NAME: polygons
        //TYPE: int
        //PURPOSE: 'polygons' stores the number of polygons per individual
        private int polygons;
        public int Polygons
        { set { polygons = value; } get { return polygons; } }
        //NAME: best
        //TYPE: int
        //PURPOSE: 'best' stores the number of individuals to advance from the current generation to the next without any changes to it
        private int best;
        public int Best
        { set { best = value; } get { return best; } }
        //NAME: mutants
        //TYPE: int
        //PURPOSE: 'mutants' stores the number of individuals to advance from the current generation to the next with changes to points and color
        private int mutants;
        public int Mutants
        { set { mutants = value; } get { return mutants; } }
        //NAME: crossovers
        //TYPE: int
        //PURPOSE: 'crossovers' store the number of individuals to create via crossover operation with parents from the current generation for the next 
        private int crossovers;
        public int Crossovers
        { set { crossovers = value; } get { return crossovers; } }
        //NAME: generationCount
        //TYPE: int
        //PURPOSE: 'generationCount' is a counter, tracking the number of generations passed
        private int generationCount;
        public int GenerationCount
        { set { generationCount = value; } get { return generationCount; } }
        //NAME: elapsedTime
        //TYPE: TimeSpan
        //PURPOSE: 'elapsedTime' is used to compute the time from 'currentTime' to 'startTime'
        private TimeSpan elapsedTime;
        public TimeSpan ElapsedTime
        { set { elapsedTime = value; } get { return elapsedTime; } }
        //NAME: lastBestScore
        //TYPE: int
        //PURPOSE: 'lastBestScore' stores the score of the final individual (the best one!)
        private int lastBestScore;
        public int LastBestScore
        { set { lastBestScore = value; } get { return lastBestScore; } }
        //NAME: lastBestImage
        //TYPE: Bitmap
        //PURPOSE: 'lastBestImage' stored the image of the final individual (the best one!)
        private Bitmap lastBestImage;
        public Bitmap LastBestImage
        { set { lastBestImage = value; } get { return lastBestImage; } }
        //NAME: closed
        //TYPE: bool
        //PURPOSE: bool flag to indicate the form as been closed
        private bool closedForm;
        public bool ClosedForm
        { set { closedForm = value; } get { return closedForm; } }
        /**************************************************************************************************/
        public AlgorithmDone()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        //EVENT HANDLER: LoadDone, click
        //PURPOSE: This event handler loads data onto the form, and prepared to close all forms
        private void LoadDone(object sender, EventArgs e)
        {
            InitializeForm();
        }
        //EVENT HANDLER: exportClick, click
        //PURPOSE: The 'exportClick' event handler allows the user to save the last best image of the algorithm
        private void exportClick(object sender, EventArgs e)
        {
            SaveFileDialog exportFile = new SaveFileDialog();

            exportFile.Filter = "Png Image|*.png";
            exportFile.Title = "Save best image";

            if (exportFile.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("File saved successfully. Close the program");

                if(exportFile.FileName != "")
                {
                    string path = exportFile.FileName + this.generationCount.ToString() + "_" + this.LastBestScore.ToString();
                    this.LastBestImage.Save(exportFile.FileName, ImageFormat.Png);
                }
                Console.WriteLine("saved to file. done");
            }
        }
        //EVENT HANDLER: closeClick, click
        //PURPOSE: The 'closeClick'  handler closes the form when clicked
        private void closeClick(object sender, EventArgs e)
        {
            this.ClosedForm = true;
            this.Close();
        }
        /****************************************FUNCTIONS***************************************/
        //FUNCTION: InitializeForm
        //PARAMETERS: Image , number of individuals, triangles, best, mutants, and crossovers
        //PURPOSE: 'InitializeForm' will fill in all of the textboxes on the form with the corresponding information
        private void InitializeForm()
        {
            pbTarget.Image = Image.FromFile(this.FilePath);             //set information on the form
            pbBest.Image = this.LastBestImage;

            tbxIndividuals.Text = this.Individuals.ToString();
            tbxPolygons.Text = this.Polygons.ToString();
            tbxBest.Text = this.Best.ToString();
            tbxMutants.Text = this.Mutants.ToString();
            tbxCrossovers.Text = this.Crossovers.ToString();

            tbxTotalGens.Text = this.GenerationCount.ToString();
            tbxLastBest.Text = this.LastBestScore.ToString();
            tbxTotalTime.Text = this.ElapsedTime.ToString();
        }
    }
}
