using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Minntech.Common.Enums.CommPortSettings;


namespace Minntech.Common.Settings
{
	/// <summary>
	/// Summary description for dlgCommPortSettings.
	/// </summary>
	public class dlgCommPortSettings : System.Windows.Forms.Form
	{
		#region declarations

		private System.Windows.Forms.ComboBox comboPort;
		private System.Windows.Forms.ComboBox comboBaudRate;
		private System.Windows.Forms.ComboBox comboDataBit;
		private System.Windows.Forms.ComboBox comboParity;
		private System.Windows.Forms.ComboBox comboStopBit;
		private System.Windows.Forms.ComboBox comboHandShake;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button commandCancel;

		private System.ComponentModel.Container components = null;
		
		#endregion

		#region constuct/load/dispose
		
		public dlgCommPortSettings()
		{
			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()
		{
			this.comboPort = new System.Windows.Forms.ComboBox();
			this.comboBaudRate = new System.Windows.Forms.ComboBox();
			this.comboDataBit = new System.Windows.Forms.ComboBox();
			this.comboParity = new System.Windows.Forms.ComboBox();
			this.comboStopBit = new System.Windows.Forms.ComboBox();
			this.comboHandShake = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.commandCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// comboPort
			// 
			this.comboPort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboPort.Location = new System.Drawing.Point(120, 8);
			this.comboPort.Name = "comboPort";
			this.comboPort.Size = new System.Drawing.Size(121, 21);
			this.comboPort.TabIndex = 0;
			// 
			// comboBaudRate
			// 
			this.comboBaudRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBaudRate.Location = new System.Drawing.Point(120, 40);
			this.comboBaudRate.Name = "comboBaudRate";
			this.comboBaudRate.Size = new System.Drawing.Size(121, 21);
			this.comboBaudRate.TabIndex = 1;
			// 
			// comboDataBit
			// 
			this.comboDataBit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboDataBit.Location = new System.Drawing.Point(120, 72);
			this.comboDataBit.Name = "comboDataBit";
			this.comboDataBit.Size = new System.Drawing.Size(121, 21);
			this.comboDataBit.TabIndex = 2;
			// 
			// comboParity
			// 
			this.comboParity.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboParity.Location = new System.Drawing.Point(120, 104);
			this.comboParity.Name = "comboParity";
			this.comboParity.Size = new System.Drawing.Size(121, 21);
			this.comboParity.TabIndex = 3;
			// 
			// comboStopBit
			// 
			this.comboStopBit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboStopBit.Location = new System.Drawing.Point(120, 136);
			this.comboStopBit.Name = "comboStopBit";
			this.comboStopBit.Size = new System.Drawing.Size(121, 21);
			this.comboStopBit.TabIndex = 4;
			// 
			// comboHandShake
			// 
			this.comboHandShake.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboHandShake.Location = new System.Drawing.Point(120, 168);
			this.comboHandShake.Name = "comboHandShake";
			this.comboHandShake.Size = new System.Drawing.Size(121, 21);
			this.comboHandShake.TabIndex = 5;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 21);
			this.label1.TabIndex = 6;
			this.label1.Text = "Port: ";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 21);
			this.label2.TabIndex = 7;
			this.label2.Text = "Baud Rate:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 21);
			this.label3.TabIndex = 8;
			this.label3.Text = "Data Bit:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 104);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(100, 21);
			this.label5.TabIndex = 10;
			this.label5.Text = "Parity:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 136);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(100, 21);
			this.label6.TabIndex = 11;
			this.label6.Text = "Stop Bit:";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 168);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(100, 21);
			this.label4.TabIndex = 12;
			this.label4.Text = "Hand Shake:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(80, 200);
			this.button1.Name = "button1";
			this.button1.TabIndex = 13;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.commandOK);
			// 
			// commandCancel
			// 
			this.commandCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.commandCancel.Location = new System.Drawing.Point(168, 200);
			this.commandCancel.Name = "commandCancel";
			this.commandCancel.TabIndex = 14;
			this.commandCancel.Text = "Cancel";
			this.commandCancel.Click += new System.EventHandler(this.commandCancel_Click);
			// 
			// dlgCommPortSettings
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(248, 229);
			this.Controls.Add(this.commandCancel);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboHandShake);
			this.Controls.Add(this.comboStopBit);
			this.Controls.Add(this.comboParity);
			this.Controls.Add(this.comboDataBit);
			this.Controls.Add(this.comboBaudRate);
			this.Controls.Add(this.comboPort);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "dlgCommPortSettings";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "dlgCommPortSettings";
			this.Load += new System.EventHandler(this.dlgCommPortSettings_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void dlgCommPortSettings_Load(object sender, System.EventArgs e)
		{
			this.comboBaudRate.Items.AddRange(System.Enum.GetNames(typeof(BaudRates)));
			this.comboPort.Items.AddRange(System.Enum.GetNames(typeof(CommPorts)));
			this.comboDataBit.Items.AddRange(System.Enum.GetNames(typeof(DataBits)));
			this.comboHandShake.Items.AddRange(System.Enum.GetNames(typeof(HandShakes)));
			this.comboParity.Items.AddRange(System.Enum.GetNames(typeof(Parities)));
			this.comboStopBit.Items.AddRange(System.Enum.GetNames(typeof(StopBits)));
		}

		
		#endregion

		private void commandOK(object sender, System.EventArgs e)
		{
			bool validated = true;
			foreach ( Control ctl in this.Controls )
			{
				if ( ctl.GetType().Equals(typeof(ComboBox)) )
				{
					if ( ((ComboBox)ctl).SelectedItem == null )
					{
						ErrorHandler.GetInstance().LogException(true, "All Comm Port settings are required!");
						ctl.Focus();
						validated = false;
						break;
					}
				}
			}
			if ( validated ) 
			{
				this.Hide();
				this.DialogResult = DialogResult.OK;
			}
		}

		private void commandCancel_Click(object sender, System.EventArgs e)
		{
			this.Hide();
		}


		#region comm port settings

		public BaudRates BaudRate
		{
			get { return (BaudRates)System.Enum.Parse(typeof(BaudRates),(string)this.comboBaudRate.SelectedItem, true); }
			set {this.comboBaudRate.SelectedItem = value.ToString(); }
		}
		public CommPorts CommPort
		{
			get { return (CommPorts)System.Enum.Parse(typeof(CommPorts),(string)this.comboPort.SelectedItem, true); }
			set {this.comboPort.SelectedItem = value.ToString(); }
		}
		public DataBits DataBit
		{
			get { return (DataBits)System.Enum.Parse(typeof(DataBits),(string)this.comboDataBit.SelectedItem, true); }
			set {this.comboDataBit.SelectedItem = value.ToString(); }
		}
		public HandShakes HandShake
		{
			get { return (HandShakes)System.Enum.Parse(typeof(HandShakes),(string)this.comboHandShake.SelectedItem, true); }
			set {this.comboHandShake.SelectedItem = value.ToString(); }
		}
		public Parities Parity
		{
			get { return (Parities)System.Enum.Parse(typeof(Parities),(string)this.comboParity.SelectedItem, true); }
			set {this.comboParity.SelectedItem = value.ToString(); }
		}
		public StopBits StopBit
		{
			get { return (StopBits)System.Enum.Parse(typeof(StopBits),(string)this.comboStopBit.SelectedItem, true); }
			set {this.comboStopBit.SelectedItem = value.ToString(); }
		}

		#endregion


	}
}
