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

namespace MathSpeakWrite
{
	/// <summary>
	/// Summary description for SymChooser.
	/// </summary>
	public class SymChooser : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private SymbolData syms, lower, upper;
		private System.Windows.Forms.ListBox SymbolsLB;
		private System.Windows.Forms.CheckBox LowerCaseCB;

		public string selName, selCode;
		public NameChecker aNC;
		private ArrayList presNames, presCodes;
		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button b_Exit; //Present names and corresponding codes in the window

		private System.Windows.Forms.Button TrainB;
		private System.Windows.Forms.Button b_Delete;
		private TrainerMainWin myParent;

		public SymChooser(SymbolData syms, SymbolData lower, SymbolData upper, NameChecker aNC, TrainerMainWin parentF)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			this.aNC = aNC;
			this.syms = syms;
			this.lower = lower;
			this.upper = upper;
			this.presCodes = new ArrayList();
			this.presNames = new ArrayList();
			this.myParent = parentF;
			setupLB();
		}
		
		/// <summary>
		/// Used to create the strings for the ListBox. These strings contain a * if they 
		/// have been trained and an upper/lower case identifier if case is important
		/// </summary>
		public void setupLB()
		{
			aNC.reloadTable();
			this.SymbolsLB.Items.Clear();
			this.presCodes.Clear();
			this.presNames.Clear();
			
			if(this.LowerCaseCB.Checked)
				foreach(string s in lower.names)
				{
					string presStr = "[Lower Case] " + s;
					presStr = aNC.containsName(lower.CodeCorrespondingToName(s)) ? "* "+ presStr : presStr;

					this.presCodes.Add(lower.CodeCorrespondingToName(s));
					this.presNames.Add(s);
					SymbolsLB.Items.Add(presStr);
				}
			else
				foreach (string s in upper.names)
				{
					string presStr = "[Upper Case] " + s;
					presStr = aNC.containsName(upper.CodeCorrespondingToName(s)) ? "* "+ presStr : presStr;

					this.presCodes.Add(upper.CodeCorrespondingToName(s));
					this.presNames.Add(s);
					SymbolsLB.Items.Add(presStr);
				}
			foreach (string s in syms.names)
			{
				this.presCodes.Add(syms.CodeCorrespondingToName(s));
				this.presNames.Add(s);
				SymbolsLB.Items.Add(aNC.containsName(syms.CodeCorrespondingToName(s)) ? "* "+ s : s);
			}
		}

		/// <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.SymbolsLB = new System.Windows.Forms.ListBox();
			this.LowerCaseCB = new System.Windows.Forms.CheckBox();
			this.TrainB = new System.Windows.Forms.Button();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.b_Exit = new System.Windows.Forms.Button();
			this.b_Delete = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// SymbolsLB
			// 
			this.SymbolsLB.Location = new System.Drawing.Point(8, 32);
			this.SymbolsLB.Name = "SymbolsLB";
			this.SymbolsLB.Size = new System.Drawing.Size(200, 381);
			this.SymbolsLB.TabIndex = 1;
			// 
			// LowerCaseCB
			// 
			this.LowerCaseCB.Location = new System.Drawing.Point(8, 416);
			this.LowerCaseCB.Name = "LowerCaseCB";
			this.LowerCaseCB.Size = new System.Drawing.Size(208, 24);
			this.LowerCaseCB.TabIndex = 2;
			this.LowerCaseCB.Text = "Use Lower Case for Letters + Greek";
			this.LowerCaseCB.Click += new System.EventHandler(this.LowerCaseCB_Click);
			// 
			// TrainB
			// 
			this.TrainB.Location = new System.Drawing.Point(8, 440);
			this.TrainB.Name = "TrainB";
			this.TrainB.Size = new System.Drawing.Size(200, 24);
			this.TrainB.TabIndex = 3;
			this.TrainB.Text = "Train Selected Symbol";
			this.TrainB.Click += new System.EventHandler(this.TrainB_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(200, 16);
			this.label1.TabIndex = 4;
			this.label1.Text = "Please Select a Symbol to Train";
			this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// b_Exit
			// 
			this.b_Exit.Location = new System.Drawing.Point(112, 472);
			this.b_Exit.Name = "b_Exit";
			this.b_Exit.Size = new System.Drawing.Size(96, 24);
			this.b_Exit.TabIndex = 5;
			this.b_Exit.Text = "Stop Training";
			this.b_Exit.Click += new System.EventHandler(this.b_Exit_Click);
			// 
			// b_Delete
			// 
			this.b_Delete.Location = new System.Drawing.Point(8, 472);
			this.b_Delete.Name = "b_Delete";
			this.b_Delete.Size = new System.Drawing.Size(96, 24);
			this.b_Delete.TabIndex = 6;
			this.b_Delete.Text = "Delete Selected";
			this.b_Delete.Click += new System.EventHandler(this.b_Delete_Click);
			// 
			// SymChooser
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(216, 502);
			this.ControlBox = false;
			this.Controls.Add(this.b_Delete);
			this.Controls.Add(this.b_Exit);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.TrainB);
			this.Controls.Add(this.LowerCaseCB);
			this.Controls.Add(this.SymbolsLB);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SymChooser";
			this.ShowInTaskbar = false;
			this.Text = "Symbol Chooser";
			this.Load += new System.EventHandler(this.SymChooser_Load);
			this.Closed += new System.EventHandler(this.SymChooser_Closed);
			this.ResumeLayout(false);

		}
		#endregion
		
		/// <summary>
		/// Reset the ListBox to display lowercase
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LowerCaseCB_Click(object sender, System.EventArgs e)
		{
			this.setupLB();
			this.Refresh();
		}

		/// <summary>
		/// Set the symbol as trained and close the window if a symbol is selected.
		/// If not, display an error message.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void TrainB_Click(object sender, System.EventArgs e)
		{
			int selIndex = this.SymbolsLB.SelectedIndex;
			
			if(selIndex < 0)
			{
				MessageBox.Show("Please Select A Symbol to Train");
				return;
			}

			this.selName = (string) this.presNames[selIndex];
			this.selCode = (string) this.presCodes[selIndex];
			this.Hide();
		}

		private void SymChooser_Closed(object sender, System.EventArgs e)
		{
			this.selName = null;
			this.selCode = null;
		}

		private void SymChooser_Load(object sender, System.EventArgs e)
		{
		
		}
		
		/// <summary>
		/// If the exit box is clicked, don't run posttrain in parent window
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void b_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
			this.myParent.CloseWithoutSaving();
		}

		/// <summary>
		/// Deletes a selected symbol using the NameChecker for aid.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void b_Delete_Click(object sender, System.EventArgs e)
		{
			int selIndex = this.SymbolsLB.SelectedIndex;
			
			if(selIndex < 0)
			{
				MessageBox.Show("Please Select A Symbol to Delete");
				return;
			}
				
			string aName = (string) this.presCodes[selIndex];
			//	this.selName = (string) this.presNames[selIndex];
			//	this.selCode = (string) this.presCodes[selIndex];
			
			if(!this.aNC.deleteFile(aName))
				MessageBox.Show("Unable to Delete File for some reason");
			this.setupLB();
			this.Refresh();
		}
	}
}
