using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using DataPerformer;
using CategoryTheory;
using DiagramUI;

namespace DataPerformerUI
{
    public class PanelMeasurements : Panel
    {
        private ICollection<string> vars;

        private Dictionary<string, string> selected;

        private IAssociatedObject obj;

        private Dictionary<string, ComboBox> boxes = new Dictionary<string,ComboBox>();

        private IMeasurements measurements;


        public PanelMeasurements(IAssociatedObject obj, IMeasurements measurements, object vars, Dictionary<string, string> selected)
        {
            this.obj = obj;
            this.measurements = measurements;
            this.selected = selected;
            init();
            Vars = vars;
            Selected = selected;
        }


        private void init()
        {
            Control panel = HeaderControl.Object.GetHeaderControl(measurements);
            Controls.Add(panel);
            int y = panel.Height + 10;

            for (int i = 0; i < measurements.Count; i++)
            {
                IMeasure measure = measurements[i];
                Label lab = new Label();
                lab.Top = y;
                lab.Left = 20;
                lab.Text = Measure.GetTypeName(measure.Type);
                y = lab.Bottom + 10;
                Controls.Add(lab);
                ComboBox cb = new ComboBox();
                cb.Location = new System.Drawing.Point(20, y);
                cb.Size = new System.Drawing.Size(121, 21);
                Label l = new Label();
                l.Text = (string)measure.Name.Clone();
                l.Location = new System.Drawing.Point(cb.Left + cb.Width + 10, y);
                l.Width = Width - 20;
                Controls.Add(cb);
                Controls.Add(l);
                y = cb.Bottom + 5;
                string argName = DataConsumer.GetName(obj, measurements)
                    + "." + measure.Name;
                DefaultForm.SelectCombo(cb, argName);
                boxes[argName] = cb;
            }
            Width = 100;
            Height = y + 10;
       }


        internal object Vars
        {
            set
            {
                if (value is ICollection<string>)
                {
                    vars = value as ICollection<string>;
                }
                if (value is string)
                {
                    string s = value as string;
                    vars = new List<string>();
                    foreach (char c in s)
                    {
                        vars.Add(c + "");
                    }
                }
                foreach (ComboBox box in boxes.Values)
                {
                    box.Items.Clear();
                    DefaultForm.FillCombo(box, vars);
                }
            }
        }

        internal Dictionary<string, string> Selected
        {
            get
            {
                Dictionary<string, string> vn = new Dictionary<string, string>();
                foreach (string key in boxes.Keys)
                {
                    ComboBox box = boxes[key];
                    string s = box.SelectedItem + "";
                    if (s.Length == 0)
                    {
                        continue;
                    }
                    if (vn.ContainsKey(s))
                    {
                        throw new Exception("Measurement \"" + s + "\" already exists");
                    }
                    vn[s] = key;
                }
                return vn;
            }
            set
            {
                foreach (string key in value.Keys)
                {
                    string val = value[key];
                    if (!boxes.ContainsKey(val))
                    {
                        continue;
                    }
                    ComboBox box = boxes[value[key]];
                    DefaultForm.SelectCombo(box, key);
                }
            }
        }
    }
}
