using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestLibrary;


namespace TestApplication
{
    public partial class Form1 : Form
    {
        // Create an instance of TestClass.
        TestClass TheClass;

        // The current ball.
        List<BallData> CurrentBalls;

        // The selected ball within the list.
        Int32 SelectedBall;

        public Form1()
        {
            InitializeComponent();

            // Initialize the TestClass.
            TheClass = new TestClass();

            // Set the source of information for the comboboxes.
            cbColor.Items.AddRange(Enum.GetNames(typeof(BallColor)));
            cbSize.Items.AddRange(Enum.GetNames(typeof(BallSize)));

            // Create an empty ball list.
            CurrentBalls = new List<BallData>();

            // Set the selected ball number.
            SelectedBall = 0;

            // Add event handlers for the class.
            TheClass.OnBallAdded += new TestClass.BallAdded(TheClass_OnBallAdded);
            TheClass.OnBallDeleted += new TestClass.BallDeleted(TheClass_OnBallDeleted);
            TheClass.OnEndOfBalls += new TestClass.EndOfBalls(TheClass_OnEndOfBalls);
        }

        void TheClass_OnEndOfBalls()
        {
            MessageBox.Show("End of the list!");
        }

        void TheClass_OnBallDeleted(BallData Ball)
        {
            MessageBox.Show("The ball was deleted.");
        }

        void TheClass_OnBallAdded(BallData Ball)
        {
            MessageBox.Show("The ball was added.");
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            // End the program.
            Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Don't allow blank values.
            if ((txtBallName.Text == "") | (cbColor.Text == "") | (cbSize.Text == ""))
            {
                MessageBox.Show("You must provide values for all three fields.");
                return;
            }

            // Crate a new BallData structure.
            BallData NewBall = new BallData();

            // Fill it with data.
            try
            {
                // The name may not meet the criterion.
                NewBall.Name = txtBallName.Text;
            }
            catch (ArgumentOutOfRangeException AOORE)
            {
                // If not, display an error message, display the
                // previous data, and exit.
                MessageBox.Show(AOORE.Message);
                DisplayCurrentBall();
                return;
            }
            NewBall.Color = (BallColor)cbColor.SelectedIndex;
            NewBall.Size = (BallSize)cbSize.SelectedIndex;

            // Create the new ball.
            CurrentBalls = TheClass.AddBall(NewBall);

            // Change the ball selection.
            SelectedBall = CurrentBalls.Count - 1;

            // Display the ball information.
            DisplayCurrentBall();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // Create a blank BallData object.
            BallData DeleteBall = new BallData();

            // Obtain the correct ball to delete based on the entry
            // in the textbox.
            try
            {
                DeleteBall = TheClass.GetBall(txtBallName.Text);
            }
            catch (KeyNotFoundException KNFE)
            {
                // Display an error message if the ball isn't found and
                // exit the event handler.
                MessageBox.Show(KNFE.Message);
                return;
            }

            // Use the information to delete the ball.
            try
            {
                CurrentBalls = TheClass.DeleteBall(DeleteBall);
            }
            catch (InvalidOperationException IOE)
            {
                // Display an error message if necessary.
                MessageBox.Show(IOE.Message);
            }

            // Check the number of remaining balls.
            if (TheClass.BallCount > 0)
            {
                // Reset the selected ball and redisplay if possible.
                SelectedBall = 0;
                DisplayCurrentBall();
            }
            else
                DisplayCurrentBall();
        }

        private void DisplayCurrentBall()
        {
            if (TheClass.BallCount == 0)
            {
                // Clear the form.
                txtBallName.Text = "";
                cbColor.SelectedIndex = -1;
                cbSize.SelectedIndex = -1;

                // Disable the Next and Previous buttons.
                btnNext.Enabled = false;
                btnPrevious.Enabled = false;
            }
            else
            {

                // Enable or disable the Next and Previous buttons as needed.
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
                if (SelectedBall == TheClass.BallCount - 1)
                    btnNext.Enabled = false;
                if (SelectedBall == 0)
                    btnPrevious.Enabled = false;

                // Obtain the information for the selected element.
                BallData ThisBall = CurrentBalls.ElementAt(SelectedBall);

                // Place the information in the form.
                txtBallName.Text = ThisBall.Name;
                cbSize.SelectedItem = ThisBall.Size.ToString();
                cbColor.SelectedItem = ThisBall.Color.ToString();
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            // Obtain a list of the ball names.
            List<String> BallNames = TheClass.GetNames();

            // Create a string from the array.
            String Result = "";
            foreach (String Item in BallNames)
                Result += Item + "\r\n";

            // Display them in a message box.
            MessageBox.Show(Result);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            // If we're not at the end of the list, increment the
            // selected ball count and display the new information.
            if (SelectedBall != TheClass.BallCount - 1)
            {
                SelectedBall++;
                DisplayCurrentBall();
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            // If we're not at the beginning of the list, decrement the
            // selected ball count and display the new information.
            if (SelectedBall != 0)
            {
                SelectedBall--;
                DisplayCurrentBall();
            }
        }
    }
}
