using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Northwind
{
	/// <summary>
	/// Summary description for frmTricksWithArrays.
	/// </summary>
	public class frmTricksWithArrays : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnSort;
		internal System.Windows.Forms.Button btnAddRange;
		internal System.Windows.Forms.Button btnLoop;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.TextBox TextBox1;
		internal System.Windows.Forms.ListBox lbItems;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmTricksWithArrays()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnSort = new System.Windows.Forms.Button();
			this.btnAddRange = new System.Windows.Forms.Button();
			this.btnLoop = new System.Windows.Forms.Button();
			this.Label1 = new System.Windows.Forms.Label();
			this.TextBox1 = new System.Windows.Forms.TextBox();
			this.lbItems = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// btnSort
			// 
			this.btnSort.Location = new System.Drawing.Point(114, 242);
			this.btnSort.Name = "btnSort";
			this.btnSort.TabIndex = 11;
			this.btnSort.Text = "Sort Asc";
			this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
			// 
			// btnAddRange
			// 
			this.btnAddRange.Location = new System.Drawing.Point(186, 210);
			this.btnAddRange.Name = "btnAddRange";
			this.btnAddRange.TabIndex = 10;
			this.btnAddRange.Text = "Add Range";
			this.btnAddRange.Click += new System.EventHandler(this.btnAddRange_Click);
			// 
			// btnLoop
			// 
			this.btnLoop.Location = new System.Drawing.Point(42, 210);
			this.btnLoop.Name = "btnLoop";
			this.btnLoop.TabIndex = 9;
			this.btnLoop.Text = "Loop";
			this.btnLoop.Click += new System.EventHandler(this.btnLoop_Click);
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(10, 178);
			this.Label1.Name = "Label1";
			this.Label1.TabIndex = 8;
			this.Label1.Text = "Fill with:";
			// 
			// TextBox1
			// 
			this.TextBox1.Location = new System.Drawing.Point(114, 178);
			this.TextBox1.Name = "TextBox1";
			this.TextBox1.TabIndex = 7;
			this.TextBox1.Text = "10000";
			// 
			// lbItems
			// 
			this.lbItems.Location = new System.Drawing.Point(10, 2);
			this.lbItems.Name = "lbItems";
			this.lbItems.Size = new System.Drawing.Size(272, 173);
			this.lbItems.TabIndex = 6;
			// 
			// frmTricksWithArrays
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.btnSort);
			this.Controls.Add(this.btnAddRange);
			this.Controls.Add(this.btnLoop);
			this.Controls.Add(this.Label1);
			this.Controls.Add(this.TextBox1);
			this.Controls.Add(this.lbItems);
			this.Name = "frmTricksWithArrays";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Tricks With Arrays";
			this.ResumeLayout(false);

		}
		#endregion


		//Get n random numbers
		private object[]GetRandomIntArray(int n)
		{
			object[] aintArray = (object[])Array.CreateInstance(typeof(object),n - 1);
			Random rnd = new Random();
			for(int i = 0;i<aintArray.Length;i++)
			{
				aintArray[i] = Convert.ToInt32( 
					rnd.Next(0, System.Int32.MaxValue));
			}
			return aintArray;
		}

		private void btnLoop_Click(object sender, System.EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;

			object[] aArray  = GetRandomIntArray(Convert.ToInt32(TextBox1.Text));

			lbItems.Items.Clear();

			DateTime start = System.DateTime.Now;

			//Loop through the objects, adding them one
			//at a time using ListBox.Items.Add()
			for(int i=0;i<aArray.Length - 1;i++)
			{
				lbItems.Items.Add(aArray[i]);
			}

			TimeSpan diff = System.DateTime.Now - start ;

			this.Cursor = Cursors.Default;

			MessageBox.Show(diff.TotalMilliseconds.ToString() + " Milliseconds" 
				, "Adding in a loop took:");

		}

		private void btnAddRange_Click(object sender, System.EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;

			object[] aArray  = GetRandomIntArray(Convert.ToInt32(TextBox1.Text));

			lbItems.Items.Clear();

			DateTime start = System.DateTime.Now;

			//Use Addrange
			lbItems.Items.AddRange(aArray);

			TimeSpan diff = System.DateTime.Now - start ;

			this.Cursor = Cursors.Default;

			MessageBox.Show(diff.TotalMilliseconds.ToString() + " Milliseconds" 
				, "AddRange took:");
		}


		#region Array Sorting example

		private void btnSort_Click(System.Object sender
			, System.EventArgs e) 
		{

			this.Cursor = Cursors.WaitCursor;

			//Get the items out of the ListBox:
			object[] aintArray = (object[])Array.CreateInstance(
				typeof(object),lbItems.Items.Count);
			for(int i = 0;i <lbItems.Items.Count;i++)
			{
				aintArray[i] = lbItems.Items[i];
			}

			DateTime start = System.DateTime.Now;

			//Sort the items: 
			if("Sort Asc" == btnSort.Text)
			{
				//Sort ascending - bult in to the array class
				Array.Sort(aintArray);
				//Update the UI
				btnSort.Text = "Sort Desc";

			}
			else
			{

				//Get the items
				//Sort them descending - must use IComparer implementing
				//class for this...

				IComparer Comparer = new DescendingIntComparer();

				Array.Sort(aintArray, Comparer);

				//update the UI
				btnSort.Text = "Sort Asc";

			}

			TimeSpan diff = System.DateTime.Now - start;
						 
			//Push the sorted items back into the listbox:
			lbItems.Items.Clear();
			lbItems.Items.AddRange(aintArray);

			this.Cursor = Cursors.Default;

			MessageBox.Show(diff.TotalMilliseconds.ToString() + " Milliseconds" 
				, "Sorting took:");
		}

		//Using the System.Collections.IComparer interface, you can implement
		//your own sorting however you see fit, you'll instanciate
		//an instance of this class, then pass that variable reference
		//to the Array.Sort() overload that takes two arguments:
		//an array and an IComparer interface.
		private class DescendingIntComparer :IComparer
		{

			public int Compare(object x, object y)
			{
				//Implement our own sorting logic however we want:
				return (-1)*Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
			}

		}

		#endregion


	}
}
