using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using Microsoft.Ink;


namespace MathSpeakWrite
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class TrainerMainWin : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel writingPanel;
		private System.Windows.Forms.TextBox tb_Outputs;
		private System.Windows.Forms.Button b_AddSample;
		private System.Windows.Forms.Button b_SaveTraining;
		private System.Windows.Forms.MainMenu mainMenu4;
		private System.Windows.Forms.MenuItem mi_MenuBar;
		private System.Windows.Forms.MenuItem mi_Exit;
		private System.Windows.Forms.Button b_TrainNew;
		private System.Windows.Forms.MenuItem mi_SaveQuit;

		private InkCollector daInkCol;
		private System.Windows.Forms.Button b_ClearScreen;
		private SymbolData mathSymSD, lowerSD, upperSD;
		private int num_trained;
		private string saveName;
		private NameChecker daNC;
		private SymChooser aSC;

		private static string TrainingDir = "data\\", ptPath = "PostTrain.exe";
		private static string mathSymFN = "AllNumSym.txt", lowerFN = "AllLower.txt", upperFN = "AllUpper.txt";
		

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

		public TrainerMainWin()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			this.RunPostTrain();
			
			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.writingPanel = new System.Windows.Forms.Panel();
			this.tb_Outputs = new System.Windows.Forms.TextBox();
			this.b_AddSample = new System.Windows.Forms.Button();
			this.b_TrainNew = new System.Windows.Forms.Button();
			this.b_SaveTraining = new System.Windows.Forms.Button();
			this.mainMenu4 = new System.Windows.Forms.MainMenu();
			this.mi_MenuBar = new System.Windows.Forms.MenuItem();
			this.mi_Exit = new System.Windows.Forms.MenuItem();
			this.mi_SaveQuit = new System.Windows.Forms.MenuItem();
			this.b_ClearScreen = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// writingPanel
			// 
			this.writingPanel.Location = new System.Drawing.Point(8, 8);
			this.writingPanel.Name = "writingPanel";
			this.writingPanel.Size = new System.Drawing.Size(504, 344);
			this.writingPanel.TabIndex = 0;
			// 
			// tb_Outputs
			// 
			this.tb_Outputs.Location = new System.Drawing.Point(8, 360);
			this.tb_Outputs.Name = "tb_Outputs";
			this.tb_Outputs.ReadOnly = true;
			this.tb_Outputs.Size = new System.Drawing.Size(504, 20);
			this.tb_Outputs.TabIndex = 1;
			this.tb_Outputs.Text = "Various Outputs Appear Here";
			// 
			// b_AddSample
			// 
			this.b_AddSample.Location = new System.Drawing.Point(8, 384);
			this.b_AddSample.Name = "b_AddSample";
			this.b_AddSample.Size = new System.Drawing.Size(96, 24);
			this.b_AddSample.TabIndex = 2;
			this.b_AddSample.Text = "Add To Training";
			this.b_AddSample.Click += new System.EventHandler(this.b_AddSample_Click);
			// 
			// b_TrainNew
			// 
			this.b_TrainNew.Location = new System.Drawing.Point(208, 384);
			this.b_TrainNew.Name = "b_TrainNew";
			this.b_TrainNew.Size = new System.Drawing.Size(168, 24);
			this.b_TrainNew.TabIndex = 3;
			this.b_TrainNew.Text = "Stop Training This Symbol";
			this.b_TrainNew.Click += new System.EventHandler(this.b_TrainNew_Click);
			// 
			// b_SaveTraining
			// 
			this.b_SaveTraining.Location = new System.Drawing.Point(112, 384);
			this.b_SaveTraining.Name = "b_SaveTraining";
			this.b_SaveTraining.Size = new System.Drawing.Size(88, 24);
			this.b_SaveTraining.TabIndex = 4;
			this.b_SaveTraining.Text = "Save Training";
			this.b_SaveTraining.Click += new System.EventHandler(this.b_SaveTraining_Click);
			// 
			// mainMenu4
			// 
			this.mainMenu4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mi_MenuBar});
			// 
			// mi_MenuBar
			// 
			this.mi_MenuBar.Index = 0;
			this.mi_MenuBar.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.mi_Exit,
																					   this.mi_SaveQuit});
			this.mi_MenuBar.Text = "File";
			// 
			// mi_Exit
			// 
			this.mi_Exit.Index = 0;
			this.mi_Exit.Text = "Close Without Saving Current Symbol";
			this.mi_Exit.Click += new System.EventHandler(this.mi_Exit_Click);
			// 
			// mi_SaveQuit
			// 
			this.mi_SaveQuit.Index = 1;
			this.mi_SaveQuit.ShowShortcut = false;
			this.mi_SaveQuit.Text = "Close and Save Current Symbol";
			this.mi_SaveQuit.Click += new System.EventHandler(this.mi_SaveQuit_Click);
			// 
			// b_ClearScreen
			// 
			this.b_ClearScreen.Location = new System.Drawing.Point(384, 384);
			this.b_ClearScreen.Name = "b_ClearScreen";
			this.b_ClearScreen.Size = new System.Drawing.Size(128, 24);
			this.b_ClearScreen.TabIndex = 6;
			this.b_ClearScreen.Text = "Clear Screen";
			this.b_ClearScreen.Click += new System.EventHandler(this.b_ClearScreen_Click);
			// 
			// TrainerMainWin
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(522, 416);
			this.Controls.Add(this.b_ClearScreen);
			this.Controls.Add(this.b_SaveTraining);
			this.Controls.Add(this.b_TrainNew);
			this.Controls.Add(this.b_AddSample);
			this.Controls.Add(this.tb_Outputs);
			this.Controls.Add(this.writingPanel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Menu = this.mainMenu4;
			this.Name = "TrainerMainWin";
			this.Text = "Math Handwriting Trainer";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void ClearScreen()
		{
			this.writingPanel.Invalidate();
			this.daInkCol.Ink.DeleteStrokes();
			this.writingPanel.Refresh();
		}

		private void RunTrainingDialog()
		{
			CITHandwriting.ClearTraining();
			this.num_trained = 0;
			//this.IsMdiContainer = true;
		//	aSC.MdiParent = this;
			aSC.setupLB();
			aSC.ShowDialog(this);
			this.tb_Outputs.Text = "You have choosen to Train: " + aSC.selName + " with Code : " +aSC.selCode + 
				", training reset to 0 trained";
			saveName = aSC.selCode;
		}
		private void SaveTraining()
		{
			ClearScreen();

			if(saveName == null)
			{
				MessageBox.Show("Cannot Save without first selecting which character you are training, please"+
					"click \"Train New Symbol\" and try again");
				return;
			}

			CITHandwriting.WriteTraining(saveName.ToCharArray(), TrainingDir.ToCharArray());
			this.tb_Outputs.Text = "The symbol with code " + this.saveName + " has been saved";
		}
		private void RunPostTrain()
		{
			Process aProcess;
			ProcessStartInfo psi = new ProcessStartInfo(ptPath);
			psi.WindowStyle = ProcessWindowStyle.Hidden;
			this.tb_Outputs.Text = "Running PostTrain";
		//	Console.WriteLine("Running PostTrain");
			aProcess = Process.Start(psi);
			aProcess.WaitForExit();
			this.tb_Outputs.Text = "PostTrain Complete";
		//	Console.WriteLine("PostTrain Complete");
		}
		private void setupSymData()
		{
			mathSymSD = new SymbolData();
			mathSymSD.ReadData(mathSymFN);
			lowerSD = new SymbolData();
			lowerSD.ReadData(lowerFN);
			upperSD = new SymbolData();
			upperSD.ReadData(upperFN);
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			daInkCol = new InkCollector(writingPanel.Handle);
			daInkCol.Enabled = true;
			daNC = new NameChecker(TrainingDir);
			setupSymData();
			this.Show();
			aSC = new SymChooser(this.mathSymSD, this.lowerSD, this.upperSD, this.daNC, this);
			RunTrainingDialog();
		}

		private void mi_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void b_ClearScreen_Click(object sender, System.EventArgs e)
		{
			this.ClearScreen();
			this.tb_Outputs.Text = "Just Cleared Screen";
		}

		private void b_ClearTraining_Click(object sender, System.EventArgs e)
		{
			CITHandwriting.ClearTraining();
			num_trained = 0;
			this.tb_Outputs.Text = "Just Cleared All Training Data for unsaved symbol";
		}

		private void b_AddSample_Click(object sender, System.EventArgs e)
		{
			if(this.saveName == null)
			{
				MessageBox.Show("Please select a symbol");
				return;
			}
			if(!CITHandwriting.addToTrainingFromStrokes(daInkCol.Ink.Strokes, writingPanel.CreateGraphics(), daInkCol))
				return;
			this.ClearScreen();
			num_trained ++;
			this.tb_Outputs.Text = "Just Added Sample To Training, have now trained: " + num_trained + 
								   " with this method";
		}

		private void b_TrainNew_Click(object sender, System.EventArgs e)
		{
			RunTrainingDialog();
		}

		private void b_SaveTraining_Click(object sender, System.EventArgs e)
		{
			this.SaveTraining();
			RunTrainingDialog();
		}

		private void mi_SaveQuit_Click(object sender, System.EventArgs e)
		{
			this.SaveTraining();
			this.Close();
		}

	}
}
