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

using DiagramUI;
using FormulaEditor;
using ToolBox;
using DataPerformer;

namespace DataPerformerUI
{
	/// <summary>
	/// Summary description for FormRecursive.
	/// </summary>
	public class FormRecursive : System.Windows.Forms.Form, IUpdatableForm
	{
		private System.Windows.Forms.TabControl tabControlMain;
		private System.Windows.Forms.TabPage tabPageVariables;
		private System.Windows.Forms.TabPage tabPageEquations;
		private System.Windows.Forms.TabPage tabPageConstants;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private Hashtable variableCombo = new Hashtable();
		private IObjectLabel label;
		private System.Windows.Forms.Panel panelVariables;
		private System.Windows.Forms.Panel panelFormula;
		private Recursive recursive;
		private System.Windows.Forms.Button buttonPar;
		private System.Windows.Forms.Button buttonArgs;
		private System.Windows.Forms.Button buttonAccept;
		private System.Windows.Forms.CheckedListBox checkedListBoxP;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Panel panelMea;
		private System.Windows.Forms.Button buttonAcceptAliases;
		private System.Windows.Forms.Panel panelComboAlias;
		private System.Windows.Forms.Button buttonAcceptInitial;
		private System.Windows.Forms.DataGrid dataGridP;
		private System.Data.DataTable dataTableP;
		private System.Data.DataColumn dataColumnP;
		private System.Data.DataColumn dataColumnVal;
		private System.Windows.Forms.DataGrid dataGridInitial;
		private System.Data.DataTable dataTableInitial;
		private System.Data.DataColumn dataColumnV;
		private System.Data.DataColumn dataColumnValue;
		private System.Data.DataSet dataSetP;
		private System.Data.DataSet dataSetInitial;
		private System.Windows.Forms.Label labelInitial;
		private Hashtable formulaPanels = new Hashtable();
		private System.Windows.Forms.Button buttonVar;
		private Hashtable aliasCombo = new Hashtable();
		private System.Windows.Forms.Panel panelComments;
		private System.Windows.Forms.Button buttonAcceptComments;
		private System.Windows.Forms.MainMenu mainMenuConsumer;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemFont;
		private System.Windows.Forms.Label label2;
		private Hashtable formulaHash = new Hashtable();

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

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

		public FormRecursive(IObjectLabel label)
		{
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
			EditorReceiver.AddEditorDrag(panelComments);
			PictureReceiver.AddImageDrag(panelComments);
			this.label = label;
			recursive = label.Object as Recursive;
			ArrayList comments = recursive.Comments;
			ControlPanel.LoadControls(panelComments, comments);
			UpdateFormUI();
			fillVariables();
			fillFormulas();
			fillConstants();
			fillMeasurements();
			createAndFillAliasComboBox();
		}


		#region IUpdatableForm Members

		public void UpdateFormUI()
		{
			Text = label.RootName;//NamedComponent.GetText(label);
		}

		#endregion


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			//NamedComponent.RemoveForm(label);
			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.tabControlMain = new System.Windows.Forms.TabControl();
			this.tabPageVariables = new System.Windows.Forms.TabPage();
			this.buttonAcceptComments = new System.Windows.Forms.Button();
			this.buttonVar = new System.Windows.Forms.Button();
			this.panelVariables = new System.Windows.Forms.Panel();
			this.panelComments = new System.Windows.Forms.Panel();
			this.tabPageEquations = new System.Windows.Forms.TabPage();
			this.buttonPar = new System.Windows.Forms.Button();
			this.buttonArgs = new System.Windows.Forms.Button();
			this.buttonAccept = new System.Windows.Forms.Button();
			this.checkedListBoxP = new System.Windows.Forms.CheckedListBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.panelMea = new System.Windows.Forms.Panel();
			this.panelFormula = new System.Windows.Forms.Panel();
			this.tabPageConstants = new System.Windows.Forms.TabPage();
			this.label2 = new System.Windows.Forms.Label();
			this.buttonAcceptInitial = new System.Windows.Forms.Button();
			this.dataGridP = new System.Windows.Forms.DataGrid();
			this.dataTableP = new System.Data.DataTable();
			this.dataColumnP = new System.Data.DataColumn();
			this.dataColumnVal = new System.Data.DataColumn();
			this.dataGridInitial = new System.Windows.Forms.DataGrid();
			this.dataTableInitial = new System.Data.DataTable();
			this.dataColumnV = new System.Data.DataColumn();
			this.dataColumnValue = new System.Data.DataColumn();
			this.labelInitial = new System.Windows.Forms.Label();
			this.buttonAcceptAliases = new System.Windows.Forms.Button();
			this.panelComboAlias = new System.Windows.Forms.Panel();
			this.dataSetP = new System.Data.DataSet();
			this.dataSetInitial = new System.Data.DataSet();
			this.mainMenuConsumer = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemFont = new System.Windows.Forms.MenuItem();
			this.tabControlMain.SuspendLayout();
			this.tabPageVariables.SuspendLayout();
			this.tabPageEquations.SuspendLayout();
			this.tabPageConstants.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridP)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableP)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGridInitial)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableInitial)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetP)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetInitial)).BeginInit();
			this.SuspendLayout();
			// 
			// tabControlMain
			// 
			this.tabControlMain.Controls.Add(this.tabPageVariables);
			this.tabControlMain.Controls.Add(this.tabPageEquations);
			this.tabControlMain.Controls.Add(this.tabPageConstants);
			this.tabControlMain.Location = new System.Drawing.Point(48, 56);
			this.tabControlMain.Name = "tabControlMain";
			this.tabControlMain.SelectedIndex = 0;
			this.tabControlMain.Size = new System.Drawing.Size(1080, 648);
			this.tabControlMain.TabIndex = 0;
			// 
			// tabPageVariables
			// 
			this.tabPageVariables.Controls.Add(this.buttonAcceptComments);
			this.tabPageVariables.Controls.Add(this.buttonVar);
			this.tabPageVariables.Controls.Add(this.panelVariables);
			this.tabPageVariables.Controls.Add(this.panelComments);
			this.tabPageVariables.Location = new System.Drawing.Point(4, 22);
			this.tabPageVariables.Name = "tabPageVariables";
			this.tabPageVariables.Size = new System.Drawing.Size(1072, 622);
			this.tabPageVariables.TabIndex = 0;
			this.tabPageVariables.Text = "Variables";
			// 
			// buttonAcceptComments
			// 
			this.buttonAcceptComments.Location = new System.Drawing.Point(384, 24);
			this.buttonAcceptComments.Name = "buttonAcceptComments";
			this.buttonAcceptComments.Size = new System.Drawing.Size(224, 23);
			this.buttonAcceptComments.TabIndex = 22;
			this.buttonAcceptComments.Text = "Accept comments";
			this.buttonAcceptComments.Click += new System.EventHandler(this.buttonAcceptComments_Click);
			// 
			// buttonVar
			// 
			this.buttonVar.Location = new System.Drawing.Point(720, 536);
			this.buttonVar.Name = "buttonVar";
			this.buttonVar.Size = new System.Drawing.Size(192, 23);
			this.buttonVar.TabIndex = 2;
			this.buttonVar.Text = "Accept variables";
			this.buttonVar.Click += new System.EventHandler(this.buttonVar_Click);
			// 
			// panelVariables
			// 
			this.panelVariables.AutoScroll = true;
			this.panelVariables.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelVariables.Location = new System.Drawing.Point(720, 56);
			this.panelVariables.Name = "panelVariables";
			this.panelVariables.Size = new System.Drawing.Size(296, 424);
			this.panelVariables.TabIndex = 0;
			// 
			// panelComments
			// 
			this.panelComments.BackColor = System.Drawing.Color.White;
			this.panelComments.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panelComments.Location = new System.Drawing.Point(24, 64);
			this.panelComments.Name = "panelComments";
			this.panelComments.Size = new System.Drawing.Size(664, 544);
			this.panelComments.TabIndex = 23;
			// 
			// tabPageEquations
			// 
			this.tabPageEquations.Controls.Add(this.buttonPar);
			this.tabPageEquations.Controls.Add(this.buttonArgs);
			this.tabPageEquations.Controls.Add(this.buttonAccept);
			this.tabPageEquations.Controls.Add(this.checkedListBoxP);
			this.tabPageEquations.Controls.Add(this.label4);
			this.tabPageEquations.Controls.Add(this.label1);
			this.tabPageEquations.Controls.Add(this.panelMea);
			this.tabPageEquations.Controls.Add(this.panelFormula);
			this.tabPageEquations.Location = new System.Drawing.Point(4, 22);
			this.tabPageEquations.Name = "tabPageEquations";
			this.tabPageEquations.Size = new System.Drawing.Size(1072, 622);
			this.tabPageEquations.TabIndex = 1;
			this.tabPageEquations.Text = "Recursive relations";
			// 
			// buttonPar
			// 
			this.buttonPar.Location = new System.Drawing.Point(288, 576);
			this.buttonPar.Name = "buttonPar";
			this.buttonPar.Size = new System.Drawing.Size(248, 23);
			this.buttonPar.TabIndex = 28;
			this.buttonPar.Text = "Accept constants";
			this.buttonPar.Click += new System.EventHandler(this.buttonPar_Click);
			// 
			// buttonArgs
			// 
			this.buttonArgs.Location = new System.Drawing.Point(576, 584);
			this.buttonArgs.Name = "buttonArgs";
			this.buttonArgs.Size = new System.Drawing.Size(280, 23);
			this.buttonArgs.TabIndex = 27;
			this.buttonArgs.Text = "Accept input parameters";
			this.buttonArgs.Click += new System.EventHandler(this.buttonArgs_Click);
			// 
			// buttonAccept
			// 
			this.buttonAccept.Location = new System.Drawing.Point(32, 576);
			this.buttonAccept.Name = "buttonAccept";
			this.buttonAccept.Size = new System.Drawing.Size(232, 23);
			this.buttonAccept.TabIndex = 26;
			this.buttonAccept.Text = "Accept equations";
			this.buttonAccept.Click += new System.EventHandler(this.buttonAccept_Click);
			// 
			// checkedListBoxP
			// 
			this.checkedListBoxP.Location = new System.Drawing.Point(680, 72);
			this.checkedListBoxP.Name = "checkedListBoxP";
			this.checkedListBoxP.Size = new System.Drawing.Size(120, 469);
			this.checkedListBoxP.TabIndex = 24;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(696, 32);
			this.label4.Name = "label4";
			this.label4.TabIndex = 25;
			this.label4.Text = "Constants";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(872, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 23);
			this.label1.TabIndex = 23;
			this.label1.Text = "Data sources";
			// 
			// panelMea
			// 
			this.panelMea.AutoScroll = true;
			this.panelMea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelMea.Location = new System.Drawing.Point(816, 72);
			this.panelMea.Name = "panelMea";
			this.panelMea.Size = new System.Drawing.Size(248, 464);
			this.panelMea.TabIndex = 22;
			// 
			// panelFormula
			// 
			this.panelFormula.AutoScroll = true;
			this.panelFormula.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelFormula.Location = new System.Drawing.Point(32, 72);
			this.panelFormula.Name = "panelFormula";
			this.panelFormula.Size = new System.Drawing.Size(616, 472);
			this.panelFormula.TabIndex = 12;
			// 
			// tabPageConstants
			// 
			this.tabPageConstants.Controls.Add(this.label2);
			this.tabPageConstants.Controls.Add(this.buttonAcceptInitial);
			this.tabPageConstants.Controls.Add(this.dataGridP);
			this.tabPageConstants.Controls.Add(this.dataGridInitial);
			this.tabPageConstants.Controls.Add(this.labelInitial);
			this.tabPageConstants.Controls.Add(this.buttonAcceptAliases);
			this.tabPageConstants.Controls.Add(this.panelComboAlias);
			this.tabPageConstants.Location = new System.Drawing.Point(4, 22);
			this.tabPageConstants.Name = "tabPageConstants";
			this.tabPageConstants.Size = new System.Drawing.Size(1072, 622);
			this.tabPageConstants.TabIndex = 2;
			this.tabPageConstants.Text = "Constants";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(744, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(184, 23);
			this.label2.TabIndex = 36;
			this.label2.Text = "External aliases";
			// 
			// buttonAcceptInitial
			// 
			this.buttonAcceptInitial.Location = new System.Drawing.Point(280, 536);
			this.buttonAcceptInitial.Name = "buttonAcceptInitial";
			this.buttonAcceptInitial.TabIndex = 33;
			this.buttonAcceptInitial.Text = "Accept";
			this.buttonAcceptInitial.Click += new System.EventHandler(this.buttonAcceptInitial_Click);
			// 
			// dataGridP
			// 
			this.dataGridP.DataMember = "";
			this.dataGridP.DataSource = this.dataTableP;
			this.dataGridP.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridP.Location = new System.Drawing.Point(368, 80);
			this.dataGridP.Name = "dataGridP";
			this.dataGridP.Size = new System.Drawing.Size(296, 416);
			this.dataGridP.TabIndex = 32;
			// 
			// dataTableP
			// 
			this.dataTableP.Columns.AddRange(new System.Data.DataColumn[] {
																			  this.dataColumnP,
																			  this.dataColumnVal});
			this.dataTableP.TableName = "TableP";
			// 
			// dataColumnP
			// 
			this.dataColumnP.ColumnName = "Parameter";
			this.dataColumnP.ReadOnly = true;
			// 
			// dataColumnVal
			// 
			this.dataColumnVal.ColumnName = "Value";
			this.dataColumnVal.DataType = typeof(System.Double);
			// 
			// dataGridInitial
			// 
			this.dataGridInitial.DataMember = "";
			this.dataGridInitial.DataSource = this.dataTableInitial;
			this.dataGridInitial.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridInitial.Location = new System.Drawing.Point(56, 80);
			this.dataGridInitial.Name = "dataGridInitial";
			this.dataGridInitial.Size = new System.Drawing.Size(296, 416);
			this.dataGridInitial.TabIndex = 31;
			// 
			// dataTableInitial
			// 
			this.dataTableInitial.Columns.AddRange(new System.Data.DataColumn[] {
																					this.dataColumnV,
																					this.dataColumnValue});
			this.dataTableInitial.TableName = "TableInitial";
			// 
			// dataColumnV
			// 
			this.dataColumnV.ColumnName = "Variable";
			this.dataColumnV.ReadOnly = true;
			// 
			// dataColumnValue
			// 
			this.dataColumnValue.ColumnName = "Value";
			this.dataColumnValue.DataType = typeof(System.Double);
			// 
			// labelInitial
			// 
			this.labelInitial.Location = new System.Drawing.Point(128, 32);
			this.labelInitial.Name = "labelInitial";
			this.labelInitial.Size = new System.Drawing.Size(184, 23);
			this.labelInitial.TabIndex = 30;
			this.labelInitial.Text = "Initial values";
			// 
			// buttonAcceptAliases
			// 
			this.buttonAcceptAliases.Location = new System.Drawing.Point(752, 504);
			this.buttonAcceptAliases.Name = "buttonAcceptAliases";
			this.buttonAcceptAliases.TabIndex = 35;
			this.buttonAcceptAliases.Text = "Accept";
			this.buttonAcceptAliases.Click += new System.EventHandler(this.buttonAcceptAliases_Click);
			// 
			// panelComboAlias
			// 
			this.panelComboAlias.AutoScroll = true;
			this.panelComboAlias.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelComboAlias.Location = new System.Drawing.Point(680, 56);
			this.panelComboAlias.Name = "panelComboAlias";
			this.panelComboAlias.Size = new System.Drawing.Size(352, 440);
			this.panelComboAlias.TabIndex = 34;
			// 
			// dataSetP
			// 
			this.dataSetP.DataSetName = "NewDataSet";
			this.dataSetP.Locale = new System.Globalization.CultureInfo("ru-RU");
			this.dataSetP.Tables.AddRange(new System.Data.DataTable[] {
																		  this.dataTableP});
			// 
			// dataSetInitial
			// 
			this.dataSetInitial.DataSetName = "NewDataSet";
			this.dataSetInitial.Locale = new System.Globalization.CultureInfo("ru-RU");
			this.dataSetInitial.Tables.AddRange(new System.Data.DataTable[] {
																				this.dataTableInitial});
			// 
			// mainMenuConsumer
			// 
			this.mainMenuConsumer.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							 this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemFont});
			this.menuItem1.Text = "Format";
			// 
			// menuItemFont
			// 
			this.menuItemFont.Index = 0;
			this.menuItemFont.Text = "Font";
			this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
			// 
			// FormRecursive
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(1144, 741);
			this.Controls.Add(this.tabControlMain);
			this.Menu = this.mainMenuConsumer;
			this.Name = "FormRecursive";
			this.Text = "FormRecursive";
			this.tabControlMain.ResumeLayout(false);
			this.tabPageVariables.ResumeLayout(false);
			this.tabPageEquations.ResumeLayout(false);
			this.tabPageConstants.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridP)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableP)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGridInitial)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableInitial)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetP)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetInitial)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void fillVariables()
		{
			string var = FormDiffEquation.Variables;
			int y = 40;
			foreach (char c in var)
			{
				Label l = new Label();
				l.Text = c + "";
				l.Left = 20;
				l.Width = 40;
				l.Top = y;
				panelVariables.Controls.Add(l);
				ComboBox cb = new ComboBox();
				cb.Top = y;
				cb.Left = l.Left + l.Width + 10;
				y += cb.Height + 10;
				variableCombo[c] = cb;
				foreach (string s in Measure.TypeNames)
				{
					cb.Items.Add(s);
				}
				panelVariables.Controls.Add(cb);
			}
			Hashtable variables = recursive.Variables;
			foreach (char c in variables.Keys)
			{
				object[] o = variables[c] as object[];
				ComboBox combo = variableCombo[c] as ComboBox;
				string type = Measure.GetTypeName(o[2]);
				for (int i = 0; i < combo.Items.Count; i++)
				{
					if (type.Equals(combo.Items[i].ToString()))
					{
						combo.SelectedIndex = i;
						break;
					}
				}
			}
		}

		private void fillFormulas()
		{
			Hashtable variables = recursive.Variables;
			formulaHash.Clear();
			formulaPanels.Clear();
			panelFormula.Controls.Clear();
			int top = 0;
			foreach (char c in variables.Keys)
			{
				PanelFormula p = new PanelFormula("" + c, this, panelFormula.Width, 200, FormDiffEquation.Variables, false, null, null);
				p.Left = 0;
				p.Top = top;
				top += p.Height;
				object[] o = variables[c] as object[];
				if (o[1] != null)
				{
					string f = o[1] as string;
					p.Formula = f;
				}
				panelFormula.Controls.Add(p);
				formulaHash[c] = p;
			}
		}

		private void fillConstants()
		{
			string st = recursive.AllExternalVariables;
			string str = recursive.AliasesString;
			Hashtable vars = recursive.Variables;
			checkedListBoxP.Items.Clear();
			foreach (char c in st)
			{
				if (vars.ContainsKey(c))
				{
					continue;
				}
				if (str.IndexOf(c) < 0)
				{
					checkedListBoxP.Items.Add(c + "", CheckState.Unchecked);
				}
				else
				{
					checkedListBoxP.Items.Add(c + "", CheckState.Checked);
				}

			}
			dataTableInitial.Clear();
			Hashtable var = recursive.Variables;
			foreach (char c in var.Keys)
			{
				object[] o = var[c] as object[];
				double a = ElementaryIntegerOperation.ToDouble(o[2]);
				dataTableInitial.Rows.Add(new object[]{c + "", a});
			}
			IAlias al = recursive;
			List<string> list = al.AliasNames;
			dataTableP.Clear();
			foreach (string s in list)
			{
				if (var.ContainsKey(s[0]))
				{
					continue;
				}
				dataTableP.Rows.Add(new object[]{s, ElementaryIntegerOperation.ToDouble(al[s])});
			}
		}


		private void fillMeasurements()
		{
			string s = "";
			Hashtable table = recursive.Arguments;
			foreach (char ch in table.Keys)
			{
				s += ch;
			}
			IDataConsumer c = recursive;
			int y = 0;
			for (int i = 0; i < c.Count; i++)
			{
				IMeasurements arrow = c[i];
				PanelMeasureFormula panel = new PanelMeasureFormula(arrow, s, recursive);
				panel.Width = 300;
				Panel pan = new Panel();
				pan.Width = panel.Width;
				pan.BackColor = Color.Black;
				pan.Top = y;
				pan.Height = 2;
				panelMea.Controls.Add(pan);
				y += pan.Height;
				panelMea.Controls.Add(panel);
				panel.Left = 0;
				panel.Top = y;
				y += panel.Height + 1;
			}
	/*		foreach (string str in solver.Arguments)
			{
				comboBoxTime.Items.Add(str[0] + "");
			}
			foreach (string str in solver.Arguments)
			{
				string s = str;
				int n = str.Length;
				if (s.Substring(n - 4, 4).Equals("Time"))
				{
					for (int i = 0; i < variables.Length; i++)
					{
						if (variables[i] == str[0])
						{
							this.comboBoxTime.SelectedIndex = i;
							return;
						}
					}
				}
			}*/
			

		}

		private void fillMeaCombo()
		{
			string s = "";
			Hashtable table = recursive.Arguments;
			foreach (char ch in table.Keys)
			{
				s += ch;
			}
			foreach (Control c in panelMea.Controls)
			{
				if (!(c is PanelMeasureFormula))
				{
					continue;
				}
				PanelMeasureFormula p = c as PanelMeasureFormula;
				p.FillComboboxes(s);
			}
		}

		private void createAndFillAliasComboBox()
		{
			IMeasurements m = recursive;
			Panel panelComboAliasInner = new Panel();
			panelComboAliasInner.Width = panelComboAlias.Width;
			panelComboAliasInner.Height = 5;
			panelComboAlias.Controls.Add(panelComboAliasInner);
			int y = 0;
            List<string> al = new List<string>();
			DataConsumer.GetAliases(recursive, al);
			Hashtable ea = recursive.ExternalAliases;
			for (int i = 0; i < m.Count; i++)
			{
				IMeasure mea = m[i];
				char c = mea.Name[0];
				ComboBox cb = new ComboBox();
				panelComboAliasInner.Controls.Add(cb);
				aliasCombo[c] = cb;
				cb.Width = 121;
				cb.Height = 21;
				cb.Top = 10 + y;
				cb.Left = 10;
				Label l = new Label();
				panelComboAliasInner.Controls.Add(l);
				l.Text = c + "";
				l.Top = cb.Top;
				l.Left = cb.Left + cb.Width + 10;
				foreach (string s in al)
				{
					cb.Items.Add(s);
				}
				y += cb.Height + 10;
				if (ea == null)
				{
					continue;
				}
				if (ea.ContainsKey(c))
				{
					string str = ea[c] as string;
					for (int j = 0; j < cb.Items.Count; j++)
					{
						if (str.Equals(cb.Items[j].ToString()))
						{
							cb.SelectedIndex = j;
							break;
						}
					}
				}
			}
			panelComboAliasInner.Height = y;
			
		}

		private void buttonVar_Click(object sender, System.EventArgs e)
		{
			try
			{
				Hashtable table = new Hashtable();
				foreach (char c in variableCombo.Keys)
				{
					ComboBox cb = variableCombo[c] as ComboBox;
					if (cb.SelectedIndex < 0)
					{
						continue;
					}
					string s = cb.SelectedItem.ToString();
					object[] o = new object[3];
					double a = 0;
					o[0] = a;
					o[2] = Measure.GetType(s);
					table[c] = o;
				}
				recursive.Variables = table;
				fillFormulas();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void buttonAccept_Click(object sender, System.EventArgs e)
		{
			try
			{
				Hashtable table = new Hashtable();
				foreach (char c in formulaHash.Keys)
				{
					PanelFormula p = formulaHash[c] as PanelFormula;
					table[c] = p.Formula;
				}
				recursive.Formulas = table;
				fillConstants();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void buttonPar_Click(object sender, System.EventArgs e)
		{
			try
			{
				string s = "";
				foreach (string str in checkedListBoxP.CheckedItems)
				{
					s += str[0];
				}
				recursive.AliasesString = s;
				fillMeaCombo();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}

		}


		private void buttonArgs_Click(object sender, System.EventArgs e)
		{
			try
			{
				ArrayList arg = arguments;
				Hashtable table = new Hashtable();
				foreach (string s in arg)
				{
					table[s[0]] = s.Substring(4);
				}
				recursive.Arguments = table;
				fillConstants();
				createAndFillAliasComboBox();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void buttonAcceptInitial_Click(object sender, System.EventArgs e)
		{
			IAlias al = recursive;
			try
			{
				foreach (DataRow row in dataTableP.Rows)
				{
					string s = row[0] as string;
					double a = (double)row[1];

					al[s] = a;
				}
				Hashtable var = recursive.Variables;
				foreach (DataRow row in dataTableInitial.Rows)
				{
					string st = row[0] as string;
					double at = (double)row[1];
					object[] o = var[st[0]] as object[];
					o[0] = ElementaryBinaryOperation.Convert(o[2], at);
				}
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}

		}

		private void buttonAcceptAliases_Click(object sender, System.EventArgs e)
		{
			try
			{
				Hashtable table = new Hashtable();
				foreach (char c in aliasCombo.Keys)
				{
					ComboBox cb = aliasCombo[c] as ComboBox;
					if (cb.SelectedIndex < 0)
					{
						continue;
					}
					table[c] = cb.SelectedItem.ToString();
				}
				recursive.ExternalAliases = table;
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		
		}

		private void buttonAcceptComments_Click(object sender, System.EventArgs e)
		{
			ArrayList comments = ToolBox.ControlPanel.GetControls(panelComments);
			recursive.Comments = comments;
		}

		private void menuItemFont_Click(object sender, System.EventArgs e)
		{
			try
			{
				TextBox box = ToolBox.ControlPanel.GetActiveTextBox(panelComments);
				if (box == null)
				{
					return;
				}
				FontDialog dlg = new FontDialog();
				dlg.ShowDialog(this);
				Font font = dlg.Font;
				box.Font = font;
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}

		}

		private ArrayList arguments
		{
			get
			{
				ArrayList list = new ArrayList();
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureFormula))
					{
						continue;
					}
					PanelMeasureFormula p = c as PanelMeasureFormula;
					p.AddArgumentLabels(list);
				}
/*				object ob = comboBoxTime.SelectedItem;
				if (ob != null)
				{
					string sn = comboBoxTime.SelectedItem.ToString();
					if (sn.Length != 0)
					{
						list.Add(sn + " = Time");
					}
				}*/
				return list;
			}
		}

	}
}
