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

namespace Northwind
{
	/// <summary>
	/// Summary description for frmCustomers.
	/// </summary>
	public class frmCustomers : frmBaseForm
	{
		public delegate void DoneDelegate(bool Stopped);
		public delegate void
			WorkerThreadExceptionHandlerDelegate(Exception e);
		void WorkerThreadExceptionHandler(Exception e)
		{
			UpdateProgress();
			SetStatusText("Problem Encountered");
			this.Text = "Customers";
			btnSave.Text = "Save";
			btnClose.Enabled = true;
			
			//Call the global exception handler
			MainClass.MainUIThreadExceptionHandler(
				this, new System.Threading.
				ThreadExceptionEventArgs(e));
		}

		#region internal members
		internal System.Windows.Forms.Button btnSave;
		internal System.Windows.Forms.Label Label11;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.Button btnClose;
		#endregion

		private Northwind.dsCustomers dsCustomers;
		internal System.Windows.Forms.TextBox tbCustomerID;
		internal System.Windows.Forms.ComboBox cboCountry;
		internal System.Windows.Forms.ComboBox cboRegion;
		internal System.Windows.Forms.TextBox tbFax;
		internal System.Windows.Forms.TextBox tbPhone;
		internal System.Windows.Forms.TextBox tbPostalCode;
		internal System.Windows.Forms.TextBox tbCity;
		internal System.Windows.Forms.TextBox tbAddress;
		internal System.Windows.Forms.TextBox tbTitle;
		internal System.Windows.Forms.TextBox tbContactName;
		internal System.Windows.Forms.TextBox tbCompanyName;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCustomers()
		{
			//
			// 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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmCustomers));
			this.btnClose = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.Label11 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label8 = new System.Windows.Forms.Label();
			this.Label7 = new System.Windows.Forms.Label();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.cboCountry = new System.Windows.Forms.ComboBox();
			this.cboRegion = new System.Windows.Forms.ComboBox();
			this.tbFax = new System.Windows.Forms.TextBox();
			this.tbPhone = new System.Windows.Forms.TextBox();
			this.tbPostalCode = new System.Windows.Forms.TextBox();
			this.tbCity = new System.Windows.Forms.TextBox();
			this.tbAddress = new System.Windows.Forms.TextBox();
			this.tbTitle = new System.Windows.Forms.TextBox();
			this.tbContactName = new System.Windows.Forms.TextBox();
			this.tbCompanyName = new System.Windows.Forms.TextBox();
			this.tbCustomerID = new System.Windows.Forms.TextBox();
			this.dsCustomers = new Northwind.dsCustomers();
			((System.ComponentModel.ISupportInitialize)(this.dsCustomers)).BeginInit();
			this.SuspendLayout();
			// 
			// ilImages
			// 
			this.ilImages.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilImages.ImageStream")));
			// 
			// tbToolBar
			// 
			this.tbToolBar.Name = "tbToolBar";
			this.tbToolBar.Size = new System.Drawing.Size(306, 28);
			// 
			// btnClose
			// 
			this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnClose.Location = new System.Drawing.Point(158, 296);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 47;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(50, 296);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 46;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// Label11
			// 
			this.Label11.Location = new System.Drawing.Point(42, 268);
			this.Label11.Name = "Label11";
			this.Label11.TabIndex = 44;
			this.Label11.Text = "&Fax";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(42, 246);
			this.Label10.Name = "Label10";
			this.Label10.TabIndex = 42;
			this.Label10.Text = "&Phone";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(42, 222);
			this.Label9.Name = "Label9";
			this.Label9.TabIndex = 40;
			this.Label9.Text = "&Country";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(42, 200);
			this.Label8.Name = "Label8";
			this.Label8.TabIndex = 38;
			this.Label8.Text = "&Postal Code";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(42, 176);
			this.Label7.Name = "Label7";
			this.Label7.TabIndex = 36;
			this.Label7.Text = "&Region";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(42, 154);
			this.Label6.Name = "Label6";
			this.Label6.TabIndex = 34;
			this.Label6.Text = "&City";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(42, 130);
			this.Label5.Name = "Label5";
			this.Label5.TabIndex = 32;
			this.Label5.Text = "&Address";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(42, 108);
			this.Label4.Name = "Label4";
			this.Label4.TabIndex = 30;
			this.Label4.Text = "&Title";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(42, 84);
			this.Label3.Name = "Label3";
			this.Label3.TabIndex = 28;
			this.Label3.Text = "&Contact Name:";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(42, 62);
			this.Label2.Name = "Label2";
			this.Label2.TabIndex = 26;
			this.Label2.Text = "Company &Name:";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(42, 38);
			this.Label1.Name = "Label1";
			this.Label1.TabIndex = 24;
			this.Label1.Text = "Customer &ID:";
			// 
			// cboCountry
			// 
			this.cboCountry.Location = new System.Drawing.Point(146, 222);
			this.cboCountry.Name = "cboCountry";
			this.cboCountry.Size = new System.Drawing.Size(121, 21);
			this.cboCountry.TabIndex = 41;
			// 
			// cboRegion
			// 
			this.cboRegion.Location = new System.Drawing.Point(146, 176);
			this.cboRegion.Name = "cboRegion";
			this.cboRegion.Size = new System.Drawing.Size(121, 21);
			this.cboRegion.TabIndex = 37;
			// 
			// tbFax
			// 
			this.tbFax.Location = new System.Drawing.Point(146, 268);
			this.tbFax.Name = "tbFax";
			this.tbFax.TabIndex = 45;
			this.tbFax.Text = "";
			// 
			// tbPhone
			// 
			this.tbPhone.Location = new System.Drawing.Point(146, 246);
			this.tbPhone.Name = "tbPhone";
			this.tbPhone.TabIndex = 43;
			this.tbPhone.Text = "";
			// 
			// tbPostalCode
			// 
			this.tbPostalCode.Location = new System.Drawing.Point(146, 200);
			this.tbPostalCode.Name = "tbPostalCode";
			this.tbPostalCode.TabIndex = 39;
			this.tbPostalCode.Text = "";
			// 
			// tbCity
			// 
			this.tbCity.Location = new System.Drawing.Point(146, 154);
			this.tbCity.Name = "tbCity";
			this.tbCity.TabIndex = 35;
			this.tbCity.Text = "";
			// 
			// tbAddress
			// 
			this.tbAddress.Location = new System.Drawing.Point(146, 130);
			this.tbAddress.Name = "tbAddress";
			this.tbAddress.Size = new System.Drawing.Size(158, 20);
			this.tbAddress.TabIndex = 33;
			this.tbAddress.Text = "";
			// 
			// tbTitle
			// 
			this.tbTitle.Location = new System.Drawing.Point(146, 108);
			this.tbTitle.Name = "tbTitle";
			this.tbTitle.Size = new System.Drawing.Size(158, 20);
			this.tbTitle.TabIndex = 31;
			this.tbTitle.Text = "";
			// 
			// tbContactName
			// 
			this.tbContactName.Location = new System.Drawing.Point(146, 84);
			this.tbContactName.Name = "tbContactName";
			this.tbContactName.Size = new System.Drawing.Size(158, 20);
			this.tbContactName.TabIndex = 29;
			this.tbContactName.Text = "";
			// 
			// tbCompanyName
			// 
			this.tbCompanyName.Location = new System.Drawing.Point(146, 62);
			this.tbCompanyName.Name = "tbCompanyName";
			this.tbCompanyName.Size = new System.Drawing.Size(158, 20);
			this.tbCompanyName.TabIndex = 27;
			this.tbCompanyName.Text = "";
			// 
			// tbCustomerID
			// 
			this.tbCustomerID.Location = new System.Drawing.Point(146, 38);
			this.tbCustomerID.Name = "tbCustomerID";
			this.tbCustomerID.TabIndex = 25;
			this.tbCustomerID.Text = "";
			// 
			// dsCustomers
			// 
			this.dsCustomers.DataSetName = "dsCustomers";
			this.dsCustomers.Locale = new System.Globalization.CultureInfo("en-US");
			// 
			// frmCustomers
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(306, 343);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.Label11);
			this.Controls.Add(this.Label10);
			this.Controls.Add(this.Label9);
			this.Controls.Add(this.Label8);
			this.Controls.Add(this.Label7);
			this.Controls.Add(this.Label6);
			this.Controls.Add(this.Label5);
			this.Controls.Add(this.Label4);
			this.Controls.Add(this.Label3);
			this.Controls.Add(this.Label2);
			this.Controls.Add(this.Label1);
			this.Controls.Add(this.cboCountry);
			this.Controls.Add(this.cboRegion);
			this.Controls.Add(this.tbFax);
			this.Controls.Add(this.tbPhone);
			this.Controls.Add(this.tbPostalCode);
			this.Controls.Add(this.tbCity);
			this.Controls.Add(this.tbAddress);
			this.Controls.Add(this.tbTitle);
			this.Controls.Add(this.tbContactName);
			this.Controls.Add(this.tbCompanyName);
			this.Controls.Add(this.tbCustomerID);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmCustomers";
			this.Text = "Customers";
			this.Load += new System.EventHandler(this.frmCustomers_Load);
			this.Controls.SetChildIndex(this.tbCustomerID, 0);
			this.Controls.SetChildIndex(this.tbCompanyName, 0);
			this.Controls.SetChildIndex(this.tbContactName, 0);
			this.Controls.SetChildIndex(this.tbTitle, 0);
			this.Controls.SetChildIndex(this.tbAddress, 0);
			this.Controls.SetChildIndex(this.tbCity, 0);
			this.Controls.SetChildIndex(this.tbPostalCode, 0);
			this.Controls.SetChildIndex(this.tbPhone, 0);
			this.Controls.SetChildIndex(this.tbFax, 0);
			this.Controls.SetChildIndex(this.cboRegion, 0);
			this.Controls.SetChildIndex(this.cboCountry, 0);
			this.Controls.SetChildIndex(this.Label1, 0);
			this.Controls.SetChildIndex(this.Label2, 0);
			this.Controls.SetChildIndex(this.Label3, 0);
			this.Controls.SetChildIndex(this.Label4, 0);
			this.Controls.SetChildIndex(this.Label5, 0);
			this.Controls.SetChildIndex(this.Label6, 0);
			this.Controls.SetChildIndex(this.Label7, 0);
			this.Controls.SetChildIndex(this.Label8, 0);
			this.Controls.SetChildIndex(this.Label9, 0);
			this.Controls.SetChildIndex(this.Label10, 0);
			this.Controls.SetChildIndex(this.Label11, 0);
			this.Controls.SetChildIndex(this.btnSave, 0);
			this.Controls.SetChildIndex(this.btnClose, 0);
			this.Controls.SetChildIndex(this.tbToolBar, 0);
			((System.ComponentModel.ISupportInitialize)(this.dsCustomers)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
	
		private void Done (bool Stopped)
		{

			UpdateProgress();
			this.Text = "Customers";
			btnSave.Enabled=true;
			btnClose.Enabled=true;
			if(Stopped)
			{
				MessageBox.Show("Save Stopped"
					, "Stopped"
					, MessageBoxButtons.OK
					, MessageBoxIcon.Warning);
				SetStatusText("Save Stopped");
			}
			else
			{
				MessageBox.Show("Save Done"
					, "Done" 
					, MessageBoxButtons.OK 
					, MessageBoxIcon.Information);
				SetStatusText("Save Done");
			}
		}

		private void SaveCustomer()
		{
			try
			{
				SetStatusText(
					"Saving record on a background thread...");

				//TODO:  Add code to save the customer record
				this.BindingContext[dsCustomers,"Customers"].
					EndCurrentEdit();
				dsCustomers objDataSetChanges = ((dsCustomers)
					(dsCustomers.GetChanges()));
				if(null!=objDataSetChanges)
					new NorthwindDataAccess().UpdateCustomers(
						objDataSetChanges);

				isCanceled = false;
				DoneDelegate delDone = new DoneDelegate(Done);
				delDone.BeginInvoke(isCanceled, null, null);

			}
			catch(Exception e)
			{
				new WorkerThreadExceptionHandlerDelegate(
					WorkerThreadExceptionHandler).BeginInvoke(e
					,null
					,null);
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(true==btnSave.Enabled)
				{
					this.Text = "Customers (Saving...)";
					btnSave.Enabled=false;
					btnClose.Enabled=false;
					System.Threading.Thread t = 
						new System.Threading.Thread( 
						new System.Threading.ThreadStart(
						SaveCustomer));
					t.Start();
				}
				else
				{
					isCanceled = true;
					btnSave.Enabled = true;
				}		
			}
			catch(NotSupportedException exception)
			{
				MessageBox.Show("Code not done yet "
					+ exception.Message 
					);
			}
			catch(Exception exception)
			{
				//Tell the user a boo-boo occurred...
				MainClass.MainUIThreadExceptionHandler(this
					,new System.Threading.
					ThreadExceptionEventArgs(exception));
				MessageBox.Show(
					"Northwind Orders has encountered a problem.  "
					+ "Please contact your administrator or the "
					+ "Northwind Help desk at "
					+ (char)13 + (char)13 +"1-855-555-5555."
					+ (char)13 + (char)13 
					+ "We apologize for the inconvenience."
					, "Northwind Orders Encountered a Problem"
					, MessageBoxButtons.OK
					, MessageBoxIcon.Error);
			}
			finally
			{
				this.Cursor=Cursors.Default;
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmCustomers_Load(object sender, System.EventArgs e)
		{
			try
			{
				this.Cursor = Cursors.WaitCursor;
				using(NorthwindDataAccess nwda = new NorthwindDataAccess())
				{
					cboRegion.DataSource = 
						nwda.RetrieveCustomerRegionList();
					//TODO: Add code to call RetrieveCustomerCountryList
					//		and set the value to cboCountry.DataSource
					System.Collections.Specialized.StringCollection ret;
					ret = nwda.RetrieveCustomerCountryList();
					cboCountry.DataSource = ret;
				}
				SetupDataBindings();
			}
			catch (Exception ex)
			{
				//Call the global exception handler
				MainClass.MainUIThreadExceptionHandler(ex);
			}
			finally
			{
				this.Cursor = Cursors.Default;
			}
		}

		private void SetupDataBindings()
		{
			tbCustomerID.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.CustomerID"));
			tbCompanyName.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.CompanyName"));
			tbContactName.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.ContactName"));
			tbTitle.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.ContactTitle"));
			tbAddress.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.Address"));
			tbCity.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.City"));
			cboRegion.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.Region"));
			tbPostalCode.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.PostalCode"));
			cboCountry.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.Country"));
			tbPhone.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.Phone"));
			tbFax.DataBindings.Add(new Binding("Text",
				dsCustomers,
				"Customers.Fax"));
		}
		public void Initialize(dsCustomers cust)
		{
			dsCustomers = cust;
		}
	}
}
