using System;
using System.Collections;
using System.Drawing;
using System.Runtime.Serialization;
using System.Windows.Forms;

using DataPerformer;


namespace DataPerformerUI
{
	public class PanelMeasureGraph : Panel
	{

		protected IMeasurements measurements;
		protected ArrayList measurementControls;
		private string panelName;
		private ArrayList series = new ArrayList();
		private Hashtable mea = new Hashtable();
		private DataConsumer consumer;
        public PanelMeasureGraph(IMeasurements measurements, DataConsumer consumer)
		{
            this.measurements = measurements;
			this.consumer = consumer;
			initialize();
		}
		
		public ArrayList CreateSeries(IMeasure x, string argName)
		{
			series.Clear();
			foreach(object[] o in measurementControls)
			{
                CheckBox cb = o[0] as CheckBox;
				if (!cb.Checked)
				{
					continue;
				}
				IMeasure y = o[1] as IMeasure;
				object t = y.Type;
				Double a = 0;
				if (!t.Equals(a))
				{
					continue;
				}
                DataPerformerUI.ParametrizedSeries s = new DataPerformerUI.ParametrizedSeries(x.Parameter, y.Parameter);
                //DataPerformerUI.Series ser = new DataPerformerUI.Series();
				//ser.CopyFrom(s);
				s.X = argName;
				s.Y = panelName + "." + y.Name;
				//s.PenColor = cb.BackColor;
                OfficePickers.ColorPicker.ComboBoxColorPicker pic = o[3] as OfficePickers.ColorPicker.ComboBoxColorPicker;
				series.Add(new object[]{s, o[2], pic.Color});
			}
			return series;
		}

		public Hashtable CreateMeasurements(IMeasure x)
		{
			mea.Clear();
			foreach(object[] o in measurementControls)
			{
				CheckBox cb = o[0] as CheckBox;
				if (!cb.Checked)
				{
					continue;
				}
				IMeasure y = o[1] as IMeasure;
				object t = y.Type;
				Double a = 0;
				if (t.Equals(a))
				{
					continue;
				}
				DoubleArrayFunction f = new DoubleArrayFunction(y.Type);
				mea[o[0]] = new object[]{f, x, y};
			}
			return mea;
		}
		
		public string PanelName
		{
			get
			{
				return panelName;
			}
		}
		
		public IMeasure GetArgument(string name)
		{
			foreach(object[] o in measurementControls)
			{
				IMeasure m = o[1] as IMeasure;
				if (name.Equals(panelName + "." + m.Name))
				{
					return m;
				}
			}
			return null;
		}
		
		/// <summary>
		/// Initialization
		/// </summary>
		private void initialize()
		{
			Control panel = HeaderControl.Object.GetHeaderControl(measurements);
			panelName = panel.Name;
			Controls.Add(panel);
			measurementControls = new ArrayList();
			int y = panel.Height + 10;
			for (int i = 0; i < measurements.Count; i++)
			{
				IMeasure measure = measurements[i];
                Label l = new Label();
                OfficePickers.ColorPicker.ComboBoxColorPicker pic = new OfficePickers.ColorPicker.ComboBoxColorPicker();
				CheckBox cb = new CheckBox();
                cb.Text = measure.Name;
                cb.Left = 20;
                cb.Top = y;
				pic.Left = cb.Left;
				pic.Top = cb.Bottom + 5;
				Button b = new Button();
				b.Top = pic.Top;
				b.Left = pic.Right + 10;
				b.Text = ResourceService.Resources.GetResourceString("Save");
				b.Click += new EventHandler(onButtonSaveClick);
				measurementControls.Add(new object[]{cb, measure, b, pic});
                Controls.Add(pic);
                Controls.Add(cb);
                Controls.Add(b);
				y = b.Bottom + 5;
			}
			Width = 250;
			Height = y + 10;
		}

		private void onButtonSaveClick(Object sender, EventArgs e)
		{
			foreach (object[] o in measurementControls)
			{
				if (o[2] == sender)
				{
					if (!mea.ContainsKey(o[0]))
					{
						continue;
					}
					object[] ob = mea[o[0]] as object[];
					Form form = new FormSaveVectorFunction(ob);
					form.Show();
					return;
				}
			}
			foreach (object[] o in series)
			{
				if (o[1] != sender)
				{
					continue;
				}
				Form form = new FormGraphSave(o[0] as DataPerformer.Series, consumer.GraphControls);
				form.Show();
			}
		}
	}
}
