using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using vclass.network.client;
using System.IO;

namespace vclass.appsharing
{
	/// <summary>
	/// Summary description for ApplicationShareForm.
	/// </summary>
	public class ApplicationShareForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.NumericUpDown numericUpDown1;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Diagnostics.Process selectedProcess;
		private System.Diagnostics.Process[] localProcesses;
		private int[] windowProcesses;
		private System.Windows.Forms.TextBox posYTextBox;
		private System.Windows.Forms.TextBox posXTextBox;
		private System.Windows.Forms.TextBox fromYTextBox;
		private System.Windows.Forms.TextBox toXTextBox;
		private System.Windows.Forms.TextBox fromXTextBox;
		private System.Windows.Forms.TextBox toYTextBox;
		private int moveStartX = 0 , moveStartY = 0;
		private int moveEndX = 0 , moveEndY = 0;
		private double desktopResolutionX = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width / 100.0;
		private double desktopResolutionY = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height / 100.0;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ApplicationShareForm(MainForm mf)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			selectedProcess = System.Diagnostics.Process.GetCurrentProcess();
			localProcesses = System.Diagnostics.Process.GetProcesses();

			listProcesses();
			ScreenCapturer sc = mf.getCapturer();
			if(sc == null) Console.WriteLine("sc is null");
			Image img = sc.captureScreen("c:/desktop.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
			if(img == null) Console.WriteLine("img is null");
			panel1.BackgroundImage = img.GetThumbnailImage(panel1.Width, panel1.Height, null, IntPtr.Zero);
		}

		private void listProcesses()
		{
//			Control cont = this.MdiParent.ActiveControl;
//			cont.GetNextControl(cont, true);
			int selected = 0;
			windowProcesses = new int[localProcesses.Length];
			for(int i = 0;i < localProcesses.Length;i++)
				if(localProcesses[i].MainWindowHandle != IntPtr.Zero)
				{
					listBox1.Items.Add(localProcesses[i].MainWindowTitle);
					windowProcesses[selected++] = i;
				}													 
		}

		/// <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.listBox1 = new System.Windows.Forms.ListBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.button2 = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.posYTextBox = new System.Windows.Forms.TextBox();
			this.posXTextBox = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.fromYTextBox = new System.Windows.Forms.TextBox();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.label5 = new System.Windows.Forms.Label();
			this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.toXTextBox = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.fromXTextBox = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.toYTextBox = new System.Windows.Forms.TextBox();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(8, 40);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(112, 82);
			this.listBox1.TabIndex = 0;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.button1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.listBox1);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(128, 168);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Application";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 128);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(112, 24);
			this.button1.TabIndex = 4;
			this.button1.Text = "Select";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.TabIndex = 1;
			this.label1.Text = "Select Process";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(104, 20);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "<selected-process>";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 16);
			this.label2.Name = "label2";
			this.label2.TabIndex = 3;
			this.label2.Text = "Selected Process";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.button2);
			this.groupBox2.Controls.Add(this.textBox1);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Location = new System.Drawing.Point(144, 72);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(120, 104);
			this.groupBox2.TabIndex = 4;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Sharing";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(8, 72);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(104, 23);
			this.button2.TabIndex = 4;
			this.button2.Text = "Share";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.posYTextBox);
			this.groupBox3.Controls.Add(this.posXTextBox);
			this.groupBox3.Controls.Add(this.label4);
			this.groupBox3.Controls.Add(this.label3);
			this.groupBox3.Location = new System.Drawing.Point(144, 8);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(120, 64);
			this.groupBox3.TabIndex = 5;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Position";
			this.groupBox3.Enter += new System.EventHandler(this.groupBox3_Enter);
			// 
			// posYTextBox
			// 
			this.posYTextBox.Location = new System.Drawing.Point(40, 40);
			this.posYTextBox.Name = "posYTextBox";
			this.posYTextBox.Size = new System.Drawing.Size(72, 20);
			this.posYTextBox.TabIndex = 6;
			this.posYTextBox.Text = "0";
			// 
			// posXTextBox
			// 
			this.posXTextBox.Location = new System.Drawing.Point(40, 16);
			this.posXTextBox.Name = "posXTextBox";
			this.posXTextBox.Size = new System.Drawing.Size(72, 20);
			this.posXTextBox.TabIndex = 2;
			this.posXTextBox.Text = "0";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 40);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 23);
			this.label4.TabIndex = 1;
			this.label4.Text = "Y";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 23);
			this.label3.TabIndex = 0;
			this.label3.Text = "X";
			// 
			// fromYTextBox
			// 
			this.fromYTextBox.Location = new System.Drawing.Point(48, 56);
			this.fromYTextBox.Name = "fromYTextBox";
			this.fromYTextBox.Size = new System.Drawing.Size(40, 20);
			this.fromYTextBox.TabIndex = 4;
			this.fromYTextBox.Text = "0";
			this.fromYTextBox.TextChanged += new System.EventHandler(this.updatePanel2);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.panel1);
			this.groupBox4.Controls.Add(this.label5);
			this.groupBox4.Controls.Add(this.numericUpDown1);
			this.groupBox4.Controls.Add(this.radioButton2);
			this.groupBox4.Controls.Add(this.radioButton1);
			this.groupBox4.Controls.Add(this.label9);
			this.groupBox4.Controls.Add(this.label8);
			this.groupBox4.Controls.Add(this.toXTextBox);
			this.groupBox4.Controls.Add(this.label7);
			this.groupBox4.Controls.Add(this.fromXTextBox);
			this.groupBox4.Controls.Add(this.label6);
			this.groupBox4.Controls.Add(this.toYTextBox);
			this.groupBox4.Controls.Add(this.fromYTextBox);
			this.groupBox4.Location = new System.Drawing.Point(272, 8);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(272, 136);
			this.groupBox4.TabIndex = 7;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "View";
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.panel1.Controls.Add(this.panel2);
			this.panel1.Location = new System.Drawing.Point(156, 21);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(100, 100);
			this.panel1.TabIndex = 17;
			this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
			this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
			// 
			// panel2
			// 
			this.panel2.BackColor = System.Drawing.Color.Red;
			this.panel2.Location = new System.Drawing.Point(0, 0);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(8, 8);
			this.panel2.TabIndex = 0;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(120, 107);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(16, 23);
			this.label5.TabIndex = 16;
			this.label5.Text = "%";
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Location = new System.Drawing.Point(72, 104);
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(48, 20);
			this.numericUpDown1.TabIndex = 8;
			this.numericUpDown1.Value = new System.Decimal(new int[] {
																		 100,
																		 0,
																		 0,
																		 0});
			this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(8, 104);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.TabIndex = 15;
			this.radioButton2.Text = "Percent";
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new System.Drawing.Point(8, 80);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.TabIndex = 14;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "Actual";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 56);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(32, 23);
			this.label9.TabIndex = 13;
			this.label9.Text = "y";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 32);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(32, 23);
			this.label8.TabIndex = 12;
			this.label8.Text = "x";
			// 
			// toXTextBox
			// 
			this.toXTextBox.Location = new System.Drawing.Point(96, 32);
			this.toXTextBox.Name = "toXTextBox";
			this.toXTextBox.Size = new System.Drawing.Size(40, 20);
			this.toXTextBox.TabIndex = 6;
			this.toXTextBox.Text = "0";
			this.toXTextBox.TextChanged += new System.EventHandler(this.updatePanel2);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(96, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(40, 23);
			this.label7.TabIndex = 11;
			this.label7.Text = "To";
			// 
			// fromXTextBox
			// 
			this.fromXTextBox.Location = new System.Drawing.Point(48, 32);
			this.fromXTextBox.Name = "fromXTextBox";
			this.fromXTextBox.Size = new System.Drawing.Size(40, 20);
			this.fromXTextBox.TabIndex = 2;
			this.fromXTextBox.Text = "0";
			this.fromXTextBox.TextChanged += new System.EventHandler(this.updatePanel2);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(48, 15);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(40, 23);
			this.label6.TabIndex = 10;
			this.label6.Text = "From";
			// 
			// toYTextBox
			// 
			this.toYTextBox.Location = new System.Drawing.Point(96, 56);
			this.toYTextBox.Name = "toYTextBox";
			this.toYTextBox.Size = new System.Drawing.Size(40, 20);
			this.toYTextBox.TabIndex = 5;
			this.toYTextBox.Text = "0";
			this.toYTextBox.TextChanged += new System.EventHandler(this.updatePanel2);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(272, 152);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(72, 24);
			this.button3.TabIndex = 8;
			this.button3.Text = "OK";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(352, 152);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(64, 23);
			this.button4.TabIndex = 9;
			this.button4.Text = "Cancel";
			// 
			// ApplicationShareForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 182);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "ApplicationShareForm";
			this.Text = "ApplicationShareForm";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

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

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			System.Diagnostics.Process selectedProcess = localProcesses[windowProcesses[ listBox1.SelectedIndex]];
			textBox1.Text = selectedProcess.MainWindowTitle;
			IntPtr winPtr = selectedProcess.MainWindowHandle;
			Control winCntr = Control.FromHandle(winPtr);
			if(winPtr.Equals(null)){
				
				return;
			}
			posXTextBox.Text = winCntr.Location.X.ToString();
			posYTextBox.Text = winCntr.Location.Y.ToString();
			fromXTextBox.Text = posXTextBox.Text;
			fromYTextBox.Text = posYTextBox.Text;
			toXTextBox.Text = winCntr.Size.Width.ToString();
			toYTextBox.Text = winCntr.Size.Height.ToString();
			updatePanel2();
		}

		void updatePanel2(object sender, System.EventArgs e)
		{
			if(fromXTextBox.Text == "" ||
				fromYTextBox.Text == "" ||
				toXTextBox.Text == "" ||
				toYTextBox.Text == "")
			{
				Console.WriteLine("empty");
			}
			else
			{
				panel2.Bounds = new Rectangle(new Point(Convert.ToInt32(Convert.ToInt32(fromXTextBox.Text) / desktopResolutionX), Convert.ToInt32(Convert.ToInt32(fromYTextBox.Text) / desktopResolutionY)),
					new Size(Convert.ToInt32(Convert.ToInt32(toXTextBox.Text) / desktopResolutionX), Convert.ToInt32(Convert.ToInt32(toYTextBox.Text) / desktopResolutionY)));
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			String fileName = "C:/deneme.jpg";
			ScreenCapturer capturer = ((MainForm)this.MdiParent).getCapturer();
			if(capturer == null){
				capturer = new ScreenCapturer();
				((MainForm)this.MdiParent).setCapturer(capturer);
			}
			capturer.setRectangle(Convert.ToInt32(fromXTextBox.Text),Convert.ToInt32(fromYTextBox.Text), Convert.ToInt32(toXTextBox.Text) - Convert.ToInt32(fromXTextBox.Text), Convert.ToInt32(toYTextBox.Text) - Convert.ToInt32(fromYTextBox.Text) );
			
			//standardize the file name
			capturer.captureRectangle(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
			MainForm mf = (MainForm)this.MdiParent;
			ClassClient vcClient = mf.getClient();
//			vcClient.sendFile("C:/deneme.jpg");

			//FileStream fileStream = File.OpenRead(fileName);
			//byte[] snapshot = new byte[fileStream.Length + 4];
			//fileStream.Read(snapshot, 0, Convert.ToInt32(fileStream.Length) + 4);
			Image snapshot = Image.FromFile(fileName);

			vcClient.shareApplication(snapshot, 1);
			snapshot.Dispose();
		}

		private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			moveStartX = e.X;
			moveStartY = e.Y;
			panel2.Location = new Point(moveStartX, moveStartY);
			panel2.Size = new Size(1, 1);
		}

		private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left){
				moveEndX = e.X;
				moveEndY = e.Y;
				int moveX = moveEndX - moveStartX;
				int moveY = moveEndY - moveStartY;
				int startX = moveStartX, startY = moveStartY;
				if(moveX < 0 && moveY < 0)
				{
					startX = moveEndX;
					startY = moveEndY;
					moveX = -moveX;
					moveY = -moveY;
					toXTextBox.Text = Math.Floor(moveStartX * desktopResolutionX).ToString();
					toYTextBox.Text = Math.Floor(moveStartY * desktopResolutionY).ToString();
				}
				else if(moveX < 0)
				{
					startX = moveEndX;
					moveX = -moveX;
					toXTextBox.Text = Math.Floor(moveStartX * desktopResolutionX).ToString();
					toYTextBox.Text = Math.Floor(moveEndY * desktopResolutionY).ToString();
				}
				else if(moveY < 0)
				{
					startY = moveEndY;
					moveY = -moveY;
					toYTextBox.Text = Math.Floor(moveStartY * desktopResolutionY).ToString();
					toXTextBox.Text = Math.Floor(moveEndX * desktopResolutionX).ToString();
				}
				else{
					toXTextBox.Text = Math.Floor(moveEndX * desktopResolutionX).ToString();
					toYTextBox.Text = Math.Floor(moveEndY * desktopResolutionY).ToString();
				}
				fromXTextBox.Text = Math.Floor(startX * desktopResolutionX).ToString();
				fromYTextBox.Text = Math.Floor(startY * desktopResolutionY).ToString();
				panel2.Bounds = new Rectangle(new Point(startX, startY),new Size(moveX, moveY));
			}
		}

		private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
		{
			radioButton2.Checked = true;
			radioButton2.Update();
			this.Update();
		}

		private void updatePanel2()
		{
		
		}
	}
}
