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;

using System.Numerics;

namespace Mandelbrot
{
    public partial class MandelbrotConfig : Form
    {
        public MandelbrotConfig()
        {
            InitializeComponent();
        }

        // The form this one is configuring.
        private Form1 FractalForm;

        // Initialize the options.
        public void Initialize(Form1 frm)
        {
            FractalForm = frm;

            int maxIter = FractalForm.MaxIterations;
            maxIterationsTextBox.Text = maxIter.ToString("0");

            Complex z = FractalForm.Z;
            zreaTextBox.Text = z.Real.ToString("0.00");
            zimaTextBox.Text = z.Imaginary.ToString("0.00");

            // Deselect all colors.
            noneButton_Click(null, null);

            // Select the colors in use by the main form.
            for (int i = 0; i < FractalForm.Colors.Count; i++)
            {
                // Select the PictureBox with this color.
                SelectColor(FractalForm.Colors[i]);
            }
        }

        // Select all colors.
        private void allButton_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is PictureBox)
                {
                    PictureBox pic = ctl as PictureBox;
                    pic.BorderStyle = BorderStyle.Fixed3D;
                }
            }
        }

        // Deselect all colors.
        private void noneButton_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is PictureBox)
                {
                    PictureBox pic = ctl as PictureBox;
                    pic.BorderStyle = BorderStyle.None;
                }
            }
        }

        // Select the colors in this column.
        private void SelectColumn(int col)
        {
            // Deselect all colors.
            noneButton_Click(null, null);

            // Select the colors in this column.
            int first = col * 6; 
            for (int i = 0; i <= 5; i++)
            {
                string picName = "colorPictureBox" + (first + i).ToString();
                SelectPictureBox(picName);
            }
        }

        // Select the colors in this row.
        private void SelectRow(int row)
        {
            // Deselect all colors.
            noneButton_Click(null, null);

            // Select the colors in this column.
            for (int i = 0; i <= 7; i++)
            {
                string picName = "colorPictureBox" + (row + i * 6).ToString();
                SelectPictureBox(picName);
            }
        }

        // Select the PictureBox with this name.
        private void SelectPictureBox(string picName)
        {
            Control ctl = GetControlByName(picName);
            PictureBox pic = ctl as PictureBox;
            pic.BorderStyle = BorderStyle.Fixed3D;
        }

        // Select the PictureBox with this color.
        private void SelectColor(Color clr)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is PictureBox)
                {
                    if (ctl.BackColor.Equals(clr))
                    {
                        PictureBox pic = ctl as PictureBox;
                        pic.BorderStyle = BorderStyle.Fixed3D;
                        return;
                    }
                }
            }
        }

        // Return the control with this name.
        private Control GetControlByName(string picName)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl.Name == picName) return ctl;
            }
            return null;
        }

        // Select the colors in a column.
        private void colButton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            SelectColumn(int.Parse(btn.Tag.ToString()));
        }

        // Select the colors in this row.
        private void rowButton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            SelectRow(int.Parse(btn.Tag.ToString()));
        }

        // Apply the changes.
        private void okButton_Click(object sender, EventArgs e)
        {
            // Get the number of iterations.
            FractalForm.MaxIterations = int.Parse(maxIterationsTextBox.Text);

            // Get Z.
            FractalForm.Z = new Complex(
                double.Parse(zreaTextBox.Text),
                double.Parse(zimaTextBox.Text));

            // Save the selected colors.
            // (This is a little odd because we want to save them
            // in the order in which they appear on the dialog.)
            FractalForm.ResetColors();
            for (int i = 0; i < 48; i++)
            {
                // See if this color is selected.
                Control ctl = GetControlByName("colorPictureBox" + i.ToString());
                PictureBox pic = ctl as PictureBox;
                if (pic.BorderStyle == BorderStyle.Fixed3D)
                    FractalForm.Colors.Add(pic.BackColor);
            }

            this.Close();
        }

        // Close the dialog without saving the changes.
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Select some default colors.
        private void defaultButton_Click(object sender, EventArgs e)
        {
            // Deselect all colors.
            noneButton_Click(null, null);

            // Select the default colors.
            colorPictureBox5.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox8.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox14.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox20.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox26.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox32.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox38.BorderStyle = BorderStyle.Fixed3D;
            colorPictureBox44.BorderStyle = BorderStyle.Fixed3D;
        }

        // Select ths color.
        private void colorPictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pic = sender as PictureBox;
            if (pic.BorderStyle == BorderStyle.Fixed3D)
                pic.BorderStyle = BorderStyle.None;
            else
                pic.BorderStyle = BorderStyle.Fixed3D;
        }
    }
}
