using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using vclass;
using vclass.util;
using vclass.appsharing;
using vclass.outline;
using vclass.presentation;
using vclass.network.client;
using vclass.network.server;
using vclass.broadcast;
using vclass.broadcast.client;
using vclass.whiteboard;
using vclass.chat;
using vclass.chat.client;

namespace vclass
{
	/// <summary>
	/// A summary will be provided soon.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuFile;
		private System.Windows.Forms.MenuItem menuFileNew;
		private System.Windows.Forms.MenuItem menuFileClose;
		private System.Windows.Forms.MenuItem menuWindow;
		private System.Windows.Forms.MenuItem menuView;
		private System.Windows.Forms.MenuItem menuViewVideo;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
		private ScreenCapturer capturer = null;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private ClassClient vcClient = null;

//		private SynchronousSocketClient client = null;
		private System.Windows.Forms.MenuItem menuItem9;
		private vclass.network.client.LoginForm loginForm = null;
		private ServerForm serverForm = null;
		private System.Windows.Forms.MenuItem menuItem10;
		private ClassServer vcServer = null;
		private vclass.broadcast.Form1 broadcastClient = null;
		private ApplicationViewer appViewer = new ApplicationViewer();
		private Image appImage = null;
		private Image slideImage = null;
//		private SynchronousSocketListener server = null;

		private LoginForm loginform = null;
//		private ServerForm serverForm = null;
		//		private ClassServer vcServer = null;
		//		private SynchronousSocketListener server = null;
		private ChatClient chatclient = null;
		private chatPanel chatpanel = null;
		private userList userlist = null;
		public int server = 0,client = 0;
		private PresentationForm newPresentationForm = null;
		private System.Windows.Forms.MenuItem menuItem11;
		//-------------whiteboard--------------
		public WhiteBoard whiteboard = null;
		
		public int setChatClient(ChatClient chatclient)
		{
			this.chatclient = chatclient;
			return 1;
		}

		public ChatClient getChatClient()
		{
			return this.chatclient;
		}
		//-------------userlist---------------
		public int setUserList(userList userlist)
		{
			this.userlist = userlist;	
			return 1;
		}

		public userList getUserList()
		{
			return this.userlist;
		}
		//---------------chatpanel-----------------
		public chatPanel getChatPanel()
		{
			return this.chatpanel;
		}
				public int clearChatPanel()
		{
			this.chatpanel = null;
			this.chatclient.setChatpanel(null);
			return 1;
		}
		
		public int setChatPanel(chatPanel chatpanel)
		{
			this.chatpanel = chatpanel;
			return 1;
		}

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			EnvironmentController eControl = new EnvironmentController();
			eControl.MdiParent = this;
			eControl.Show();
			eControl.SetBounds(0, 
								this.Size.Height - eControl.Size.Height,
								eControl.Size.Width, eControl.Size.Height);
			eControl.Validate();

			appViewer.MdiParent = this;
			Application.DoEvents();
			//
			// 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 );
			if(server == 1)
			{
				//do the logout of the server
				//this will logout all the users from the server by itself
				;
			}
			if(client == 1)
			{
				//logout client from all clients userlist(send to server first)
				chatclient.Write("<command><logout><username>"+chatclient.getUsername()+"</username></logout></command>");
			}

			Application.Exit();
			Environment.Exit(0);
		}

		#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.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuFileNew = new System.Windows.Forms.MenuItem();
			this.menuFileClose = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuView = new System.Windows.Forms.MenuItem();
			this.menuViewVideo = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuWindow = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuFile,
																					  this.menuView,
																					  this.menuWindow});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuFileNew,
																					 this.menuFileClose,
																					 this.menuItem7,
																					 this.menuItem8});
			this.menuFile.Text = "&File";
			// 
			// menuFileNew
			// 
			this.menuFileNew.Index = 0;
			this.menuFileNew.Text = "&New";
			this.menuFileNew.Click += new System.EventHandler(this.FormOnClick);
			// 
			// menuFileClose
			// 
			this.menuFileClose.Index = 1;
			this.menuFileClose.Text = "&Close";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 2;
			this.menuItem7.Text = "Start Server";
			this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 3;
			this.menuItem8.Text = "Connect";
			this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
			// 
			// menuView
			// 
			this.menuView.Index = 1;
			this.menuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuViewVideo,
																					 this.menuItem1,
																					 this.menuItem2,
																					 this.menuItem3,
																					 this.menuItem4,
																					 this.menuItem5,
																					 this.menuItem6,
																					 this.menuItem9,
																					 this.menuItem10,
																					 this.menuItem11});
			this.menuView.Text = "&View";
			// 
			// menuViewVideo
			// 
			this.menuViewVideo.Index = 0;
			this.menuViewVideo.Text = "&Video";
			this.menuViewVideo.Click += new System.EventHandler(this.ViewVideoOnClick);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "Presentation";
			this.menuItem1.Click += new System.EventHandler(this.ViewPresentationOnClick);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 2;
			this.menuItem2.Text = "Tools";
			this.menuItem2.Click += new System.EventHandler(this.ToolsOnClick);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 3;
			this.menuItem3.Text = "Chat";
			this.menuItem3.Click += new System.EventHandler(this.ChatOnClick);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 4;
			this.menuItem4.Text = "Outline";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 5;
			this.menuItem5.Text = "ApplicationSharing";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 6;
			this.menuItem6.Text = "Presenter Tools";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 7;
			this.menuItem9.Text = "Whiteboard";
			this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 8;
			this.menuItem10.Text = "Broadcast";
			this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
			// 
			// menuWindow
			// 
			this.menuWindow.Index = 2;
			this.menuWindow.MdiList = true;
			this.menuWindow.Text = "&Window";
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 9;
			this.menuItem11.Text = "Video Previewer";
			this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 645);
			this.IsMdiContainer = true;
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "CL@SS++ - Virtual Classroom - profIT";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.MainForm_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void FormOnClick(object sender, System.EventArgs e)
		{
			ChildForm1 newMDIChild = new ChildForm1();
			// Set the Parent Form of the Child window.
			newMDIChild.MdiParent = this;
			// Display the new form.
			newMDIChild.Show();
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
		
		}

		private void ViewVideoOnClick(object sender, System.EventArgs e)
		{
			/*
			VideoForm newVideoForm = new VideoForm();
			newVideoForm.MdiParent = this;
			newVideoForm.Show();
			*/
		}

		private void ViewPresentationOnClick(object sender, System.EventArgs e)
		{
			newPresentationForm = new PresentationForm(this);
			newPresentationForm.MdiParent = this;
			newPresentationForm.Show();
		}

		private void ToolsOnClick(object sender, System.EventArgs e)
		{
			StudentToolsForm newToolsForm = new StudentToolsForm();
			newToolsForm.MdiParent = this;
			newToolsForm.Show();
		}

		private void ChatOnClick(object sender, System.EventArgs e)
		{
			if(chatpanel == null)
			{
				chatpanel = new chatPanel();
				chatpanel.MdiParent = this;
				chatpanel.addTab("Public");
				//this sets tha chatpanel into chatclient
				// so it is possible to reach the chatpanel.append from chatclient
				this.chatclient.setChatpanel(chatpanel);
				chatpanel.Show();
			}
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			OutlineFrame outline = new OutlineFrame();
			outline.MdiParent = this;
			outline.Show();
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			ApplicationShareForm appSharing = new ApplicationShareForm(this);
			appSharing.MdiParent = this;
			appSharing.Show();
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			PresenterTools tools = new PresenterTools();
			tools.MdiParent = this;
			tools.Show();
		}

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			//server = new SynchronousSocketListener();
//			server.StartListening();
			if(serverForm == null)
				serverForm = new ServerForm();
			serverForm.MdiParent = this;
			serverForm.Show();
		}

		private void menuItem8_Click(object sender, System.EventArgs e)
		{
			//client = new SynchronousSocketClient();
			//client.StartClient();
			loginForm = new LoginForm(this);
			loginForm.MdiParent = this;
			loginForm.Show();
		}

		private void menuItem9_Click(object sender, System.EventArgs e)
		{
			whiteboard = new WhiteBoard();
			whiteboard.MdiParent = this;
			if(this.getChatClient() != null)
			{
				this.getChatClient().setWhiteboard(whiteboard);
			}
			whiteboard.Show();
		}

		public static Object getConfigurationItem(String configName){
			
			return "";
		} 
		
		public ClassClient getClient(){
			return vcClient;
		}

		public void setClient(ClassClient client){
			vcClient = client;
		}

		public ScreenCapturer getCapturer()
		{
			if(capturer == null)
				capturer = new ScreenCapturer();
			return capturer;
		}

		public void setCapturer(ScreenCapturer sc)
		{
			capturer = sc;
		}

		private void menuItem10_Click(object sender, System.EventArgs e)
		{
			broadcastClient = new vclass.broadcast.Form1();
			broadcastClient.MdiParent = this;
			broadcastClient.Show();
		}
	
		public ApplicationViewer getAppViewer(){
			if(appViewer == null){
				appViewer = new ApplicationViewer();
			}
			return appViewer;
		}

		public void setAppViewer(ApplicationViewer viewer){
			appViewer = viewer;
		}

		public void setAppPicture(Image pic)
		{
			appImage = pic;
			this.appViewer.Invoke(new EventHandler(appViewerActionPerformed));
		}

		public void appViewerActionPerformed(object sender, EventArgs e){
			appViewer.setPicture(appImage);
			appViewer.Update();
		}

		public void updateSlide(Image pic){
			slideImage = pic;
			this.appViewer.Invoke(new EventHandler(slideActionPerformed));
		}

		public void slideActionPerformed(object sender, EventArgs e)
		{
			if(newPresentationForm == null)
			{
				newPresentationForm = new PresentationForm(this);
				newPresentationForm.MdiParent = this;
				newPresentationForm.Show();
			}
			newPresentationForm.setPicture(slideImage);
		}

		private void menuItem11_Click(object sender, System.EventArgs e)
		{
			clientForm cform = new clientForm();
			cform.MdiParent = this;
			cform.Show();
		}
	}
}
