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

namespace Northwind
{
	public enum WizardSteps
	{
		Welcome,
		Step1,
		Step2,
		Step3,
		Finished,
	};

	/// <summary>
	/// Summary description for frmFileImportWizard.
	/// </summary>
	public class frmFileImportWizard : System.Windows.Forms.Form
	{
		private WizardSteps currentWizardStep;
		internal System.Windows.Forms.Panel pnlWelcome;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.PictureBox PictureBox1;
		internal System.Windows.Forms.Panel pnlStep1;
		internal System.Windows.Forms.Button btnBrowse;
		internal System.Windows.Forms.TextBox tbFilePath;
		internal System.Windows.Forms.Label lblFile;
		internal System.Windows.Forms.Panel pnlButtons;
		internal System.Windows.Forms.Button btnNext;
		internal System.Windows.Forms.Button btnFinish;
		internal System.Windows.Forms.Button btnBack;
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Panel pnlNavigate;
		internal System.Windows.Forms.LinkLabel llStep3;
		internal System.Windows.Forms.LinkLabel llStep2;
		internal System.Windows.Forms.LinkLabel llStep1;
		internal System.Windows.Forms.Label lblStep;
		internal System.Windows.Forms.Panel pnlStep2;
		internal System.Windows.Forms.CheckBox cbSuppressErrors;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.OpenFileDialog ofdOpenFile;
		internal System.Windows.Forms.Panel pnlStep3;
		internal System.Windows.Forms.Label lblSuppressErrorsConfirm;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label lblFileName;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		}

		/// <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(frmFileImportWizard));
			this.pnlWelcome = new System.Windows.Forms.Panel();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.PictureBox1 = new System.Windows.Forms.PictureBox();
			this.pnlStep1 = new System.Windows.Forms.Panel();
			this.btnBrowse = new System.Windows.Forms.Button();
			this.tbFilePath = new System.Windows.Forms.TextBox();
			this.lblFile = new System.Windows.Forms.Label();
			this.pnlButtons = new System.Windows.Forms.Panel();
			this.btnNext = new System.Windows.Forms.Button();
			this.btnFinish = new System.Windows.Forms.Button();
			this.btnBack = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.pnlNavigate = new System.Windows.Forms.Panel();
			this.llStep3 = new System.Windows.Forms.LinkLabel();
			this.llStep2 = new System.Windows.Forms.LinkLabel();
			this.llStep1 = new System.Windows.Forms.LinkLabel();
			this.lblStep = new System.Windows.Forms.Label();
			this.pnlStep2 = new System.Windows.Forms.Panel();
			this.cbSuppressErrors = new System.Windows.Forms.CheckBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.ofdOpenFile = new System.Windows.Forms.OpenFileDialog();
			this.pnlStep3 = new System.Windows.Forms.Panel();
			this.lblSuppressErrorsConfirm = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.lblFileName = new System.Windows.Forms.Label();
			this.pnlWelcome.SuspendLayout();
			this.pnlStep1.SuspendLayout();
			this.pnlButtons.SuspendLayout();
			this.pnlNavigate.SuspendLayout();
			this.pnlStep2.SuspendLayout();
			this.pnlStep3.SuspendLayout();
			this.SuspendLayout();
			// 
			// pnlWelcome
			// 
			this.pnlWelcome.Controls.Add(this.Label6);
			this.pnlWelcome.Controls.Add(this.Label3);
			this.pnlWelcome.Controls.Add(this.Label4);
			this.pnlWelcome.Controls.Add(this.Label5);
			this.pnlWelcome.Location = new System.Drawing.Point(203, 194);
			this.pnlWelcome.Name = "pnlWelcome";
			this.pnlWelcome.Size = new System.Drawing.Size(352, 296);
			this.pnlWelcome.TabIndex = 13;
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(40, 136);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(288, 24);
			this.Label6.TabIndex = 4;
			this.Label6.Text = "?\tChoose whether or not to suppress errors";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(40, 112);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(288, 24);
			this.Label3.TabIndex = 3;
			this.Label3.Text = "?\tImport data files containing orders.";
			// 
			// Label4
			// 
			this.Label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label4.Location = new System.Drawing.Point(16, 24);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(288, 56);
			this.Label4.TabIndex = 2;
			this.Label4.Text = "Welcome to the File Import Wizard.";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(16, 88);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(312, 24);
			this.Label5.TabIndex = 0;
			this.Label5.Text = "With this wizard, you will be able to ";
			// 
			// PictureBox1
			// 
			this.PictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("PictureBox1.Image")));
			this.PictureBox1.Location = new System.Drawing.Point(8, 0);
			this.PictureBox1.Name = "PictureBox1";
			this.PictureBox1.Size = new System.Drawing.Size(472, 72);
			this.PictureBox1.TabIndex = 11;
			this.PictureBox1.TabStop = false;
			// 
			// pnlStep1
			// 
			this.pnlStep1.Controls.Add(this.btnBrowse);
			this.pnlStep1.Controls.Add(this.tbFilePath);
			this.pnlStep1.Controls.Add(this.lblFile);
			this.pnlStep1.Location = new System.Drawing.Point(130, 75);
			this.pnlStep1.Name = "pnlStep1";
			this.pnlStep1.Size = new System.Drawing.Size(352, 296);
			this.pnlStep1.TabIndex = 9;
			// 
			// btnBrowse
			// 
			this.btnBrowse.Location = new System.Drawing.Point(309, 143);
			this.btnBrowse.Name = "btnBrowse";
			this.btnBrowse.Size = new System.Drawing.Size(24, 24);
			this.btnBrowse.TabIndex = 2;
			this.btnBrowse.Text = "...";
			this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
			// 
			// tbFilePath
			// 
			this.tbFilePath.Location = new System.Drawing.Point(16, 144);
			this.tbFilePath.Name = "tbFilePath";
			this.tbFilePath.Size = new System.Drawing.Size(296, 20);
			this.tbFilePath.TabIndex = 1;
			this.tbFilePath.Text = "";
			// 
			// lblFile
			// 
			this.lblFile.Location = new System.Drawing.Point(16, 120);
			this.lblFile.Name = "lblFile";
			this.lblFile.Size = new System.Drawing.Size(100, 16);
			this.lblFile.TabIndex = 0;
			this.lblFile.Text = "File:";
			// 
			// pnlButtons
			// 
			this.pnlButtons.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pnlButtons.Controls.Add(this.btnNext);
			this.pnlButtons.Controls.Add(this.btnFinish);
			this.pnlButtons.Controls.Add(this.btnBack);
			this.pnlButtons.Controls.Add(this.btnCancel);
			this.pnlButtons.Location = new System.Drawing.Point(8, 412);
			this.pnlButtons.Name = "pnlButtons";
			this.pnlButtons.Size = new System.Drawing.Size(552, 40);
			this.pnlButtons.TabIndex = 10;
			// 
			// btnNext
			// 
			this.btnNext.Location = new System.Drawing.Point(312, 8);
			this.btnNext.Name = "btnNext";
			this.btnNext.TabIndex = 3;
			this.btnNext.Text = "&Next >";
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// btnFinish
			// 
			this.btnFinish.Location = new System.Drawing.Point(392, 8);
			this.btnFinish.Name = "btnFinish";
			this.btnFinish.TabIndex = 2;
			this.btnFinish.Text = "Finish";
			this.btnFinish.Click += new System.EventHandler(this.btnFinish_Click);
			// 
			// btnBack
			// 
			this.btnBack.Location = new System.Drawing.Point(232, 8);
			this.btnBack.Name = "btnBack";
			this.btnBack.TabIndex = 1;
			this.btnBack.Text = "< &Back";
			this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(152, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 0;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// pnlNavigate
			// 
			this.pnlNavigate.Controls.Add(this.llStep3);
			this.pnlNavigate.Controls.Add(this.llStep2);
			this.pnlNavigate.Controls.Add(this.llStep1);
			this.pnlNavigate.Controls.Add(this.lblStep);
			this.pnlNavigate.Location = new System.Drawing.Point(8, 75);
			this.pnlNavigate.Name = "pnlNavigate";
			this.pnlNavigate.Size = new System.Drawing.Size(120, 296);
			this.pnlNavigate.TabIndex = 12;
			// 
			// llStep3
			// 
			this.llStep3.Location = new System.Drawing.Point(8, 160);
			this.llStep3.Name = "llStep3";
			this.llStep3.Size = new System.Drawing.Size(100, 32);
			this.llStep3.TabIndex = 3;
			this.llStep3.TabStop = true;
			this.llStep3.Text = "Step 3:  Finishing Up";
			// 
			// llStep2
			// 
			this.llStep2.Location = new System.Drawing.Point(8, 128);
			this.llStep2.Name = "llStep2";
			this.llStep2.Size = new System.Drawing.Size(100, 32);
			this.llStep2.TabIndex = 2;
			this.llStep2.TabStop = true;
			this.llStep2.Text = "Step 2: Preparing to continue on";
			// 
			// llStep1
			// 
			this.llStep1.Location = new System.Drawing.Point(8, 96);
			this.llStep1.Name = "llStep1";
			this.llStep1.Size = new System.Drawing.Size(100, 32);
			this.llStep1.TabIndex = 1;
			this.llStep1.TabStop = true;
			this.llStep1.Text = "Step 1:  Gathering Information";
			// 
			// lblStep
			// 
			this.lblStep.Location = new System.Drawing.Point(8, 16);
			this.lblStep.Name = "lblStep";
			this.lblStep.Size = new System.Drawing.Size(104, 40);
			this.lblStep.TabIndex = 0;
			this.lblStep.Text = "Step Label";
			// 
			// pnlStep2
			// 
			this.pnlStep2.Controls.Add(this.cbSuppressErrors);
			this.pnlStep2.Controls.Add(this.Label1);
			this.pnlStep2.Location = new System.Drawing.Point(123, 114);
			this.pnlStep2.Name = "pnlStep2";
			this.pnlStep2.Size = new System.Drawing.Size(352, 296);
			this.pnlStep2.TabIndex = 15;
			// 
			// cbSuppressErrors
			// 
			this.cbSuppressErrors.Location = new System.Drawing.Point(80, 136);
			this.cbSuppressErrors.Name = "cbSuppressErrors";
			this.cbSuppressErrors.Size = new System.Drawing.Size(112, 24);
			this.cbSuppressErrors.TabIndex = 1;
			this.cbSuppressErrors.Text = "Suppress Errors";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(32, 112);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(240, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "Do you want to suppress error messages?";
			// 
			// pnlStep3
			// 
			this.pnlStep3.Controls.Add(this.lblSuppressErrorsConfirm);
			this.pnlStep3.Controls.Add(this.Label2);
			this.pnlStep3.Controls.Add(this.lblFileName);
			this.pnlStep3.Location = new System.Drawing.Point(155, 154);
			this.pnlStep3.Name = "pnlStep3";
			this.pnlStep3.Size = new System.Drawing.Size(352, 296);
			this.pnlStep3.TabIndex = 14;
			// 
			// lblSuppressErrorsConfirm
			// 
			this.lblSuppressErrorsConfirm.Location = new System.Drawing.Point(32, 128);
			this.lblSuppressErrorsConfirm.Name = "lblSuppressErrorsConfirm";
			this.lblSuppressErrorsConfirm.Size = new System.Drawing.Size(240, 16);
			this.lblSuppressErrorsConfirm.TabIndex = 3;
			this.lblSuppressErrorsConfirm.Text = "Errors will/will not be supressed";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(32, 72);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(240, 16);
			this.Label2.TabIndex = 2;
			this.Label2.Text = "File to Import:";
			// 
			// lblFileName
			// 
			this.lblFileName.Location = new System.Drawing.Point(32, 96);
			this.lblFileName.Name = "lblFileName";
			this.lblFileName.Size = new System.Drawing.Size(312, 40);
			this.lblFileName.TabIndex = 0;
			this.lblFileName.Text = "File Name";
			// 
			// frmFileImportWizard
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(568, 458);
			this.ControlBox = false;
			this.Controls.Add(this.PictureBox1);
			this.Controls.Add(this.pnlStep1);
			this.Controls.Add(this.pnlButtons);
			this.Controls.Add(this.pnlNavigate);
			this.Controls.Add(this.pnlStep2);
			this.Controls.Add(this.pnlStep3);
			this.Controls.Add(this.pnlWelcome);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmFileImportWizard";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "File Import Wizard";
			this.Load += new System.EventHandler(this.frmFileImportWizard_Load);
			this.pnlWelcome.ResumeLayout(false);
			this.pnlStep1.ResumeLayout(false);
			this.pnlButtons.ResumeLayout(false);
			this.pnlNavigate.ResumeLayout(false);
			this.pnlStep2.ResumeLayout(false);
			this.pnlStep3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmFileImportWizard_Load(object sender, System.EventArgs e)
		{
			pnlWelcome.Bounds = pnlStep1.Bounds;
			pnlStep2.Bounds = pnlStep1.Bounds;
			pnlStep3.Bounds = pnlStep1.Bounds;
			pnlStep3.Bounds = pnlStep1.Bounds;
			this.Size = new Size(494, 454);
			DoStep();
		}

		private void DoStep()
		{
			btnBack.Enabled = 
				!(currentWizardStep== WizardSteps.Welcome);
			btnNext.Enabled = !(currentWizardStep 
				== WizardSteps.Step3); //2nd to last step
			btnFinish.Enabled = (currentWizardStep
				== WizardSteps.Step3 &&
				System.IO.File.Exists(tbFilePath.Text));
			llStep1.Enabled = ! (currentWizardStep ==
				WizardSteps.Step1);
			llStep2.Enabled = ! (currentWizardStep ==
				WizardSteps.Step2);
			llStep3.Enabled = ! (currentWizardStep ==
				WizardSteps.Step3);
			pnlWelcome.Visible = currentWizardStep == WizardSteps.Welcome;
			pnlStep1.Visible = (currentWizardStep ==
				WizardSteps.Step1);
			pnlStep2.Visible = (currentWizardStep ==
				WizardSteps.Step2);
			pnlStep3.Visible = (currentWizardStep ==
				WizardSteps.Step3);

			switch(currentWizardStep)
			{
				case (WizardSteps.Welcome) :
					lblStep.Text = "Welcome";
					break;
				case (WizardSteps.Step1) :
					lblStep.Text = "Step 1:  Gathering Information";
					break;
				case (WizardSteps.Step2) :
					lblStep.Text = "Step 2:  Setting Options";
					break;
				case (WizardSteps.Step3) :
					lblStep.Text = "Step 3:  Confirmation";
					lblFileName.Text = tbFilePath.Text;
					lblSuppressErrorsConfirm.Text = cbSuppressErrors.Checked ?
						"Errors Will Be Suppressed" :
						"Errors Will NOT Be Suppressed";
					break;
				case (WizardSteps.Finished) :
					this.Close();
					MessageBox.Show("Wizard Completed Successfully!" 
						, "Finished" 
						, MessageBoxButtons.OK 
						, MessageBoxIcon.Information);
					break;
			}
		}

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

		private void btnBrowse_Click(object sender, System.EventArgs e)
		{
			ofdOpenFile.InitialDirectory = Path.GetDirectoryName(
				Assembly.GetEntryAssembly().Location);
			if(DialogResult.OK==ofdOpenFile.ShowDialog())
			{
				tbFilePath.Text = ofdOpenFile.FileName;
			}
		}

		private void btnNext_Click(object sender, System.EventArgs e)
		{
			currentWizardStep++;
			DoStep();
		}

		private void btnBack_Click(object sender, System.EventArgs e)
		{
			currentWizardStep--;
			DoStep();

		}

		private void btnFinish_Click(object sender, System.EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			bool Retry=true;
			//TODO:  Create a loop that runs until Retry is false
			while (Retry)
			{
				//TODO:  Create a try block
				try 
				{
					FileStream fs = File.Open(tbFilePath.Text 
						,FileMode.Open
						,FileAccess.ReadWrite 
						,FileShare.ReadWrite);
					fs.Write(new byte[] 
				{(byte)'a',(byte)'b',(byte)'c'},0,3);
					Retry=false;
					currentWizardStep = WizardSteps.Finished;
					DoStep();
				}
					//TODO: Create a catch block to filter 
					//      UnauthorizedAccessExceptions
				catch(UnauthorizedAccessException exception)
				{
					if(DialogResult.Cancel == MessageBox.Show(
						"Could not not write to the file due to "
						+ "permisions.  If you can correct the "
						+ "condition, do so and click 'Retry'.  "
						+ "If not, click 'Cancel'"
						, "Unauthorized Access"
						, MessageBoxButtons.RetryCancel 
						, MessageBoxIcon.Warning
						, MessageBoxDefaultButton.Button2))
						return;
				}

					//TODO: Create a catch block to filter 
					//      IOException
				catch(IOException exception)
				{
					if(DialogResult.Cancel == MessageBox.Show(
						"Could not not write to the file due to "
						+ "a file i/o error.  If you can correct the "
						+ "condition, do so and click 'Retry'.  "
						+ "If not, click 'Cancel'"
						, "Unauthorized Access"
						, MessageBoxButtons.RetryCancel 
						, MessageBoxIcon.Warning
						, MessageBoxDefaultButton.Button2))
						return;
				}

					//TODO:  Create a catch all block
				catch(Exception exception)
				{
					MainClass.MainUIThreadExceptionHandler(this,
						new System.Threading.ThreadExceptionEventArgs(
						exception));
					Retry=false;

					//TODO:  Create a finally block that sets the 
					//Cursor property to Cursors.Default
				}
				finally
				{
					this.Cursor = Cursors.Default;
				}
			}
		}
	}
}