using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Assemblies;
using System.Xml;

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

using Chart;

namespace DataPerformerUI
{

	public class FormGraph : Form, IUpdatableForm, IXmlElementCreator
	{
		public static readonly string UndefinedArgument = "Argument of function is not defined";
		private System.ComponentModel.Container components = null;
		private DataConsumer consumer;
		private IObjectLabel label;
		private Chart.ChartPerformer performer;
		private LogarithmCoordinator coordinator;
		private System.Windows.Forms.OpenFileDialog openFileDialogSeries;
        private System.Windows.Forms.SaveFileDialog saveFileDialogSeries;
        private System.Windows.Forms.SaveFileDialog saveFileDialogXml;
		private Series seriesStep;
		private System.Windows.Forms.Panel panelLeft;
		private System.Windows.Forms.Panel panelBottom;
		private System.Windows.Forms.Button buttonChooseSeries;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label labelSeries;
		private System.Windows.Forms.Panel panelChooseMode;
		private System.Windows.Forms.RadioButton radioButtonRepeatingSeries;
		private System.Windows.Forms.RadioButton radioButtonFixed;
		private System.Windows.Forms.CheckBox checkBoxTime;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBoxStart;
		private System.Windows.Forms.TextBox textBoxStepCount;
		private System.Windows.Forms.Button buttonStart;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBoxStep;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox comboBoxArg;
		private System.Windows.Forms.Panel panelRight;
		private System.Windows.Forms.Panel panelCenter;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Panel panelTopCheck;
		private System.Windows.Forms.Panel panelCheckBottom;
		private System.Windows.Forms.CheckBox checkBoxInvertX;
		private System.Windows.Forms.Panel panelIntLeft;
		private System.Windows.Forms.Panel panelIntRight;
		private System.Windows.Forms.Panel panelMeaTop;
		private System.Windows.Forms.Panel panelMeaLeft;
		private System.Windows.Forms.Panel panelMeaRight;
		private System.Windows.Forms.Panel panelMeaBottom;
		private System.Windows.Forms.Panel panelMeaCenter;
		private System.Windows.Forms.Panel panelMea;
		private System.Windows.Forms.Panel panelGraphCenter;
        private System.Windows.Forms.CheckBox checkBoxLogX;
        private Panel panelTop;
        private Label labelT;
        private MenuStrip menuStripMain;
        private ToolStrip toolStripMain;
        private ToolStripMenuItem fileToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private CheckBox checkBoxInvertY;
        private CheckBox checkBoxLogY;
        private Panel panelGraphTop;
        private Panel panelGraph;
        private ToolStripMenuItem toolsToolStripMenuItem;
        private ToolStripButton toolStripButtonAdd;
		private Hashtable measurements;
        private ToolStripMenuItem addseriesToolStripMenuItem;
        private ToolStripMenuItem clearallToolStripMenuItem;
        private ToolStripButton toolStripButtonClearAll;
        private ToolStripComboBox toolStripButtonType;
        private List<ISeries> ownSeries = new List<ISeries>();
        private ToolStripComboBox toolStripButtonSeries;

        OfficePickers.ColorPicker.ToolStripColorPicker pic = new OfficePickers.ColorPicker.ToolStripColorPicker();
        

		private FormGraph()
		{
			InitializeComponent();
		}

		public FormGraph(DataConsumer consumer, IObjectLabel label)
		{
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
			labelT.Text = NamedComponent.GetToolTip(label);
			this.consumer = consumer;
			this.label = label;
			UpdateFormUI();
			PanelChart panel = new PanelChart(new int[,]{{80, 30}, {10, 40}});
			panel.Width = panelGraph.Width - 100;
			panel.Height = panelGraph.Height - 100;
			panel.Left = 50;
			panel.Top = 50;

			performer = panel.Performer;
			/*
			performer.InternalInsets[0, 0] = 20;
			performer.InternalInsets[1, 0] = 20;
			performer.InternalInsets[0, 1] = 20;
			performer.InternalInsets[1, 1] = 20;
			*/
			panelGraph.Controls.Add(panel);
			panel.Dock = DockStyle.Fill;
			performer.Resize();
			coordinator = new LogarithmCoordinator(performer);
			performer.Coordinator = coordinator;
			EditorReceiver.AddEditorDrag(panelGraph);
			PictureReceiver.AddImageDrag(panelGraph);
			ArrayList graphControls = consumer.GraphControls;
			ControlPanel.LoadControls(panelGraph, graphControls);
			textBoxStart.Text = consumer.Start + "";
			textBoxStep.Text = consumer.Step + "";
			textBoxStepCount.Text = consumer.Steps + "";
            pic.Text = ResourceService.Resources.GetControlResource("Color");
            pic.Width = 50;
            FormSeries.FillTypeCombo(toolStripButtonType);
            IDesktop d = label.Desktop;
            List<string> series = FormSeries.GetSeries(d);
            DefaultForm.FillCombo(toolStripButtonSeries, series);


            
            toolStripMain.Items.Add(pic);
		}

		/// <summary>
		/// Updates form UI
		/// </summary>
		public void UpdateFormUI()
		{
			Text = label.RootName;//NamedComponent.GetText(label);
			int y = 0;
			comboBoxArg.Items.Clear();
			for (int i = 0; i < consumer.Count; i++)
			{
				
				IMeasurements arrow = consumer[i];
				PanelMeasureGraph panel = new PanelMeasureGraph(arrow, consumer);
				panel.Width = panelMea.Width - 10;
				Panel sep = new Panel();
				sep.Width = panel.Width;
				sep.BackColor = Color.Black;
				sep.Height = 3;
				sep.Left = 0;
				sep.Top = y;

				panelMea.Controls.Add(sep);
				y += sep.Height;
				panelMea.Controls.Add(panel);
				panel.Left = 0;
				panel.Top = y;
				y += panel.Height + 1;
				string name = panel.PanelName;
				for (int j = 0; j < arrow.Count; j++)
				{
					IMeasure m = arrow[j];
					comboBoxArg.Items.Add(name + "." + m.Name);
				}
			}
		}

		public IMeasure Argument
		{
			get
			{
				if (this.checkBoxTime.Checked)
				{
					return DataConsumer.TimeMeasure;
				}
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureGraph))
					{
						continue;
					}
					PanelMeasureGraph p = c as PanelMeasureGraph;
					if (comboBoxArg.SelectedItem == null)
					{
						throw new Exception(UndefinedArgument);
					}
					IMeasure m = p.GetArgument(comboBoxArg.SelectedItem.ToString());
					if (m != null)
					{
						return m;
					}
				}
				throw new Exception(UndefinedArgument);
			}
		}

		public ArrayList Parameters
		{
			get
			{
				IMeasure arg = Argument;
				ArrayList list = new ArrayList();
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureGraph))
					{
						continue;
					}
					PanelMeasureGraph p = c as PanelMeasureGraph;
					string argName = "";
					if (checkBoxTime.Checked)
					{
						argName = "Time";
					}
					else
					{
						argName = comboBoxArg.SelectedItem.ToString();
					}
					ArrayList par = p.CreateSeries(arg, argName);
					if (par != null)
					{
						for (int i = 0; i < par.Count; i++)
						{
							object[] o = par[i] as object[];
							list.Add(o);
						}
					}
				}
				return list;
			}
		}


		public Hashtable Measurements
		{
			get
			{
				IMeasure arg = Argument;
				Hashtable table = new Hashtable();
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureGraph))
					{
						continue;
					}
					PanelMeasureGraph p = c as PanelMeasureGraph;
					string argName = "";
					if (checkBoxTime.Checked)
					{
						argName = "Time";
					}
					else
					{
						argName = comboBoxArg.SelectedItem.ToString();
					}
					Hashtable par = p.CreateMeasurements(arg);
					if (par != null)
					{
						foreach (object o in par.Keys)
						{
							table[o] = par[o];
						}
					}
				}
				return table;
			}
		}


		/// <summary>
		/// Creates correspond xml
		/// </summary>
		/// <param name="doc">document to create element</param>
		/// <returns>The created element</returns>
		public XmlElement CreateXml(XmlDocument doc)
		{
			/*
			XmlElement el = doc.CreateElement("Plotter");
			XmlElement p = doc.CreateElement("Separate");
			for (int i = 0; i < performer.Count; i++)
			{
				Series s = performer[i];
				XmlElement es = s.CreateXml(doc);
				p.AppendChild(es);
			}
			el.AppendChild(p);
			XmlElement c = doc.CreateElement("Common");
			if (performer.Count > 0)
			{
				int n = performer[0].Count;
				for (int i = 0; i < n; i++)
				{
					XmlElement cp = doc.CreateElement("CommonPoint");
					for (int j = 0; j < performer.Count; j++)
					{
						XmlElement ec = performer[j].CreateXmlPoint(i, doc);
						cp.AppendChild(ec);
					}
					c.AppendChild(cp);
				}
			}
			el.AppendChild(c);
			return el;*/
			return null;
		}

		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()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormGraph));
            this.openFileDialogSeries = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialogSeries = new System.Windows.Forms.SaveFileDialog();
            this.saveFileDialogXml = new System.Windows.Forms.SaveFileDialog();
            this.panelLeft = new System.Windows.Forms.Panel();
            this.panelBottom = new System.Windows.Forms.Panel();
            this.buttonChooseSeries = new System.Windows.Forms.Button();
            this.label6 = new System.Windows.Forms.Label();
            this.labelSeries = new System.Windows.Forms.Label();
            this.panelChooseMode = new System.Windows.Forms.Panel();
            this.radioButtonRepeatingSeries = new System.Windows.Forms.RadioButton();
            this.radioButtonFixed = new System.Windows.Forms.RadioButton();
            this.checkBoxTime = new System.Windows.Forms.CheckBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBoxStart = new System.Windows.Forms.TextBox();
            this.textBoxStepCount = new System.Windows.Forms.TextBox();
            this.buttonStart = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.textBoxStep = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.comboBoxArg = new System.Windows.Forms.ComboBox();
            this.panelRight = new System.Windows.Forms.Panel();
            this.panelCenter = new System.Windows.Forms.Panel();
            this.panelGraphCenter = new System.Windows.Forms.Panel();
            this.panelGraph = new System.Windows.Forms.Panel();
            this.panelGraphTop = new System.Windows.Forms.Panel();
            this.panelIntRight = new System.Windows.Forms.Panel();
            this.panelMeaCenter = new System.Windows.Forms.Panel();
            this.panelMea = new System.Windows.Forms.Panel();
            this.panelMeaBottom = new System.Windows.Forms.Panel();
            this.panelMeaRight = new System.Windows.Forms.Panel();
            this.panelMeaLeft = new System.Windows.Forms.Panel();
            this.panelMeaTop = new System.Windows.Forms.Panel();
            this.label5 = new System.Windows.Forms.Label();
            this.panelIntLeft = new System.Windows.Forms.Panel();
            this.panelCheckBottom = new System.Windows.Forms.Panel();
            this.checkBoxLogX = new System.Windows.Forms.CheckBox();
            this.checkBoxInvertX = new System.Windows.Forms.CheckBox();
            this.panelTopCheck = new System.Windows.Forms.Panel();
            this.panelTop = new System.Windows.Forms.Panel();
            this.checkBoxLogY = new System.Windows.Forms.CheckBox();
            this.checkBoxInvertY = new System.Windows.Forms.CheckBox();
            this.labelT = new System.Windows.Forms.Label();
            this.menuStripMain = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.addseriesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.clearallToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripMain = new System.Windows.Forms.ToolStrip();
            this.toolStripButtonAdd = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonClearAll = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonType = new System.Windows.Forms.ToolStripComboBox();
            this.toolStripButtonSeries = new System.Windows.Forms.ToolStripComboBox();
            this.panelBottom.SuspendLayout();
            this.panelChooseMode.SuspendLayout();
            this.panelCenter.SuspendLayout();
            this.panelGraphCenter.SuspendLayout();
            this.panelIntRight.SuspendLayout();
            this.panelMeaCenter.SuspendLayout();
            this.panelMeaTop.SuspendLayout();
            this.panelCheckBottom.SuspendLayout();
            this.panelTop.SuspendLayout();
            this.menuStripMain.SuspendLayout();
            this.toolStripMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // openFileDialogSeries
            // 
            this.openFileDialogSeries.Filter = "Graph files |*.gra";
            // 
            // saveFileDialogSeries
            // 
            this.saveFileDialogSeries.Filter = "Graph files |*.gra";
            // 
            // saveFileDialogXml
            // 
            this.saveFileDialogXml.Filter = "XML files |*.xml";
            // 
            // panelLeft
            // 
            this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelLeft.Location = new System.Drawing.Point(0, 49);
            this.panelLeft.Name = "panelLeft";
            this.panelLeft.Size = new System.Drawing.Size(8, 640);
            this.panelLeft.TabIndex = 22;
            // 
            // panelBottom
            // 
            this.panelBottom.Controls.Add(this.buttonChooseSeries);
            this.panelBottom.Controls.Add(this.label6);
            this.panelBottom.Controls.Add(this.labelSeries);
            this.panelBottom.Controls.Add(this.panelChooseMode);
            this.panelBottom.Controls.Add(this.checkBoxTime);
            this.panelBottom.Controls.Add(this.label4);
            this.panelBottom.Controls.Add(this.textBoxStart);
            this.panelBottom.Controls.Add(this.textBoxStepCount);
            this.panelBottom.Controls.Add(this.buttonStart);
            this.panelBottom.Controls.Add(this.label3);
            this.panelBottom.Controls.Add(this.label1);
            this.panelBottom.Controls.Add(this.textBoxStep);
            this.panelBottom.Controls.Add(this.label2);
            this.panelBottom.Controls.Add(this.comboBoxArg);
            this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelBottom.Location = new System.Drawing.Point(8, 537);
            this.panelBottom.Name = "panelBottom";
            this.panelBottom.Size = new System.Drawing.Size(992, 152);
            this.panelBottom.TabIndex = 23;
            // 
            // buttonChooseSeries
            // 
            this.buttonChooseSeries.Location = new System.Drawing.Point(560, 64);
            this.buttonChooseSeries.Name = "buttonChooseSeries";
            this.buttonChooseSeries.Size = new System.Drawing.Size(75, 23);
            this.buttonChooseSeries.TabIndex = 32;
            this.buttonChooseSeries.Text = "Choose";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(400, 64);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(136, 23);
            this.label6.TabIndex = 31;
            this.label6.Text = "Series";
            // 
            // labelSeries
            // 
            this.labelSeries.Location = new System.Drawing.Point(400, 104);
            this.labelSeries.Name = "labelSeries";
            this.labelSeries.Size = new System.Drawing.Size(184, 23);
            this.labelSeries.TabIndex = 30;
            // 
            // panelChooseMode
            // 
            this.panelChooseMode.Controls.Add(this.radioButtonRepeatingSeries);
            this.panelChooseMode.Controls.Add(this.radioButtonFixed);
            this.panelChooseMode.Location = new System.Drawing.Point(680, 64);
            this.panelChooseMode.Name = "panelChooseMode";
            this.panelChooseMode.Size = new System.Drawing.Size(264, 72);
            this.panelChooseMode.TabIndex = 29;
            // 
            // radioButtonRepeatingSeries
            // 
            this.radioButtonRepeatingSeries.Location = new System.Drawing.Point(8, 40);
            this.radioButtonRepeatingSeries.Name = "radioButtonRepeatingSeries";
            this.radioButtonRepeatingSeries.Size = new System.Drawing.Size(240, 24);
            this.radioButtonRepeatingSeries.TabIndex = 1;
            this.radioButtonRepeatingSeries.Text = "Repeating series";
            // 
            // radioButtonFixed
            // 
            this.radioButtonFixed.Checked = true;
            this.radioButtonFixed.Location = new System.Drawing.Point(8, 8);
            this.radioButtonFixed.Name = "radioButtonFixed";
            this.radioButtonFixed.Size = new System.Drawing.Size(240, 24);
            this.radioButtonFixed.TabIndex = 0;
            this.radioButtonFixed.Text = "Fixed step";
            // 
            // checkBoxTime
            // 
            this.checkBoxTime.Location = new System.Drawing.Point(96, 56);
            this.checkBoxTime.Name = "checkBoxTime";
            this.checkBoxTime.Size = new System.Drawing.Size(192, 24);
            this.checkBoxTime.TabIndex = 28;
            this.checkBoxTime.Text = "Time as argument";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(256, 24);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(56, 23);
            this.label4.TabIndex = 27;
            this.label4.Text = "Start";
            // 
            // textBoxStart
            // 
            this.textBoxStart.Location = new System.Drawing.Point(320, 24);
            this.textBoxStart.Name = "textBoxStart";
            this.textBoxStart.Size = new System.Drawing.Size(64, 20);
            this.textBoxStart.TabIndex = 26;
            this.textBoxStart.Text = "0";
            // 
            // textBoxStepCount
            // 
            this.textBoxStepCount.Location = new System.Drawing.Point(712, 24);
            this.textBoxStepCount.Name = "textBoxStepCount";
            this.textBoxStepCount.Size = new System.Drawing.Size(40, 20);
            this.textBoxStepCount.TabIndex = 23;
            this.textBoxStepCount.Text = "2";
            // 
            // buttonStart
            // 
            this.buttonStart.Location = new System.Drawing.Point(824, 24);
            this.buttonStart.Name = "buttonStart";
            this.buttonStart.Size = new System.Drawing.Size(75, 23);
            this.buttonStart.TabIndex = 25;
            this.buttonStart.Text = "Start";
            this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(568, 24);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(136, 23);
            this.label3.TabIndex = 24;
            this.label3.Text = "Step Count";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(16, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 23);
            this.label1.TabIndex = 20;
            this.label1.Text = "Argument";
            // 
            // textBoxStep
            // 
            this.textBoxStep.Location = new System.Drawing.Point(472, 24);
            this.textBoxStep.Name = "textBoxStep";
            this.textBoxStep.Size = new System.Drawing.Size(56, 20);
            this.textBoxStep.TabIndex = 21;
            this.textBoxStep.Text = "1";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(416, 24);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(40, 23);
            this.label2.TabIndex = 22;
            this.label2.Text = "Step";
            // 
            // comboBoxArg
            // 
            this.comboBoxArg.FormattingEnabled = true;
            this.comboBoxArg.ItemHeight = 13;
            this.comboBoxArg.Location = new System.Drawing.Point(96, 24);
            this.comboBoxArg.Name = "comboBoxArg";
            this.comboBoxArg.Size = new System.Drawing.Size(121, 21);
            this.comboBoxArg.TabIndex = 19;
            // 
            // panelRight
            // 
            this.panelRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panelRight.Location = new System.Drawing.Point(992, 49);
            this.panelRight.Name = "panelRight";
            this.panelRight.Size = new System.Drawing.Size(8, 488);
            this.panelRight.TabIndex = 24;
            // 
            // panelCenter
            // 
            this.panelCenter.Controls.Add(this.panelGraphCenter);
            this.panelCenter.Controls.Add(this.panelIntRight);
            this.panelCenter.Controls.Add(this.panelIntLeft);
            this.panelCenter.Controls.Add(this.panelCheckBottom);
            this.panelCenter.Controls.Add(this.panelTopCheck);
            this.panelCenter.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelCenter.Location = new System.Drawing.Point(8, 49);
            this.panelCenter.Name = "panelCenter";
            this.panelCenter.Size = new System.Drawing.Size(984, 488);
            this.panelCenter.TabIndex = 25;
            // 
            // panelGraphCenter
            // 
            this.panelGraphCenter.Controls.Add(this.panelGraph);
            this.panelGraphCenter.Controls.Add(this.panelGraphTop);
            this.panelGraphCenter.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelGraphCenter.Location = new System.Drawing.Point(8, 40);
            this.panelGraphCenter.Name = "panelGraphCenter";
            this.panelGraphCenter.Size = new System.Drawing.Size(712, 416);
            this.panelGraphCenter.TabIndex = 28;
            // 
            // panelGraph
            // 
            this.panelGraph.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.panelGraph.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelGraph.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelGraph.Location = new System.Drawing.Point(0, 8);
            this.panelGraph.Name = "panelGraph";
            this.panelGraph.Size = new System.Drawing.Size(712, 408);
            this.panelGraph.TabIndex = 2;
            // 
            // panelGraphTop
            // 
            this.panelGraphTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelGraphTop.Location = new System.Drawing.Point(0, 0);
            this.panelGraphTop.Name = "panelGraphTop";
            this.panelGraphTop.Size = new System.Drawing.Size(712, 8);
            this.panelGraphTop.TabIndex = 1;
            // 
            // panelIntRight
            // 
            this.panelIntRight.Controls.Add(this.panelMeaCenter);
            this.panelIntRight.Controls.Add(this.panelMeaBottom);
            this.panelIntRight.Controls.Add(this.panelMeaRight);
            this.panelIntRight.Controls.Add(this.panelMeaLeft);
            this.panelIntRight.Controls.Add(this.panelMeaTop);
            this.panelIntRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panelIntRight.Location = new System.Drawing.Point(720, 40);
            this.panelIntRight.Name = "panelIntRight";
            this.panelIntRight.Size = new System.Drawing.Size(264, 416);
            this.panelIntRight.TabIndex = 27;
            // 
            // panelMeaCenter
            // 
            this.panelMeaCenter.Controls.Add(this.panelMea);
            this.panelMeaCenter.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelMeaCenter.Location = new System.Drawing.Point(8, 48);
            this.panelMeaCenter.Name = "panelMeaCenter";
            this.panelMeaCenter.Size = new System.Drawing.Size(248, 360);
            this.panelMeaCenter.TabIndex = 26;
            // 
            // panelMea
            // 
            this.panelMea.AutoScroll = true;
            this.panelMea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panelMea.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelMea.Location = new System.Drawing.Point(0, 0);
            this.panelMea.Name = "panelMea";
            this.panelMea.Size = new System.Drawing.Size(248, 360);
            this.panelMea.TabIndex = 22;
            // 
            // panelMeaBottom
            // 
            this.panelMeaBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelMeaBottom.Location = new System.Drawing.Point(8, 408);
            this.panelMeaBottom.Name = "panelMeaBottom";
            this.panelMeaBottom.Size = new System.Drawing.Size(248, 8);
            this.panelMeaBottom.TabIndex = 25;
            // 
            // panelMeaRight
            // 
            this.panelMeaRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panelMeaRight.Location = new System.Drawing.Point(256, 48);
            this.panelMeaRight.Name = "panelMeaRight";
            this.panelMeaRight.Size = new System.Drawing.Size(8, 368);
            this.panelMeaRight.TabIndex = 24;
            // 
            // panelMeaLeft
            // 
            this.panelMeaLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelMeaLeft.Location = new System.Drawing.Point(0, 48);
            this.panelMeaLeft.Name = "panelMeaLeft";
            this.panelMeaLeft.Size = new System.Drawing.Size(8, 368);
            this.panelMeaLeft.TabIndex = 23;
            // 
            // panelMeaTop
            // 
            this.panelMeaTop.Controls.Add(this.label5);
            this.panelMeaTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelMeaTop.Location = new System.Drawing.Point(0, 0);
            this.panelMeaTop.Name = "panelMeaTop";
            this.panelMeaTop.Size = new System.Drawing.Size(264, 48);
            this.panelMeaTop.TabIndex = 22;
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(56, 16);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(200, 23);
            this.label5.TabIndex = 21;
            this.label5.Text = "Data sources";
            // 
            // panelIntLeft
            // 
            this.panelIntLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelIntLeft.Location = new System.Drawing.Point(0, 40);
            this.panelIntLeft.Name = "panelIntLeft";
            this.panelIntLeft.Size = new System.Drawing.Size(8, 416);
            this.panelIntLeft.TabIndex = 26;
            // 
            // panelCheckBottom
            // 
            this.panelCheckBottom.Controls.Add(this.checkBoxLogX);
            this.panelCheckBottom.Controls.Add(this.checkBoxInvertX);
            this.panelCheckBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelCheckBottom.Location = new System.Drawing.Point(0, 456);
            this.panelCheckBottom.Name = "panelCheckBottom";
            this.panelCheckBottom.Size = new System.Drawing.Size(984, 32);
            this.panelCheckBottom.TabIndex = 25;
            // 
            // checkBoxLogX
            // 
            this.checkBoxLogX.Location = new System.Drawing.Point(422, 4);
            this.checkBoxLogX.Name = "checkBoxLogX";
            this.checkBoxLogX.Size = new System.Drawing.Size(152, 24);
            this.checkBoxLogX.TabIndex = 29;
            this.checkBoxLogX.Text = "Logarithmic scale";
            this.checkBoxLogX.CheckedChanged += new System.EventHandler(this.checkBoxLogX_CheckedChanged);
            // 
            // checkBoxInvertX
            // 
            this.checkBoxInvertX.Location = new System.Drawing.Point(592, 8);
            this.checkBoxInvertX.Name = "checkBoxInvertX";
            this.checkBoxInvertX.Size = new System.Drawing.Size(168, 24);
            this.checkBoxInvertX.TabIndex = 23;
            this.checkBoxInvertX.Text = "Invert x - axis";
            // 
            // panelTopCheck
            // 
            this.panelTopCheck.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelTopCheck.Location = new System.Drawing.Point(0, 0);
            this.panelTopCheck.Name = "panelTopCheck";
            this.panelTopCheck.Size = new System.Drawing.Size(984, 40);
            this.panelTopCheck.TabIndex = 24;
            // 
            // panelTop
            // 
            this.panelTop.Controls.Add(this.checkBoxLogY);
            this.panelTop.Controls.Add(this.checkBoxInvertY);
            this.panelTop.Controls.Add(this.labelT);
            this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelTop.Location = new System.Drawing.Point(8, 49);
            this.panelTop.Name = "panelTop";
            this.panelTop.Size = new System.Drawing.Size(984, 42);
            this.panelTop.TabIndex = 26;
            // 
            // checkBoxLogY
            // 
            this.checkBoxLogY.Location = new System.Drawing.Point(472, 9);
            this.checkBoxLogY.Name = "checkBoxLogY";
            this.checkBoxLogY.Size = new System.Drawing.Size(152, 24);
            this.checkBoxLogY.TabIndex = 29;
            this.checkBoxLogY.Text = "Logarithmic scale";
            this.checkBoxLogY.CheckedChanged += new System.EventHandler(this.checkBoxLogY_CheckedChanged);
            // 
            // checkBoxInvertY
            // 
            this.checkBoxInvertY.Location = new System.Drawing.Point(630, 9);
            this.checkBoxInvertY.Name = "checkBoxInvertY";
            this.checkBoxInvertY.Size = new System.Drawing.Size(176, 24);
            this.checkBoxInvertY.TabIndex = 28;
            this.checkBoxInvertY.Text = "Invert y - axis";
            this.checkBoxInvertY.CheckedChanged += new System.EventHandler(this.checkBoxInvertY_CheckedChanged);
            // 
            // labelT
            // 
            this.labelT.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelT.Location = new System.Drawing.Point(6, 14);
            this.labelT.Name = "labelT";
            this.labelT.Size = new System.Drawing.Size(456, 23);
            this.labelT.TabIndex = 21;
            this.labelT.Text = "label6";
            // 
            // menuStripMain
            // 
            this.menuStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.toolsToolStripMenuItem});
            this.menuStripMain.Location = new System.Drawing.Point(0, 0);
            this.menuStripMain.Name = "menuStripMain";
            this.menuStripMain.Size = new System.Drawing.Size(1000, 24);
            this.menuStripMain.TabIndex = 27;
            this.menuStripMain.Text = "menuStripMain";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Text = "File";
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Text = "Exit";
            // 
            // toolsToolStripMenuItem
            // 
            this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.addseriesToolStripMenuItem,
            this.clearallToolStripMenuItem});
            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.Text = "Tools";
            // 
            // addseriesToolStripMenuItem
            // 
            this.addseriesToolStripMenuItem.Name = "addseriesToolStripMenuItem";
            this.addseriesToolStripMenuItem.Text = "Add series";
            this.addseriesToolStripMenuItem.Click += new System.EventHandler(this.addseriesToolStripMenuItem_Click);
            // 
            // clearallToolStripMenuItem
            // 
            this.clearallToolStripMenuItem.Name = "clearallToolStripMenuItem";
            this.clearallToolStripMenuItem.Text = "Clear all";
            // 
            // toolStripMain
            // 
            this.toolStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButtonAdd,
            this.toolStripButtonClearAll,
            this.toolStripButtonType,
            this.toolStripButtonSeries});
            this.toolStripMain.Location = new System.Drawing.Point(0, 24);
            this.toolStripMain.Name = "toolStripMain";
            this.toolStripMain.Size = new System.Drawing.Size(1000, 25);
            this.toolStripMain.TabIndex = 28;
            this.toolStripMain.Text = "toolStrip1";
            // 
            // toolStripButtonAdd
            // 
            this.toolStripButtonAdd.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonAdd.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonAdd.Image")));
            this.toolStripButtonAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonAdd.Name = "toolStripButtonAdd";
            this.toolStripButtonAdd.Text = "toolStripButtonAdd";
            this.toolStripButtonAdd.Click += new System.EventHandler(this.toolStripButtonAdd_Click);
            // 
            // toolStripButtonClearAll
            // 
            this.toolStripButtonClearAll.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonClearAll.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonClearAll.Image")));
            this.toolStripButtonClearAll.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonClearAll.Name = "toolStripButtonClearAll";
            this.toolStripButtonClearAll.Text = "toolStripButton1";
            // 
            // toolStripButtonType
            // 
            this.toolStripButtonType.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonType.Name = "toolStripButtonType";
            this.toolStripButtonType.Size = new System.Drawing.Size(121, 25);
            this.toolStripButtonType.Text = "<Series type>";
            // 
            // toolStripButtonSeries
            // 
            this.toolStripButtonSeries.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonSeries.Name = "toolStripButtonSeries";
            this.toolStripButtonSeries.Size = new System.Drawing.Size(121, 25);
            this.toolStripButtonSeries.Text = "<Series>";
            // 
            // FormGraph
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1000, 689);
            this.Controls.Add(this.panelTop);
            this.Controls.Add(this.panelCenter);
            this.Controls.Add(this.panelRight);
            this.Controls.Add(this.panelBottom);
            this.Controls.Add(this.panelLeft);
            this.Controls.Add(this.toolStripMain);
            this.Controls.Add(this.menuStripMain);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "FormGraph";
            this.Text = "FormGraph";
            this.panelBottom.ResumeLayout(false);
            this.panelBottom.PerformLayout();
            this.panelChooseMode.ResumeLayout(false);
            this.panelCenter.ResumeLayout(false);
            this.panelGraphCenter.ResumeLayout(false);
            this.panelIntRight.ResumeLayout(false);
            this.panelMeaCenter.ResumeLayout(false);
            this.panelMeaTop.ResumeLayout(false);
            this.panelCheckBottom.ResumeLayout(false);
            this.panelTop.ResumeLayout(false);
            this.menuStripMain.ResumeLayout(false);
            this.toolStripMain.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			try
			{
				DataPerformerStrategy.Object.PrepareAll(label.Desktop.Root);
				consumer.Start = Double.Parse(textBoxStart.Text);
				consumer.Step = Double.Parse(textBoxStep.Text);
				consumer.Steps = Int32.Parse(textBoxStepCount.Text);
				consumer.GraphControls = ControlPanel.GetControls(panelGraph);
				if (radioButtonFixed.Checked)
				{
					performFixed();
					return;
				}
				performSeries();
			}
			catch (Exception ex)
			{
				DiagramUI.DefaultForm.ShowError(this, ex);
			}
		}

		private void performFixed()
		{
			Exception ex = null;
			ArrayList series = new ArrayList();
			measurements = Measurements;
			try
			{
				double start = consumer.Start;
				double step = consumer.Step;
				int count = consumer.Steps;
				IDesktop d = label.Desktop;
				DifferentialEquationProcessor processor = DifferentialEquationProcessor.Object;
                if (processor != null)
                {
                    processor.Set(DataConsumer.GetDataConsumers(d));
                }
				IMeasure arg = Argument;
				if (arg == null)
				{
					return;
				}
                removeOwn();
				series = Parameters;
				for (int i = 0; i < count; i++)
				{
					DataPerformerStrategy.Object.Step = i;
					if (i == 0)
					{
						DataPerformerStrategy.Object.StartAll(d);
					}
					DataPerformerStrategy.Object.UpdateAll(d);
					DataPerformerStrategy.Object.Time = start + i * step;
					
					if (i > 0 & processor != null)
					{
						processor.Step(start + i * step - step, start + i * step);
					}
					DataPerformerStrategy.Object.Time = start + i * step;
					consumer.UpdateChildrenData();
					for (int j = 0; j < series.Count; j++)
					{
						object[] o = series[j] as object[];
						ParametrizedSeries s = o[0] as ParametrizedSeries;
						s.Step();
					}
					foreach (object[] o in measurements.Values)
					{
						DoubleArrayFunction f = o[0] as DoubleArrayFunction;
						IMeasure x = o[1] as IMeasure;
						IMeasure y = o[2] as IMeasure;
						double xx = (double) x.Parameter();
						f[xx] = y.Parameter();
					}
					DataConsumer.ResetUpdatedMeasurements(d);
				}
			}
			catch (Exception e)
			{
				ex = e;
			}

            foreach (object[] o in series)
            {
                DataPerformerUI.ParametrizedSeries s = o[0] as ParametrizedSeries;
                //            Series ser = new Series();
                //			ser.CopyFrom(s);
                performer.AddSeries(s, (Color)o[2]);
                ownSeries.Add(s);
            }

			performer.RefreshAll();
			if (ex != null)
			{
				throw ex;
			}
		}

		private void performSeries()
		{
			double start = consumer.Start;
			double step = consumer.Step;
			int count = consumer.Steps;
			IDesktop d = label.Desktop;
			DifferentialEquationProcessor processor = DifferentialEquationProcessor.Object;
			processor.Set(DataConsumer.GetDataConsumers(d));
			IMeasure arg = Argument;
			if (arg == null)
			{
				return;
			}
            removeOwn();
			ArrayList series = Parameters;
			foreach (ParametrizedSeries s in series)
			{
				performer.AddSeries(s, Color.Black);
                ownSeries.Add(s);
			}
			for (int i = 0; i < count; i++)
			{
				double stepTime = start + i * seriesStep[seriesStep.Count - 1, 0];
				double localTime = stepTime;
				for (int k = 0; k < seriesStep.Count; k++)
				{
					DataPerformerStrategy.Object.Time = localTime;
					if (i == 0 & k == 0)
					{
						DataPerformerStrategy.Object.StartAll(d);
					}
					DataPerformerStrategy.Object.UpdateAll(d);
					//factory.UpdateForms();
					double oldTime = localTime;
					if (k > 0)
					{
						localTime = stepTime + seriesStep[k, 0];
						processor.Step(oldTime, localTime);
					}
					if (i == 0 | k > 0)
					{
						DataPerformerStrategy.Object.Time = start + localTime;
						consumer.UpdateChildrenData();
						for (int j = 0; j < performer.Count; j++)
						{
							ParametrizedSeries s = performer[j] as ParametrizedSeries;
							s.Step();
						}
						DataConsumer.ResetUpdatedMeasurements(d);
					}
				}
			}
			performer.RefreshAll();
		}


        private void removeOwn()
        {
            foreach (ISeries s in ownSeries)
            {
                performer.Remove(s);
            }
            ownSeries.Clear();
        }
	

		private void checkBoxInvertY_CheckedChanged(object sender, System.EventArgs e)
		{
			performer.InvertY = checkBoxInvertY.Checked;
			performer.RefreshAll();
		}

		private void checkBoxInvertX_CheckedChanged(object sender, System.EventArgs e)
		{
			performer.InvertX = checkBoxInvertX.Checked;
			performer.RefreshAll();
		
		}

		private void buttonChooseSeries_Click(object sender, System.EventArgs e)
		{
			if (openFileDialogSeries.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			string fn = openFileDialogSeries.FileName;
			Stream stream = File.OpenRead(fn);
			BinaryFormatter bformatter = new BinaryFormatter();
			seriesStep = (Series)bformatter.Deserialize(stream);
			labelSeries.Text = fn;

		}

		private void radioButtonRepeatingSeries_CheckedChanged(object sender, System.EventArgs e)
		{
			try
			{
				if (seriesStep == null)
				{
					throw new Exception("Choose series, please");
				}
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
				radioButtonFixed.Checked = true;
			}
		}


		private void menuItemSaveAs_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (performer.Count == 0)
				{
					return;
				}
				if (saveFileDialogSeries.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				string fn = saveFileDialogSeries.FileName;
				Stream stream = File.OpenWrite(fn);
				BinaryFormatter bformatter = new BinaryFormatter();
				DataPerformer.Series s = new DataPerformer.Series();
				s.CopyFrom(performer[0] as Series);
				bformatter.Serialize(stream, s);
				stream.Close();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void menuItemSaveAsXml_Click(object sender, System.EventArgs e)
		{
			try
			{
				/*
				string xmlDir = ElectromagneticUIFactory.CurrentDirectory + "Xml\\";
				saveFileDialogXml.InitialDirectory = xmlDir;
				if (saveFileDialogXml.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				string fn = saveFileDialogXml.FileName;
				XmlDocument doc = new XmlDocument();
				doc.LoadXml("<?xml version=\"1.0\"?><Root></Root>");
				XmlElement el = consumer.CreateXml(doc);
				doc.DocumentElement.AppendChild(el);
				XmlWriter writer = new XmlTextWriter(fn, new System.Text.UTF8Encoding());
				doc.WriteContentTo(writer);
				writer.Flush();
				writer.Close();*/
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void menuItemSaveXMLArrays_Click(object sender, System.EventArgs e)
		{
			consumer.Reset();
			consumer.UpdateChildrenData();
			XmlDocument doc = DataConsumer.GetArraysXmlDocument(consumer);
			if (doc == null)
			{
				return;
			}
			if (saveFileDialogXml.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			XmlWriter writer = new XmlTextWriter(saveFileDialogXml.FileName, new System.Text.UTF8Encoding());
			doc.WriteContentTo(writer);
			writer.Flush();
			writer.Close();
		}

		private void checkBoxLogY_CheckedChanged(object sender, System.EventArgs e)
		{
			coordinator.LogY = checkBoxLogY.Checked;
			performer.RefreshAll();
		}

		private void checkBoxLogX_CheckedChanged(object sender, System.EventArgs e)
		{
			coordinator.LogX = checkBoxLogX.Checked;
			performer.RefreshAll();
		}

        private void addseriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDesktop d = label.Desktop;
            ISeriesPainter painter = FormSeries.SelectPainter(toolStripButtonType.SelectedItem + "", pic.Color, performer);
            if (painter == null)
            {
                return;
            }
            object o = d.GetObject(toolStripButtonSeries.SelectedItem + "");
            if (o == null)
            {
                return;
            }
            ISeries series = null;
            if (o is ISeries)
            {
                series = o as ISeries;
                if (series is DrawSeries)
                {
                    DrawSeries ds = series as DrawSeries;
                    painter = ds.Factory.GetPainter(performer);
                }
            }
            if (o is Series)
            {
                Series s = o as Series;
                series = new SeriesGraph(s);
            }
            performer.AddSeries(series, painter);
            performer.RefreshAll();
            Refresh();
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            addseriesToolStripMenuItem_Click(sender, e);
        }

 	}
}
