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 final_project
{
    public partial class SelectParams : Form
    {
        /*************************DATA MEMBERS***************************/
        //NAME: filePath
        //TYPE: string
        //PURPOSE: 'filePath' stores the file path of the user selecte target image
        private string filePath;
        //NAME: tbOperators
        //TYPE: BetterRangeSlider
        //PURPOSE: 'tbOperators' is a custom control, a track bar with two thumb sliders
        private BetterRangeSlider tbOperators;
        //NAME: individuals
        //TYPE: int
        //PURPOSE: 'individuals' stores the number of individuals the algorithm will run on
        private int individuals;
        //NAME: polygons
        //TYPE: int
        //PURPOSE: 'polygons' stores the number of polygons per individual
        private int 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;
        //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;
        //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;
        //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; } }
        /***********************test vars***********************/
        int targetScore;
        public SelectParams()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            tbOperators = new BetterRangeSlider();
            tbOperators.Location = new Point(587, 375);
            tbOperators.Size = new Size(483, 56);
            
            this.Controls.Add(tbOperators);
            tbOperators.Visible = false;
            tbOperators.Enabled = false;

            lblClick.Visible = false;
        }
        //EVENT HANDLER: importClick, when the user is ready to select the target image for the algorithm
        //PURPOSE: The purpose of this event handler is to open File Explorer to allow the user to select the image
        //         of their choice to be the target image for the algorithm.
        private void importClick(object sender, EventArgs e)
        {
            OpenFileDialog importFile = new OpenFileDialog();

            if(importFile.ShowDialog() == DialogResult.OK)
            {
                filePath = importFile.FileName;
                pbTarget.Image = Image.FromFile(filePath);
                //get width and height of the image here
                scoreTarget();
            }
        }
        //EVENT HANDLER: runClick, when the user is ready to run the algorithm with the select parameters
        //PURPOSE: The purpose of this event handler is to allow the user to run the algorithm once they have 
        //         selected their desired parameters. Parameters cannot be changed once 'RUN' is clicked. The event
        //         handler will open another form called, RunAlgorithm
        private void runClick(object sender, EventArgs e)
        {
            individuals = tbIndividuals.Value;
            polygons = tbPolygons.Value;
            best = Convert.ToInt32(tbxBest.Text);
            mutants = Convert.ToInt32(tbxMutants.Text);
            crossovers = Convert.ToInt32(tbxCrossovers.Text);

            RunAlgorithm run = new RunAlgorithm();
            run.FilePath = filePath;
            run.Individuals = individuals;
            run.Polygons = polygons;
            run.Best = best;
            run.Mutants = mutants;
            run.Crossovers = crossovers;

            run.ShowDialog();
            if(run.ClosedForm)
            {
                this.ClosedForm = true;
                this.Close();
            }

        }
        //EVENT HANDLER: clearLabel, click
        //PURPOSE: This click event handler is used for the purpose of instructing the user on how to begin selecting operators
        private void clearLabel(object sender, EventArgs e)
        {
            lblClick.Visible = false;
        }
        //EVENT HANDLER: individualSlider, valueChanged
        //PURPOSE: This event handler will handle necessary changes when the value of Individuals is changed. The necessary changes include
        //         Enabling, updating and validating the Operators track bar, as well as set the maximum value for the track bar and the thumb locations
        //         on the track bar
        private void individualSlider(object sender, EventArgs e)
        {
            tbxIndividuals.Text = tbIndividuals.Value.ToString();
            lblClick.Visible = true;
            tbOperators.Visible = true;
            tbOperators.Enabled = true;
            tbOperators.Maximum = tbIndividuals.Value;
            tbOperators.SelectedMax = (float)(.80 * tbOperators.Maximum);
            tbOperators.SelectedMin = (float)(.20 * tbOperators.Maximum);
            tbOperators.Update();
            tbOperators.Invalidate();
        }
        /********************************TESTING*******************************************/
        private void polygonSlider(object sender, EventArgs e)
        {
            tbxPolygons.Text = tbPolygons.Value.ToString();
           // polygons = tbPolygons.Value;
        }
        private void scoreTarget()
        {
            Bitmap targetBitmap = (Bitmap)pbTarget.Image;
            int width = targetBitmap.Width;
            int height = targetBitmap.Height;

            Color targetPixel;

            targetScore = 0;
            int i, j;
            for (i = 0; i < width; i++)       //every 5 pixels
            {
                for (j = i % 5; j < height; j += 5)    //every 5 pixels
                {
                    targetPixel = targetBitmap.GetPixel(i, j);
                    targetScore += (targetPixel.R) + (targetPixel.G) + (targetPixel.B);
                }
            }
        }
    }
}
