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



namespace vclass.chat
{
	/// <summary>
	/// Summary description for chatPanel.
	/// </summary>
	public class chatPanel : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TextBox TBChat;
		private Hashtable tabPages;
		private System.Windows.Forms.Button BColor;
		private System.Windows.Forms.Button BFont;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button BSend;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.FontDialog fontDialog1;
		private string tabName = "";
		private Color color;
		private Font font;

		public chatPanel()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			Application.DoEvents();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			tabPages = new Hashtable();
			color = Color.Black;
			font = new Font(FontFamily.GenericSerif,8);
		//	addTab2("Public");
			
		}
		public int addTab(string name)
		{
			this.tabName = name;
			try
			{
				this.Invoke(new EventHandler(appViewerActionPerformed));
			}
			catch(Exception ex)
			{
				Console.WriteLine("Chatpanel->addtab2->exception: "+ex);
			}
			return 1;
		}
		public void appViewerActionPerformed(object sender, EventArgs e)
		{
			
			this.addActualTab(tabName);

		}
		private int addActualTab(string name)
		{
			Console.WriteLine("ChatPanel->addtab: "+name);

			if(!tabPages.ContainsKey(name))
			{
			//	Console.WriteLine("the tab do not contain the key2");
				tabPage tabpage = new vclass.chat.tabPage(name);
			//	Console.WriteLine("the tab do not contain the key3");
				try
				{
					tabControl1.Controls.Add(tabpage);
				}
				catch(Exception ex)
				{
					Console.WriteLine("chatpanel->addtab->exception: "+ex);
				}
			//	Console.WriteLine("the tab do not contain the key4");
				tabPages.Add(name,tabpage);
			//	Console.WriteLine("the tab do not contain the key5");
				tabControl1.SelectedTab = (tabPage)this.tabPages[name];
			//	Console.WriteLine("the tab do not contain the key6");
				//	Console.WriteLine("chatPanel->addTab : "+name+" tab is added"); 
				return 1;
			}
			tabControl1.SelectedTab = (tabPage)this.tabPages[name];
			return 0;
		}

		public int removeTab(string name)
		{
			if(tabPages.ContainsKey(name))
			{
				tabPage tabpage = (tabPage)tabPages[name];
				tabPages.Remove(name);
				tabControl1.Controls.Remove(tabpage);
			//	Console.WriteLine("chatPanel->removeTab : "+name+" tab is removed");
			}
			return 1;
		}
		public void append(string tabname,string message,string color,string font)
		{
			Console.WriteLine("chatpanel->append->entered");
			
				
			if(!tabPages.ContainsKey(tabname))
			{
		//		Console.WriteLine("the tab do not contain the key");
				this.addTab(tabname);
		//		Console.WriteLine("chatpanel->append->newtab: "+tabname);
			//	tabControl1.SelectedTab = (tabPage)this.tabPages[tabname];
				((tabPage)this.tabPages[tabname]).Add(message,color,font);
			}
			else
			{
				((tabPage)this.tabPages[tabname]).Add(message,color,font);
			//	tabControl1.SelectedTab = (tabPage)this.tabPages[tabname];
			}
			
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				((MainForm)(this.MdiParent)).clearChatPanel();
				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(chatPanel));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.TBChat = new System.Windows.Forms.TextBox();
			this.BSend = new System.Windows.Forms.Button();
			this.BColor = new System.Windows.Forms.Button();
			this.BFont = new System.Windows.Forms.Button();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
			this.tabControl1.Location = new System.Drawing.Point(0, 0);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(488, 192);
			this.tabControl1.TabIndex = 0;
			// 
			// TBChat
			// 
			this.TBChat.Location = new System.Drawing.Point(0, 192);
			this.TBChat.MaxLength = 350;
			this.TBChat.Name = "TBChat";
			this.TBChat.Size = new System.Drawing.Size(488, 20);
			this.TBChat.TabIndex = 1;
			this.TBChat.Text = "";
			this.TBChat.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TBChat_KeyDown);
			// 
			// BSend
			// 
			this.BSend.BackColor = System.Drawing.SystemColors.Control;
			this.BSend.Location = new System.Drawing.Point(496, 144);
			this.BSend.Name = "BSend";
			this.BSend.Size = new System.Drawing.Size(40, 64);
			this.BSend.TabIndex = 3;
			this.BSend.Text = "Send";
			this.BSend.Click += new System.EventHandler(this.BSend_Click);
			// 
			// BColor
			// 
			this.BColor.BackColor = System.Drawing.SystemColors.Control;
			this.BColor.Image = ((System.Drawing.Image)(resources.GetObject("BColor.Image")));
			this.BColor.Location = new System.Drawing.Point(496, 16);
			this.BColor.Name = "BColor";
			this.BColor.Size = new System.Drawing.Size(40, 56);
			this.BColor.TabIndex = 6;
			this.BColor.Click += new System.EventHandler(this.BColor_Click);
			// 
			// BFont
			// 
			this.BFont.BackColor = System.Drawing.SystemColors.Control;
			this.BFont.Image = ((System.Drawing.Image)(resources.GetObject("BFont.Image")));
			this.BFont.Location = new System.Drawing.Point(496, 80);
			this.BFont.Name = "BFont";
			this.BFont.Size = new System.Drawing.Size(40, 56);
			this.BFont.TabIndex = 7;
			this.BFont.Click += new System.EventHandler(this.BFont_Click);
			// 
			// chatPanel
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Desktop;
			this.ClientSize = new System.Drawing.Size(544, 214);
			this.Controls.Add(this.BFont);
			this.Controls.Add(this.BColor);
			this.Controls.Add(this.BSend);
			this.Controls.Add(this.TBChat);
			this.Controls.Add(this.tabControl1);
			this.Name = "chatPanel";
			this.Text = "chatPanel";
			this.ResumeLayout(false);

		}
		#endregion

		// Handle the KeyDown event to determine the type of character entered into the control.
		private void TBChat_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				if(TBChat.Text != "")
				{
					string message = "<command><message><from>"+((MainForm)(this.MdiParent)).getChatClient().getUsername()+"</from><to>"+this.tabControl1.SelectedTab.Name.ToString()+"</to><body>"+TBChat.Text.ToString()+"</body><color>"+color.ToArgb()+"</color><font>"+font.ToHfont().ToInt32().ToString()+"</font></message></command>";
					((MainForm)this.MdiParent).getChatClient().Write(message);
					TBChat.Clear();
				}
			}
		}

		private void BColor_Click(object sender, System.EventArgs e)
		{
			
			this.colorDialog1.ShowDialog();
			color = this.colorDialog1.Color;
			
		//	Console.WriteLine("chatpanel->colorButton: "+color.Name);
		}

		private void BFont_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.ShowDialog();
			font = this.fontDialog1.Font;
		}

		private void BSend_Click(object sender, System.EventArgs e)
		{
			if(TBChat.Text != "")
			{
				string message = "<command><message><from>"+((MainForm)(this.MdiParent)).getChatClient().getUsername()+"</from><to>"+this.tabControl1.SelectedTab.Name.ToString()+"</to><body>"+TBChat.Text.ToString()+"</body><color>"+color.ToArgb()+"</color><font>"+font.ToHfont().ToInt32().ToString()+"</font></message></command>";
				((MainForm)this.MdiParent).getChatClient().Write(message);
				TBChat.Clear();
			}
		}
		//END key down
	}
}
