// ConnectForm.cs: set the remoting object paramter
// --------------------------------------------------------
// June 20,2004 
// By Xiaotao Shu.

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

using TrackingController;


namespace tools
{
	/// <summary>
	/// Summary description for connectForm.
	/// </summary>
	
	public class connectForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button cancelbutton;
		private System.Windows.Forms.Button okbutton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.CheckBox tcpCheckBox;
		private System.Windows.Forms.CheckBox httpCheckBox;
		private System.Windows.Forms.TextBox addressTextBox;
		private System.Windows.Forms.TextBox portTextBox;
	
		private Controller m_controller;               //controller 
      
	
		//constructor
		public connectForm(Controller aController)
		{
			m_controller = aController;
			InitializeComponent();   
			
			//set the connectform atrributes according by controller
			this.httpCheckBox.Checked = (m_controller.ChannelType == "HTTP"); 
			this.tcpCheckBox.Checked = (m_controller.ChannelType == "TCP");   
			if ( m_controller.Port >0 )
				this.portTextBox.Text = m_controller.Port.ToString();
			
			//get the IPaddress from the DNS
			IPHostEntry hostInfo = Dns.GetHostByName(Dns.GetHostName());
			string ipaddress = null;
			for (int i=0 ; i < hostInfo.AddressList.Length; i++)
				if (ipaddress != null)
					ipaddress+=";"+hostInfo.AddressList[i].ToString();
				else
					ipaddress = hostInfo.AddressList[i].ToString();
			//set the ip address Textbox
			this.addressTextBox.Text = ipaddress;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		//declaring controller property
		public Controller Controller
		{
			set	{	m_controller=value;	}
			get	{	return m_controller;	}
		}

			
		#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(connectForm));
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.httpCheckBox = new System.Windows.Forms.CheckBox();
			this.tcpCheckBox = new System.Windows.Forms.CheckBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.portTextBox = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.addressTextBox = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.cancelbutton = new System.Windows.Forms.Button();
			this.okbutton = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.pictureBox1);
			this.groupBox1.Controls.Add(this.httpCheckBox);
			this.groupBox1.Controls.Add(this.tcpCheckBox);
			this.groupBox1.Location = new System.Drawing.Point(16, 88);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(304, 72);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Protocol Type";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(56, 32);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(24, 24);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 2;
			this.pictureBox1.TabStop = false;
			// 
			// httpCheckBox
			// 
			this.httpCheckBox.Location = new System.Drawing.Point(176, 48);
			this.httpCheckBox.Name = "httpCheckBox";
			this.httpCheckBox.Size = new System.Drawing.Size(80, 16);
			this.httpCheckBox.TabIndex = 1;
			this.httpCheckBox.Text = "HTTP";
			this.httpCheckBox.CheckedChanged += new System.EventHandler(this.httpCheckBox_CheckedChanged);
			// 
			// tcpCheckBox
			// 
			this.tcpCheckBox.Checked = true;
			this.tcpCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
			this.tcpCheckBox.Location = new System.Drawing.Point(176, 24);
			this.tcpCheckBox.Name = "tcpCheckBox";
			this.tcpCheckBox.Size = new System.Drawing.Size(80, 16);
			this.tcpCheckBox.TabIndex = 0;
			this.tcpCheckBox.Text = "TCP";
			this.tcpCheckBox.CheckedChanged += new System.EventHandler(this.tcpCheckBox_CheckedChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.portTextBox);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.addressTextBox);
			this.groupBox2.Controls.Add(this.label1);
			this.groupBox2.Location = new System.Drawing.Point(16, 16);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(400, 56);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Server";
			// 
			// portTextBox
			// 
			this.portTextBox.Location = new System.Drawing.Point(288, 24);
			this.portTextBox.Name = "portTextBox";
			this.portTextBox.Size = new System.Drawing.Size(88, 20);
			this.portTextBox.TabIndex = 3;
			this.portTextBox.Text = "4000";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(256, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Port:";
			// 
			// addressTextBox
			// 
			this.addressTextBox.Enabled = false;
			this.addressTextBox.Location = new System.Drawing.Point(72, 24);
			this.addressTextBox.Name = "addressTextBox";
			this.addressTextBox.Size = new System.Drawing.Size(168, 20);
			this.addressTextBox.TabIndex = 1;
			this.addressTextBox.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address:";
			// 
			// cancelbutton
			// 
			this.cancelbutton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cancelbutton.Location = new System.Drawing.Point(344, 136);
			this.cancelbutton.Name = "cancelbutton";
			this.cancelbutton.TabIndex = 2;
			this.cancelbutton.Text = "Cancel";
			this.cancelbutton.Click += new System.EventHandler(this.cancelbutton_Click);
			// 
			// okbutton
			// 
			this.okbutton.Location = new System.Drawing.Point(344, 96);
			this.okbutton.Name = "okbutton";
			this.okbutton.TabIndex = 3;
			this.okbutton.Text = "Ok";
			this.okbutton.Click += new System.EventHandler(this.okbutton_Click);
			// 
			// connectForm
			// 
			this.AcceptButton = this.okbutton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cancelbutton;
			this.ClientSize = new System.Drawing.Size(434, 178);
			this.Controls.Add(this.okbutton);
			this.Controls.Add(this.cancelbutton);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "connectForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "connect parameter setting";
			this.TopMost = true;
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		private void httpCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			if (httpCheckBox.Checked)
			{
				tcpCheckBox.Checked = false;
				this.portTextBox.Text = "80";
			}
		}

		private void tcpCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			if (tcpCheckBox.Checked)
			{
				httpCheckBox.Checked = false;
				if (this.portTextBox.Text == "80")
					this.portTextBox.Text = "4000";
			}
		}

		private void okbutton_Click(object sender, System.EventArgs e)
		{
			//update connection parameter
			if ( httpCheckBox.Checked)
				this.Controller.ChannelType = "HTTP";
			else
				this.Controller.ChannelType = "TCP";
			
			this.Controller.Port = Convert.ToInt32(portTextBox.Text);
			this.Hide();
		}

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