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

namespace MathSpeakWrite
{
	public delegate void AltDel(string name, string code);

	/// <summary>
	/// AltsWindow is a WindowsForm for displaying handwriting alternatives.
	/// </summary>
	public class AltsWindow : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox AltsBox;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		
		private SymbolData []symData;
		private ArrayList Names;
		private AltDel callback;

		/// <summary>
		/// Constructor for the Alts Window. Provide an array of all SymbolDatas 
		/// and the Delegate to call when a symbol is choosen.
		/// </summary>
		/// <param name="allSyms">All SymbolDatas for all possible symbols</param>
		/// <param name="aDel">A delegate to call if the user chooses an Alt</param>
		public AltsWindow(SymbolData [] allSyms, AltDel aDel)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			Names = new ArrayList(); 
			symData = allSyms;
			callback = aDel;
		}
		
		/// <summary>
		/// Takes an array, arr of codes and converts them into their proper
		/// names which it then displays in the window. The names must be 
		/// somewhere in the symbol data objects we were provided.
		/// </summary>
		/// <param name="arr"></param>
		public void changeAlts(string [] arr)
		{
			int i;

			this.AltsBox.Items.Clear();
			this.Names.Clear();

			for(i=0; i < arr.Length; i++)
			{
				string daStr = null;
				
				foreach (SymbolData sd in symData)
				{
					daStr = sd.NameCorrespondingToCode(arr[i]);
					if(daStr != null)
						break;
				}
				
				if(daStr != null)
				{
					Names.Add(daStr);
					this.AltsBox.Items.Add(i.ToString() + ":\t" + daStr);
				}
			}
			this.Refresh();
		}
		
		/// <summary>
		/// Clear the Alternates window so that it displays no alternates
		/// </summary>
		public void clearAlts()
		{
			this.AltsBox.Items.Clear();
		}
		/// <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.AltsBox = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// AltsBox
			// 
			this.AltsBox.Location = new System.Drawing.Point(8, 8);
			this.AltsBox.Name = "AltsBox";
			this.AltsBox.Size = new System.Drawing.Size(216, 173);
			this.AltsBox.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 192);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(216, 24);
			this.button1.TabIndex = 1;
			this.button1.Text = "Replace Last Char With Selected";
			this.button1.Click += new System.EventHandler(this.B_ReplaceWith_Click);
			// 
			// AltsWindow
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(232, 222);
			this.ControlBox = false;
			this.Controls.Add(this.button1);
			this.Controls.Add(this.AltsBox);
			this.Name = "AltsWindow";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Handwriting Alternatives";
			this.ResumeLayout(false);

		}
		#endregion

		private void B_ReplaceWith_Click(object sender, System.EventArgs e)
		{
			int selI = this.AltsBox.SelectedIndex;
	
			if(selI == -1)
				return;

			DoAltN(selI);
		}

		/// <summary>
		/// Do the nth alternative in the window.
		/// </summary>
		/// <param name="n"></param>
		public void DoAltN(int n)
		{
			string selName, selCode=null;
			selName = (string) this.Names[n];

			foreach (SymbolData s in symData)
			{
				selCode=s.CodeCorrespondingToName(selName);	
				if(selCode != null)
					break;
			}

			if(selCode != null)
			{
				callback(selName,selCode);
			}
		}
	}
}
