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

namespace Northwind
{
	public class frmRegionView : Northwind.frmBaseForm
	{
		private int EmployeeID=0;
		private System.Windows.Forms.Panel pnlMain;
		private System.Windows.Forms.TreeView tvRegions;
		private System.Windows.Forms.Splitter splVertical;
		private System.Windows.Forms.Panel pnlCustomerDetails;
		private System.Windows.Forms.Label lblCustomerID;
		private System.Windows.Forms.TextBox tbCustomerID;
		private System.Windows.Forms.Label lblCompanyName;
		private System.Windows.Forms.TextBox tbCompanyName;
		private System.Windows.Forms.Button btnLoadOrders;
		private System.Windows.Forms.Splitter splHorizontal;
		private System.Windows.Forms.DataGrid dgOrders;
		private System.ComponentModel.IContainer components = null;

		public frmRegionView()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the 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 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(frmRegionView));
			this.pnlMain = new System.Windows.Forms.Panel();
			this.dgOrders = new System.Windows.Forms.DataGrid();
			this.splHorizontal = new System.Windows.Forms.Splitter();
			this.pnlCustomerDetails = new System.Windows.Forms.Panel();
			this.btnLoadOrders = new System.Windows.Forms.Button();
			this.tbCompanyName = new System.Windows.Forms.TextBox();
			this.lblCompanyName = new System.Windows.Forms.Label();
			this.tbCustomerID = new System.Windows.Forms.TextBox();
			this.lblCustomerID = new System.Windows.Forms.Label();
			this.splVertical = new System.Windows.Forms.Splitter();
			this.tvRegions = new System.Windows.Forms.TreeView();
			this.pnlMain.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dgOrders)).BeginInit();
			this.pnlCustomerDetails.SuspendLayout();
			this.SuspendLayout();
			// 
			// miFOpen
			// 
			this.miFOpen.Text = "&Load";
			this.miFOpen.Click += new System.EventHandler(this.miFOpen_Click);
			// 
			// miFNew
			// 
			this.miFNew.Visible = false;
			// 
			// ilImages
			// 
			this.ilImages.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilImages.ImageStream")));
			// 
			// tbToolBar
			// 
			this.tbToolBar.Name = "tbToolBar";
			this.tbToolBar.Size = new System.Drawing.Size(542, 28);
			this.tbToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tbToolBar_ButtonClick);
			// 
			// tbbOpen
			// 
			this.tbbOpen.ToolTipText = "Load";
			// 
			// tbbNew
			// 
			this.tbbNew.Visible = false;
			// 
			// pnlMain
			// 
			this.pnlMain.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pnlMain.Controls.Add(this.dgOrders);
			this.pnlMain.Controls.Add(this.splHorizontal);
			this.pnlMain.Controls.Add(this.pnlCustomerDetails);
			this.pnlMain.Controls.Add(this.splVertical);
			this.pnlMain.Controls.Add(this.tvRegions);
			this.pnlMain.Location = new System.Drawing.Point(0, 32);
			this.pnlMain.Name = "pnlMain";
			this.pnlMain.Size = new System.Drawing.Size(540, 410);
			this.pnlMain.TabIndex = 2;
			// 
			// dgOrders
			// 
			this.dgOrders.DataMember = "";
			this.dgOrders.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dgOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dgOrders.Location = new System.Drawing.Point(259, 211);
			this.dgOrders.Name = "dgOrders";
			this.dgOrders.Size = new System.Drawing.Size(281, 199);
			this.dgOrders.TabIndex = 4;
			// 
			// splHorizontal
			// 
			this.splHorizontal.Dock = System.Windows.Forms.DockStyle.Top;
			this.splHorizontal.Location = new System.Drawing.Point(259, 208);
			this.splHorizontal.Name = "splHorizontal";
			this.splHorizontal.Size = new System.Drawing.Size(281, 3);
			this.splHorizontal.TabIndex = 3;
			this.splHorizontal.TabStop = false;
			// 
			// pnlCustomerDetails
			// 
			this.pnlCustomerDetails.Controls.Add(this.btnLoadOrders);
			this.pnlCustomerDetails.Controls.Add(this.tbCompanyName);
			this.pnlCustomerDetails.Controls.Add(this.lblCompanyName);
			this.pnlCustomerDetails.Controls.Add(this.tbCustomerID);
			this.pnlCustomerDetails.Controls.Add(this.lblCustomerID);
			this.pnlCustomerDetails.Dock = System.Windows.Forms.DockStyle.Top;
			this.pnlCustomerDetails.Location = new System.Drawing.Point(259, 0);
			this.pnlCustomerDetails.Name = "pnlCustomerDetails";
			this.pnlCustomerDetails.Size = new System.Drawing.Size(281, 208);
			this.pnlCustomerDetails.TabIndex = 2;
			// 
			// btnLoadOrders
			// 
			this.btnLoadOrders.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnLoadOrders.Enabled = false;
			this.btnLoadOrders.Location = new System.Drawing.Point(200, 176);
			this.btnLoadOrders.Name = "btnLoadOrders";
			this.btnLoadOrders.TabIndex = 4;
			this.btnLoadOrders.Text = "Load Orders";
			this.btnLoadOrders.Click += new System.EventHandler(this.btnLoadOrders_Click);
			// 
			// tbCompanyName
			// 
			this.tbCompanyName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.tbCompanyName.Location = new System.Drawing.Point(96, 32);
			this.tbCompanyName.Name = "tbCompanyName";
			this.tbCompanyName.ReadOnly = true;
			this.tbCompanyName.Size = new System.Drawing.Size(176, 20);
			this.tbCompanyName.TabIndex = 3;
			this.tbCompanyName.Text = "";
			// 
			// lblCompanyName
			// 
			this.lblCompanyName.Location = new System.Drawing.Point(8, 32);
			this.lblCompanyName.Name = "lblCompanyName";
			this.lblCompanyName.TabIndex = 2;
			this.lblCompanyName.Text = "Company Name:";
			// 
			// tbCustomerID
			// 
			this.tbCustomerID.Location = new System.Drawing.Point(88, 8);
			this.tbCustomerID.Name = "tbCustomerID";
			this.tbCustomerID.ReadOnly = true;
			this.tbCustomerID.TabIndex = 1;
			this.tbCustomerID.Text = "";
			// 
			// lblCustomerID
			// 
			this.lblCustomerID.Location = new System.Drawing.Point(8, 8);
			this.lblCustomerID.Name = "lblCustomerID";
			this.lblCustomerID.TabIndex = 0;
			this.lblCustomerID.Text = "Customer ID:";
			// 
			// splVertical
			// 
			this.splVertical.Location = new System.Drawing.Point(256, 0);
			this.splVertical.Name = "splVertical";
			this.splVertical.Size = new System.Drawing.Size(3, 410);
			this.splVertical.TabIndex = 1;
			this.splVertical.TabStop = false;
			// 
			// tvRegions
			// 
			this.tvRegions.Dock = System.Windows.Forms.DockStyle.Left;
			this.tvRegions.ImageList = this.ilImages;
			this.tvRegions.Location = new System.Drawing.Point(0, 0);
			this.tvRegions.Name = "tvRegions";
			this.tvRegions.Size = new System.Drawing.Size(256, 410);
			this.tvRegions.TabIndex = 0;
			this.tvRegions.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvRegions_AfterSelect);
			// 
			// frmRegionView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(542, 466);
			this.Controls.Add(this.pnlMain);
			this.Name = "frmRegionView";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Customers View by Region, Territory and Employee";
			this.Load += new System.EventHandler(this.frmRegionView_Load);
			this.Controls.SetChildIndex(this.pnlMain, 0);
			this.Controls.SetChildIndex(this.tbToolBar, 0);
			this.pnlMain.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dgOrders)).EndInit();
			this.pnlCustomerDetails.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmRegionView_Load(object sender, System.EventArgs e)
		{
			AsynchLoad();
		}

		private void AsynchLoad()
		{
			System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(
				LoadTree));
			t.IsBackground=true;
			t.Start();		
		}

		//to marshal calls back into the main UI thread from 
		//a background worker loading thread
		private delegate int TreeNodeAddDelegate(TreeNode node);

		private void LoadTree()
		{
		
			tvRegions.Nodes.Clear();
			isCanceled=false;
			miFOpen.Enabled=false;
			tbbOpen.Enabled=false;
			this.Text = "Customers View by Region, Territory and Employee (Loading data)";
			SetStatusText("Loading data...");
			NorthwindDataAccess nwda = new NorthwindDataAccess();
			dsRegionTerritoryEmployeeCustomer dsRegionTerritoryEmployeeCustomer1 = 
				nwda.RetrieveRegionTerritoryEmployeeCustomer();
			int rowsLoaded=0, 
				rowCount = dsRegionTerritoryEmployeeCustomer1.
				vwRegionTerritoryEmployeeCustomer.Rows.Count;
			TreeNode Region=null, Territory=null, Employee=null;
			//Loop through all the rows, and create tree nodes for each region,
			//territory, employee and customer
			foreach(dsRegionTerritoryEmployeeCustomer.vwRegionTerritoryEmployeeCustomerRow 
						row	in dsRegionTerritoryEmployeeCustomer1.vwRegionTerritoryEmployeeCustomer.Rows)
			{
				if(null==Region || row.RegionDescription!=Region.Text)
				{
					//TODO:  Modify this constructor to take two additional
					//arguments: ImageIndex and SelectedImageIndex
					Region=new TreeNode(row.RegionDescription,2,2);
					//Marshal the call back into the main UI thread...
					tvRegions.Invoke(new TreeNodeAddDelegate(tvRegions.Nodes.Add),
						new object[]{Region});
				}
				if(null==Territory || row.TerritoryDescription!=Territory.Text)
				{
					//TODO:  Modify this constructor to take two additional
					//arguments: ImageIndex and SelectedImageIndex
					Territory=new TreeNode(row.TerritoryDescription,3,3);
					//Marshal the call back into the main UI thread...
					tvRegions.Invoke(new TreeNodeAddDelegate(Region.Nodes.Add),
						new object[]{Territory});
				}
				if(null==Employee || row.EmployeeID!=(int)Employee.Tag)
				{
					//TODO:  Modify this constructor to take two additional
					//arguements: ImageIndex and SelectedImageIndex
					Employee=new TreeNode(row.EmployeeName,4,4);
					Employee.Tag=row.EmployeeID;
					//Marshal the call back into the main UI thread...
					tvRegions.Invoke(new TreeNodeAddDelegate(Territory.Nodes.Add),
						new object[]{Employee});
				}
				//TODO:  Modify this constructor to take two additional
				//arguements: ImageIndex and SelectedImageIndex
				TreeNode Customer = new TreeNode(row.CompanyName,5,5);
				Customer.Tag = row.CustomerID;
				//Marshal the call back into the main UI thread...
				tvRegions.Invoke(new TreeNodeAddDelegate(Employee.Nodes.Add),
					new object[]{Customer});
				rowsLoaded++;
				Single percentDone = (Single)rowsLoaded/rowCount*100;
				UpdateProgress(percentDone);
				if(isCanceled)
					break;
			}
			UpdateProgress();
			if(isCanceled)
				SetStatusText("Data load canceled");
			else
				SetStatusText("Data loaded");
			miFOpen.Enabled=true;
			tbbOpen.Enabled=true;
			this.Text = "Customers View by Region, Territory and Employee";
		}

		private void miFOpen_Click(object sender, System.EventArgs e)
		{
			AsynchLoad();
		}

		private void tbToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if(e.Button.Equals(tbbOpen))
				AsynchLoad();
		}

		private void btnLoadOrders_Click(object sender, System.EventArgs e)
		{
			dgOrders.DataSource = new NorthwindDataAccess().RetrieveOrders (EmployeeID, tbCustomerID.Text);
		}

		private void tvRegions_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if(null != e.Node.Parent &&
				null != e.Node.Parent.Parent &&
				null != e.Node.Parent.Parent.Parent)
			{
				//this is a customer node...
				tbCustomerID.Text = (string)e.Node.Tag;
				tbCompanyName.Text = e.Node.Text;
				btnLoadOrders.Enabled = true;
				EmployeeID = (int)e.Node.Parent.Tag;
				btnLoadOrders_Click(null,null);
			}
			else
			{
				//this is not a customer node...
				tbCustomerID.Text = string.Empty;
				tbCompanyName.Text = string.Empty;
				btnLoadOrders.Enabled = true;
				EmployeeID=0;
				dgOrders.DataSource = null;
			}
		}
	}
}

