using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using vclass.network.server;
using OSProgress;
using System.Xml;
using vclass.chat;
using vclass.chat.client;
using vclass.chat.server;


namespace vclass.network.server
{
	/// <summary>
	/// Summary description for ServerForm.
	/// </summary>
	public class ServerForm : System.Windows.Forms.Form
	{
//		private System.Windows.Forms.ProgressBar serverProgressBar;
		private OSProgressBar serverProgressBar;
		private System.Windows.Forms.Button stopButton;
		private System.Windows.Forms.Button startButton;
		private ClassServer vcServer = null;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private vclass.chat.server.ChatServer cs;

		public ServerForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			vcServer = new ClassServer();
			//
			// 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.serverProgressBar = new OSProgress.OSProgressBar();
			this.stopButton = new System.Windows.Forms.Button();
			this.startButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// serverProgressBar
			// 
			this.serverProgressBar.AutoProgressSpeed = ((System.Byte)(10));
			this.serverProgressBar.BackColor = System.Drawing.SystemColors.Control;
			this.serverProgressBar.ForeColor = System.Drawing.SystemColors.ControlDark;
			this.serverProgressBar.Location = new System.Drawing.Point(-1, 24);
			this.serverProgressBar.Name = "serverProgressBar";
			this.serverProgressBar.Position = 2;
			this.serverProgressBar.ProgressBoxStyle = OSProgress.OSProgressBar.OSProgressBoxStyleConstants.osBOXAROUND;
			this.serverProgressBar.ProgressType = OSProgress.OSProgressBar.OSProgressTypeConstants.osGRAPHICTYPE;
			this.serverProgressBar.ShowBorder = false;
			this.serverProgressBar.Size = new System.Drawing.Size(282, 23);
			this.serverProgressBar.TabIndex = 0;
			// 
			// stopButton
			// 
			this.stopButton.Enabled = false;
			this.stopButton.Location = new System.Drawing.Point(142, 64);
			this.stopButton.Name = "stopButton";
			this.stopButton.Size = new System.Drawing.Size(120, 24);
			this.stopButton.TabIndex = 1;
			this.stopButton.Text = "Stop Server";
			this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
			// 
			// startButton
			// 
			this.startButton.Location = new System.Drawing.Point(8, 64);
			this.startButton.Name = "startButton";
			this.startButton.Size = new System.Drawing.Size(120, 24);
			this.startButton.TabIndex = 2;
			this.startButton.Text = "Start Server";
			this.startButton.Click += new System.EventHandler(this.startButton_Click);
			// 
			// ServerForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(272, 110);
			this.Controls.Add(this.startButton);
			this.Controls.Add(this.stopButton);
			this.Controls.Add(this.serverProgressBar);
			this.Name = "ServerForm";
			this.Text = "Server";
			this.ResumeLayout(false);

		}
		#endregion

		private void startButton_Click(object sender, System.EventArgs e)
		{
			vcServer.start();
			try
			{
				//			<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;

				cs = new ChatServer(Int32.Parse(port),1);
			}
			catch(Exception ex)
			{
				Console.WriteLine("Exception from serverform.chatserver "+ex);
			}
			startButton.Enabled = false;
			stopButton.Enabled = true;
			serverProgressBar.AutoProgress = true;
		}

		private void stopButton_Click(object sender, System.EventArgs e)
		{
			vcServer.stop();
			startButton.Enabled = true;
			stopButton.Enabled = false;
			serverProgressBar.AutoProgress = false;
		}
	}
}
