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

using DiagramUI;
using DataPerformer;

namespace DataPerformerUI
{
    public partial class FormIterateGLM : Form, IUpdatableForm
    {
        private IObjectLabel comp;
        private IteratorGLM iterator;
        private List<ComboBox> left = new List<ComboBox>();
        private List<ComboBox> right = new List<ComboBox>();
        private List<ComboBox> alias = new List<ComboBox>();
        private List<Label> labs = new List<Label>();
        Control cLeft;
        Control cRight;
        Control cAli;

        public FormIterateGLM(IObjectLabel comp)
        {
            InitializeComponent();
            ResourceService.Resources.LoadControlResources(this);
            this.comp = comp;
            iterator = comp.Object as IteratorGLM;
            cLeft = panelRightCenter;
            cRight = panelRightCenter;
            cAli = panelRightLeft;
            DefaultForm.FillCombo(toolStripButtonNumberAli, 1, 100);
            DefaultForm.FillCombo(toolStripComboBoxOut, 1, 100);
            toolStripButtonNumberAli.SelectedIndex = iterator.AliasesCount - 1;
            toolStripComboBoxOut.SelectedIndex = iterator.DataCount - 1;
            fillOut();
            fillAli();
            selectCombo();
            Text = comp.Name;
        }

        private FormIterateGLM()
        {
            InitializeComponent();
        }

        #region IUpdatableForm Members

        void IUpdatableForm.UpdateFormUI()
        {
            Text = comp.Name;
        }

        #endregion


 
        private void removeData()
        {
            foreach (ComboBox cb in left)
            {
                Control p = cb.Parent;
                p.Controls.Remove(cb);
            }
            left.Clear();
            foreach (ComboBox cb in right)
            {
                Control p = cb.Parent;
                p.Controls.Remove(cb);
            }
            right.Clear();
            foreach (Label l in labs)
            {
                Control p = l.Parent;
                p.Controls.Remove(l);
            }
            labs.Clear();
        }

        private void removeAli()
        {
            foreach (ComboBox box in alias)
            {
                Control p = box.Parent;
                p.Controls.Remove(box);
            }
            alias.Clear();
        }

        void fillOut()
        {
            int y = labelEqu.Bottom + 10;
            removeData();
            int n = (int)toolStripComboBoxOut.SelectedIndex + 1;
            for (int i = 0; i < n; i++)
            {
                ComboBox comboLeft = new ComboBox();
                left.Add(comboLeft);
                fillData(comboLeft);
                comboLeft.Top = y;
                comboLeft.Left = 10;
                comboLeft.Width = 100;
                cLeft.Controls.Add(comboLeft);
                Label l = new Label();
                l.Text = "=";
                l.Left = comboLeft.Left + comboLeft.Width + 5;
                l.Top = comboLeft.Top;
                l.Width = 20;
                labs.Add(l);
                cRight.Controls.Add(l);
                ComboBox comboRight = new ComboBox();
                right.Add(comboRight);
                fillData(comboRight);
                comboRight.Top = y;
                comboRight.Left = l.Right + 5;
                comboRight.Width = 100;
                cRight.Controls.Add(comboRight);
                y = comboLeft.Top + comboLeft.Height + 10;
            }
        }

        void fillAli()
        {
            int y = labelRp.Bottom + 10;
            removeAli();
            int n = toolStripButtonNumberAli.SelectedIndex + 1;
            for (int i = 0; i < n; i++)
            {
                ComboBox box = new ComboBox();
                box.Top = y;
                alias.Add(box);
                fillAlias(box);
                box.Left = labelRp.Left;
                box.Width = 100;
                cAli.Controls.Add(box);
                y = box.Top + box.Height + 10;
            }
        }

        void selectCombo()
        {
            for (int i = 0; i < left.Count; i++)
            {
                ComboBox l = left[i];
                selectCombo(l, iterator.GetLeftName(i));
                ComboBox r = right[i];
                selectCombo(r, iterator.GetRightName(i));
            }
            for (int i = 0; i < alias.Count; i++)
            {
                selectCombo(alias[i], iterator.GetAliasName(i));
            }
        }

        void selectCombo(ComboBox box, string item)
        {
            for (int i = 0; i < box.Items.Count; i++)
            {
                string s = box.Items[i] + "";
                if (s.Equals(item))
                {
                    box.SelectedIndex = i;
                    return;
                }
            }
        }

 
        void fillData(ComboBox box)
        {
            List<string> l = iterator.AllMeasurements;
            foreach (string s in l)
            {
                box.Items.Add(s);
            }
        }

        void fillAlias(ComboBox box)
        {
            List<string> l = iterator.AllAliases;
            foreach (string s in l)
            {
                box.Items.Add(s);
            }
        }

        void accept()
        {
            List<string> l = getComboList(left);
            List<string> ri = getComboList(right);
            List<string> ali = getComboList(alias);
            List<List<string>> r = new List<List<string>>();
            int n = ali.Count;
            double[,] d = new double[n, n];
            double[] dx = new double[n];
            for (int i = 0; i < dx.Length; i++)
            {
                dx[i] = 0.00000001;
            }
            for (int i = 0; i < left.Count; i++)
            {
                List<string> list = new List<string>();
                r.Add(list);
                for (int j = 0; j <= i; j++)
                {
                    list.Add("");
                    d[i, j] = d[j, i] = 0;
                }
            }
            iterator.Set(ali, l, ri, r, dx, d);
        }

        private List<string> getComboList(List<ComboBox> l)
        {
            List<string> list = new List<string>();
            foreach (ComboBox box in l)
            {
                list.Add(box.SelectedItem + "");
            }
            return list;
        }

 
        private void buttonAcceptAll_Click(object sender, EventArgs e)
        {
            accept();
        }

        private void buttonIterate_Click(object sender, EventArgs e)
        {
            double s = iterator.Iterate();
            labelSigma.Text = "Sigma = " + s + "";
        }

        private void fill()
        {
            fillAli();
            fillOut();
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            fill();
        }

        private void buttonAcceptNumber_Click(object sender, EventArgs e)
        {
            fill();
        }

     }
}