using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using vclass.util;
using vclass.chat;
using vclass.chat.client;
using vclass.chat.server;
using System.Xml;
using System.Net;

namespace vclass.network.client
{
	/// <summary>
	/// Summary description for loginForm.
	/// </summary>
	public class LoginForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox userIDTextBox;
		private System.Windows.Forms.TextBox passwordTextBox;
		private System.Windows.Forms.Label userIDlabel;
		private System.Windows.Forms.Label passwordlabel;
		private System.Windows.Forms.Button loginButton;
		private System.Windows.Forms.Button registerButton;
		private System.Windows.Forms.ComboBox typeComboBox;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private userList userlist = null;
		private MainForm mainform = null;

		public LoginForm(MainForm mf)
		{
			this.mainform = mf;
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			typeComboBox.SelectedIndex = 0;

			//
			// 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.userIDTextBox = new System.Windows.Forms.TextBox();
			this.passwordTextBox = new System.Windows.Forms.TextBox();
			this.userIDlabel = new System.Windows.Forms.Label();
			this.passwordlabel = new System.Windows.Forms.Label();
			this.loginButton = new System.Windows.Forms.Button();
			this.registerButton = new System.Windows.Forms.Button();
			this.typeComboBox = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// userIDTextBox
			// 
			this.userIDTextBox.Location = new System.Drawing.Point(120, 24);
			this.userIDTextBox.Name = "userIDTextBox";
			this.userIDTextBox.TabIndex = 0;
			this.userIDTextBox.Text = "";
			// 
			// passwordTextBox
			// 
			this.passwordTextBox.Location = new System.Drawing.Point(120, 56);
			this.passwordTextBox.Name = "passwordTextBox";
			this.passwordTextBox.PasswordChar = '*';
			this.passwordTextBox.TabIndex = 1;
			this.passwordTextBox.Text = "";
			// 
			// userIDlabel
			// 
			this.userIDlabel.Location = new System.Drawing.Point(40, 24);
			this.userIDlabel.Name = "userIDlabel";
			this.userIDlabel.Size = new System.Drawing.Size(80, 24);
			this.userIDlabel.TabIndex = 2;
			this.userIDlabel.Text = "user ID";
			// 
			// passwordlabel
			// 
			this.passwordlabel.Location = new System.Drawing.Point(40, 56);
			this.passwordlabel.Name = "passwordlabel";
			this.passwordlabel.Size = new System.Drawing.Size(80, 23);
			this.passwordlabel.TabIndex = 3;
			this.passwordlabel.Text = "password";
			// 
			// loginButton
			// 
			this.loginButton.Location = new System.Drawing.Point(144, 128);
			this.loginButton.Name = "loginButton";
			this.loginButton.TabIndex = 4;
			this.loginButton.Text = "Login";
			this.loginButton.Click += new System.EventHandler(this.loginButton_Click);
			// 
			// registerButton
			// 
			this.registerButton.Location = new System.Drawing.Point(32, 128);
			this.registerButton.Name = "registerButton";
			this.registerButton.TabIndex = 5;
			this.registerButton.Text = "Register";
			// 
			// typeComboBox
			// 
			this.typeComboBox.Items.AddRange(new object[] {
															  "Participant",
															  "Presenter"});
			this.typeComboBox.Location = new System.Drawing.Point(120, 88);
			this.typeComboBox.Name = "typeComboBox";
			this.typeComboBox.Size = new System.Drawing.Size(104, 21);
			this.typeComboBox.TabIndex = 6;
			// 
			// LoginForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(248, 166);
			this.Controls.Add(this.typeComboBox);
			this.Controls.Add(this.registerButton);
			this.Controls.Add(this.loginButton);
			this.Controls.Add(this.passwordlabel);
			this.Controls.Add(this.userIDlabel);
			this.Controls.Add(this.passwordTextBox);
			this.Controls.Add(this.userIDTextBox);
			this.Name = "LoginForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "login";
			this.ResumeLayout(false);

		}
		#endregion

		private void loginButton_Click(object sender, System.EventArgs e)
		{
			ClassClient vcClient = ((MainForm)this.MdiParent).getClient();
			if(vcClient == null)
			{
				vcClient = new ClassClient((MainForm)this.MdiParent);
				((MainForm)this.MdiParent).setClient(vcClient);
			}
			if(vcClient.connect(userIDTextBox.Text, passwordTextBox.Text) == 0)
			{
				//invalid password
			}
			else
			{
				//connection established;
				this.Dispose();
			}
			ChatClient c;
			try
			{
				//--------------ChatClient----------------

				//			<configure>
				//				<ip>127.0.0.1</ip>
				//				<port>13000</port>
				//			</configure>
				XmlDocument doc = new XmlDocument();
				XmlTextReader reader = new XmlTextReader("C:\\configure.xml");
				doc.Load(reader);
				XmlNodeList list = doc.GetElementsByTagName("configure");
				
				string ip = list[0].ChildNodes[0].InnerText;
				string port = list[0].ChildNodes[1].InnerText;

				chatclient = new ChatClient(ip,Int32.Parse(port));
				//This is for stressing that this mainMenu application is used for client	
				((MainForm)(this.MdiParent)).client = 1;

				mainform.setChatClient(chatclient);
				//it enables to set the username and password for Chatclient

				//--------------Authentication------------
				// TODO : do the authentication

				chatclient.login(userIDTextBox.Text, passwordTextBox.Text);
				
				//--------------------userlist--------------
				userlist = new userList(mainform);
				userlist.MdiParent = mainform;
				mainform.setUserList(userlist);

				userlist.setUserHeader(userIDTextBox.Text);
				//it enables for me to reach the userlist form from Chatclient
				chatclient.setUserlist(userlist);				//		c.setChatpanel(((MainForm)this.MdiParent).getChatPanel());
					
				
			}

			catch (Exception ex)
			{
				Console.WriteLine("Exception in login of Chatclient "+ex);
			}
			this.Dispose();
			userlist.Show();
		}
	}
}
