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 Motion6D;
using DataPerformer;

namespace MotionUI
{
    public partial class FormPointsCollection : Form, IUpdatableForm
    {
        private IObjectLabel label;

        PositionCollectionData collection;

        IPositionCollectionMeasureChooser chooser;

        private FormPointsCollection()
        {
            InitializeComponent();
        }

        public FormPointsCollection(IObjectLabel label)
        {
            InitializeComponent();
            ResourceService.Resources.LoadControlResources(this);
            this.label = label;
            collection = label.Object as PositionCollectionData;
            Text = label.Name;
            fill();
            
        }



        #region IUpdatableForm Members

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

        #endregion


        private void fill()
        {
            string[] names = PositionFactory.Factory.Names;
            DefaultForm.FillCombo(comboBoxFactory, names);
            DefaultForm.SelectCombo(comboBoxFactory, collection.FactoryName);
            selectFactory(collection.Factory);
            chooser.Meausements = collection.Measures;
        }

        void selectFactory(IPositionFactory factory)
        {
            if (chooser != null)
            {
                if (chooser is UserControl)
                {
                    UserControl uc = chooser as UserControl;
                    panelContainer.Controls.Remove(uc);
                }
                else
                {
                    Control c = chooser as Control;
                    panelContainer.Controls.Remove(c);
                }
            }
            chooser = SimpleChooser.Chooser[factory];
            if (chooser is UserControl)
            {
                UserControl uc = chooser as UserControl;
                panelContainer.Controls.Add(uc);
                ResourceService.Resources.LoadControlResources(uc);
            }
            else
            {
                Control c = chooser as Control;
                panelContainer.Controls.Add(c);
                ResourceService.Resources.LoadControlResources(c);
            }
            chooser.Consumer = collection;
        }

        void acceptFactory()
        {
            string s = comboBoxFactory.SelectedItem + "";
            if (s.Length == 0)
            {
                return;
            }
            IPositionFactory old = collection.Factory;
            IPositionFactory factory = PositionFactory.Factory[s];
            if (old == factory)
            {
                return;
            }
            collection.Factory = factory;
            selectFactory(factory);
        }

        void accept()
        {
            List<string> mea = chooser.Meausements;
            collection.Measures = mea;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            try
            {
                accept();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBoxFactory_SelectedIndexChanged(object sender, EventArgs e)
        {
            acceptFactory();
        }
    }
}