using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
//TODO: Import the System.Xml and 
//System.Configuration namespaces
using System.Xml;
using System.Configuration;
//TODO: Import the System.IO, System.IO.IsolatedStorage,
//System.Runtime.Serialization, and 
//System.Runtime.Serialization.Formatters.Binary namespaces
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Northwind
{
	/// <summary>
	/// Summary description for NorthwindMDIMain.
	/// </summary>
	public class NorthwindMDIMain : System.Windows.Forms.Form
	{
		#region Private Members

		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem miFile;
		private System.Windows.Forms.MenuItem miFCustomers;
		private System.Windows.Forms.MenuItem miFEmployees;
		private System.Windows.Forms.MenuItem miFOrders;
		private System.Windows.Forms.MenuItem miFProducts;
		private System.Windows.Forms.MenuItem miFExit;
		private System.Windows.Forms.MenuItem miWindow;
		private System.Windows.Forms.MenuItem miWTileVertical;
		private System.Windows.Forms.MenuItem miWTileHorizontal;
		private System.Windows.Forms.MenuItem miWCascade;
		private System.Windows.Forms.MainMenu mmMainMenu;
		private System.Windows.Forms.MenuItem miChooseRegion;
		private System.Windows.Forms.MenuItem miHelp;
		private System.Windows.Forms.MenuItem miHAbout;
		private System.Windows.Forms.MenuItem miFRegionView;
		private System.Windows.Forms.MenuItem miFBeautyTips;
		private System.Windows.Forms.MenuItem miFEvenMoreControls;
		private System.Windows.Forms.MenuItem miFImportFile;
		private System.Windows.Forms.MenuItem miFBeautifulForm;
		private System.Windows.Forms.MenuItem miFCommonDialogs;
		private System.Windows.Forms.MenuItem miFArrays;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		#endregion

		public NorthwindMDIMain()
		{
			//
			// 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.mmMainMenu = new System.Windows.Forms.MainMenu();
			this.miFile = new System.Windows.Forms.MenuItem();
			this.miFCustomers = new System.Windows.Forms.MenuItem();
			this.miFEmployees = new System.Windows.Forms.MenuItem();
			this.miFOrders = new System.Windows.Forms.MenuItem();
			this.miFProducts = new System.Windows.Forms.MenuItem();
			this.miChooseRegion = new System.Windows.Forms.MenuItem();
			this.miFRegionView = new System.Windows.Forms.MenuItem();
			this.miFImportFile = new System.Windows.Forms.MenuItem();
			this.miFBeautyTips = new System.Windows.Forms.MenuItem();
			this.miFBeautifulForm = new System.Windows.Forms.MenuItem();
			this.miFCommonDialogs = new System.Windows.Forms.MenuItem();
			this.miFEvenMoreControls = new System.Windows.Forms.MenuItem();
			this.miFArrays = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.miFExit = new System.Windows.Forms.MenuItem();
			this.miWindow = new System.Windows.Forms.MenuItem();
			this.miWTileVertical = new System.Windows.Forms.MenuItem();
			this.miWTileHorizontal = new System.Windows.Forms.MenuItem();
			this.miWCascade = new System.Windows.Forms.MenuItem();
			this.miHelp = new System.Windows.Forms.MenuItem();
			this.miHAbout = new System.Windows.Forms.MenuItem();
			// 
			// mmMainMenu
			// 
			this.mmMainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.miFile,
																					   this.miWindow,
																					   this.miHelp});
			// 
			// miFile
			// 
			this.miFile.Index = 0;
			this.miFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				   this.miFCustomers,
																				   this.miFEmployees,
																				   this.miFOrders,
																				   this.miFProducts,
																				   this.miChooseRegion,
																				   this.miFRegionView,
																				   this.miFImportFile,
																				   this.miFBeautyTips,
																				   this.menuItem6,
																				   this.miFExit});
			this.miFile.Text = "&File";
			// 
			// miFCustomers
			// 
			this.miFCustomers.Index = 0;
			this.miFCustomers.Text = "&Customers";
			this.miFCustomers.Click += new System.EventHandler(this.miFCustomers_Click);
			// 
			// miFEmployees
			// 
			this.miFEmployees.Index = 1;
			this.miFEmployees.Text = "&Employees";
			this.miFEmployees.Click += new System.EventHandler(this.miFEmployees_Click);
			// 
			// miFOrders
			// 
			this.miFOrders.Index = 2;
			this.miFOrders.Text = "&Orders";
			this.miFOrders.Click += new System.EventHandler(this.miFOrders_Click);
			// 
			// miFProducts
			// 
			this.miFProducts.Index = 3;
			this.miFProducts.Text = "&Products";
			this.miFProducts.Click += new System.EventHandler(this.miFProducts_Click);
			// 
			// miChooseRegion
			// 
			this.miChooseRegion.Index = 4;
			this.miChooseRegion.Text = "Choose &Region";
			this.miChooseRegion.Click += new System.EventHandler(this.miChooseRegion_Click);
			// 
			// miFRegionView
			// 
			this.miFRegionView.Index = 5;
			this.miFRegionView.Text = "Region &View";
			this.miFRegionView.Click += new System.EventHandler(this.miFRegionView_Click);
			// 
			// miFImportFile
			// 
			this.miFImportFile.Index = 6;
			this.miFImportFile.Text = "Import File";
			this.miFImportFile.Click += new System.EventHandler(this.miFImportFile_Click);
			// 
			// miFBeautyTips
			// 
			this.miFBeautyTips.Index = 7;
			this.miFBeautyTips.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						  this.miFBeautifulForm,
																						  this.miFCommonDialogs,
																						  this.miFEvenMoreControls,
																						  this.miFArrays});
			this.miFBeautyTips.Text = "Beauty Tips";
			// 
			// miFBeautifulForm
			// 
			this.miFBeautifulForm.Index = 0;
			this.miFBeautifulForm.Text = "Beautiful Form";
			this.miFBeautifulForm.Click += new System.EventHandler(this.miFBBeautifulForm_Click);
			// 
			// miFCommonDialogs
			// 
			this.miFCommonDialogs.Index = 1;
			this.miFCommonDialogs.Text = "Common Dialogs";
			this.miFCommonDialogs.Click += new System.EventHandler(this.miFBCommonDialogs_Click);
			// 
			// miFEvenMoreControls
			// 
			this.miFEvenMoreControls.Index = 2;
			this.miFEvenMoreControls.Text = "Even More Controls";
			this.miFEvenMoreControls.Click += new System.EventHandler(this.miFEvenMoreControls_Click);
			// 
			// miFArrays
			// 
			this.miFArrays.Index = 3;
			this.miFArrays.Text = "Arrays";
			this.miFArrays.Click += new System.EventHandler(this.miFBArrays_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 8;
			this.menuItem6.Text = "-";
			// 
			// miFExit
			// 
			this.miFExit.Index = 9;
			this.miFExit.Text = "E&xit";
			this.miFExit.Click += new System.EventHandler(this.miFExit_Click);
			// 
			// miWindow
			// 
			this.miWindow.Index = 1;
			this.miWindow.MdiList = true;
			this.miWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.miWTileVertical,
																					 this.miWTileHorizontal,
																					 this.miWCascade});
			this.miWindow.Text = "&Window";
			// 
			// miWTileVertical
			// 
			this.miWTileVertical.Index = 0;
			this.miWTileVertical.Text = "Tile &Vertical";
			this.miWTileVertical.Click += new System.EventHandler(this.miWTileVertical_Click);
			// 
			// miWTileHorizontal
			// 
			this.miWTileHorizontal.Index = 1;
			this.miWTileHorizontal.Text = "Tile &Horizontal";
			this.miWTileHorizontal.Click += new System.EventHandler(this.miWTileHorizontal_Click);
			// 
			// miWCascade
			// 
			this.miWCascade.Index = 2;
			this.miWCascade.Text = "&Cascade";
			this.miWCascade.Click += new System.EventHandler(this.miWCascade_Click);
			// 
			// miHelp
			// 
			this.miHelp.Index = 2;
			this.miHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				   this.miHAbout});
			this.miHelp.Text = "&Help";
			// 
			// miHAbout
			// 
			this.miHAbout.Index = 0;
			this.miHAbout.Text = "&About";
			this.miHAbout.Click += new System.EventHandler(this.miHAbout_Click);
			// 
			// NorthwindMDIMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(599, 496);
			this.IsMdiContainer = true;
			this.Menu = this.mmMainMenu;
			this.Name = "NorthwindMDIMain";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Northwind Orders";
			this.Load += new System.EventHandler(this.NorthwindMDIMain_Load);
			this.Closed += new System.EventHandler(this.NorthwindMDIMain_Closed);

		}
		#endregion

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

		private void miFCustomers_Click(object sender, System.EventArgs e) 
		{
			try
			{
				frmCustomerList custs = new frmCustomerList();
				if(DialogResult.OK==custs.ShowDialog())
				{
					this.Cursor=Cursors.WaitCursor;
					frmCustomers cust = new frmCustomers();
					cust.Initialize(new NorthwindBusinessLogicFacade().RetrieveCustomer(
						custs.SelectedCustomerID));
					cust.MdiParent=this;
					cust.Show();
				}
			}
			catch(Exception ex)
			{
				MainClass.MainUIThreadExceptionHandler(ex);
			}
			finally
			{
				this.Cursor=Cursors.Default;
			}
		}

		private void miFEmployees_Click(object sender, System.EventArgs e) 
		{
			frmEmployees emps = new frmEmployees();
			emps.MdiParent=this;
			emps.Show();		
		}

		private void miFOrders_Click(object sender, System.EventArgs e) 
		{
			frmOrders ords = new frmOrders();
			ords.MdiParent=this;
			ords.Show();
		}

		private void miFProducts_Click(object sender, System.EventArgs e) 
		{
			frmProducts prods = new frmProducts();
			prods.MdiParent=this;
			prods.Show();
		}

		private void miWTileVertical_Click(object sender, System.EventArgs e) 
		{
			this.LayoutMdi(MdiLayout.TileVertical);
		}

		private void miWTileHorizontal_Click(object sender, System.EventArgs e) 
		{
			this.LayoutMdi(MdiLayout.TileHorizontal);
		}

		private void miWCascade_Click(object sender, System.EventArgs e) 
		{
			this.LayoutMdi(MdiLayout.Cascade);
		}

		private void miChooseRegion_Click(object sender, 
			EventArgs e)
		{
			try
			{
				frmRegion rgn = new frmRegion();
				DialogResult res = rgn.ShowDialog();
				if(DialogResult.OK == res)
				{
					MessageBox.Show(rgn.RegionChoice + " Chosen",
						"Region Chosen",
						MessageBoxButtons.OK,
						MessageBoxIcon.Information);
				}
				else
				{
					if(DialogResult.Cancel == res)
					{
						MessageBox.Show("Cancel Clicked",
							"Region Chosen",
							MessageBoxButtons.OK,
							MessageBoxIcon.Information);
					}
				}
			}
			catch(Exception ex)
			{
				MainClass.MainUIThreadExceptionHandler(ex);
			}
			finally
			{
				this.Cursor=Cursors.Default;
			}
		}

		private void miHAbout_Click(object sender, System.EventArgs e)
		{
			frmAbout about = new frmAbout();
			about.ShowDialog();
		}

		private void miFRegionView_Click(object sender, System.EventArgs e)
		{
			frmRegionView rv = new frmRegionView();
			rv.ShowDialog();
		}

		private void miFBBeautifulForm_Click(object sender, System.EventArgs e)
		{
			new BeautifulForm().ShowDialog();
		}

		private void miFEvenMoreControls_Click(object sender, System.EventArgs e)
		{
			new frmEvenMoreControls().ShowDialog();
		}

		private void miFBCommonDialogs_Click(object sender, System.EventArgs e)
		{
			new frmCommonDialogs().ShowDialog();
		}

		private void miFBArrays_Click(object sender, System.EventArgs e)
		{
			new frmTricksWithArrays().ShowDialog();
		}

		private void miFImportFile_Click(object sender, System.EventArgs e)
		{
			new frmFileImportWizard().ShowDialog();
		}

		private void NorthwindMDIMain_Load(object sender, System.EventArgs e)
		{
			try
			{
				this.Cursor=Cursors.WaitCursor;
				frmDashboard dash = new frmDashboard();
				dash.MdiParent = this;
				dash.Show();
				ProcessCustomSection();
				//TODO: Add a call to ReadUserPreferences()
				ReadUserPreferences();
			}
			catch(Exception ex)
			{
				MainClass.MainUIThreadExceptionHandler(ex);
			}
			finally
			{
				this.Cursor=Cursors.Default;
			}
		}

		private void NorthwindMDIMain_Closed(object sender, System.EventArgs e)
		{
			//TODO: Add a call to WriteUserPreferences
			WriteUserPreferences();
		}

		private void ProcessCustomSection()
		{
			//TODO: Add code from the SnippetProcessCustomSection.txt file
			
			XmlNode displayBeautyTipsSection = (XmlNode)ConfigurationSettings.GetConfig(
				"DisplayBeautyTipsSection");
			XmlNode displayBeautyTips = displayBeautyTipsSection.SelectSingleNode(
				"DisplayBeautyTips");
			miFBeautyTips.Visible = Boolean.Parse(displayBeautyTips.Attributes["Visible"].InnerText);
			foreach(XmlNode beautyTip in displayBeautyTips.SelectNodes("BeautyTips"))
			{
				MenuItem miThis=null;
				switch(beautyTip.Attributes["MenuItem"].InnerText)
				{
					case "miFBeautifulForm" :
						miThis = miFBeautifulForm;
						break;
					case "miFArrays" :
						miThis = miFArrays;
						break;
					case "miFCommonDialogs" :
						miThis = miFCommonDialogs;
						break;
					case "miFEvenMoreControls" :
						miThis = miFEvenMoreControls;
						break;
				}
				miThis.Text = beautyTip.Attributes["Text"].InnerText;
				miThis.Visible = Boolean.Parse(beautyTip.Attributes["Visible"].InnerText);
			}
		}

		//TODO: Write a ReadUserPreferences method
		private void ReadUserPreferences()
		{
			try
			{
				IsolatedStorageFile isFile = 
					IsolatedStorageFile.GetStore( 
					IsolatedStorageScope.User 
					| IsolatedStorageScope.Assembly
					, null
					, null);
				IsolatedStorageFileStream isFileStream = new
					IsolatedStorageFileStream("Settings.isf"
					, FileMode.Open
					, isFile);
				IFormatter formatter = new 
					//System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
					BinaryFormatter();
				this.Size = (Size)formatter.Deserialize(isFileStream);
				this.Location = (Point)formatter.Deserialize(isFileStream);
				if(null!=isFileStream)
					isFileStream.Close();
				if(null!=isFile)
					isFile.Close();
			}
			catch(System.IO.FileNotFoundException e)
			{
				//Eat this error...
			}
		}
	
		//TODO: Write a WriteUserPreferences method
		private void WriteUserPreferences()
		{
			IsolatedStorageFile isFile = 
				IsolatedStorageFile.GetStore( 
				IsolatedStorageScope.User 
				| IsolatedStorageScope.Assembly
				, null
				, null);
			IsolatedStorageFileStream isFileStream = new
				IsolatedStorageFileStream("Settings.isf"
				, FileMode.OpenOrCreate
				, isFile);
			IFormatter formatter = new 
				//System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
				BinaryFormatter();
			formatter.Serialize(isFileStream, this.Size);
			formatter.Serialize(isFileStream, this.Location);
			if(null!=isFileStream)
				isFileStream.Close();
			if(null!=isFile)
				isFile.Close();
		}
	}
}