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

using CategoryTheory;

namespace DiagramUI
{
    public partial class FormLibraryObject : Form, IUpdatableForm
    {
        IObjectLabel label;
        LibraryObjectWrpapper wrapper;
        string id;
        private FormLibraryObject()
        {
            InitializeComponent();
        }

        public FormLibraryObject(IObjectLabel label, LibraryObjectWrpapper wrapper)
        {
            this.label = label;
            this.wrapper = wrapper;
            InitializeComponent();
            ResourceService.Resources.LoadControlResources(this);
            if (CategoryTheory.BinaryLoader.Object == null || DiagramUI.BinaryChooser.Object == null)
            {
                buttonLoad.Enabled = false;
            }
            UpdateFormUI();
        }

        #region IUpdatableForm Members

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

        #endregion
        void fill()
        {
            if (wrapper.FileName == null)
            {
                return;
            }
            textBoxFileName.Text = wrapper.FileName;
            string[] items = LibraryObjectWrpapper.GetNames(wrapper.FileName);
            int n = 0;
            int sel = 0;
            foreach (string it in items)
            {
                comboBoxName.Items.Add(it);
                if (it.Equals(wrapper.Name))
                {
                    sel = n;
                }
                ++n;
            }
            comboBoxName.SelectedIndex = sel;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialogDLL.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            string fn = openFileDialogDLL.FileName + "";
            string[] names = LibraryObjectWrpapper.GetNames(fn);
            if (names == null)
            {
                return;
            }
            textBoxFileName.Text = fn;
            comboBoxName.Items.Clear();
            foreach (string it in names)
            {
                comboBoxName.Items.Add(it);
            }
            buttonLoad.Enabled = false;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            if (comboBoxName.Items.Count == 0)
            {
                return;
            }
            string it = comboBoxName.SelectedItem + "";
            if (it.Length == 0)
            {
                return;
            }
            if (id != null)
            {
                wrapper.Set(id, it);
            }
            else
            {
                wrapper.Set(textBoxFileName.Text, it);
            }

        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (CategoryTheory.BinaryLoader.Object == null || DiagramUI.BinaryChooser.Object == null)
            {
                return;
            }

            DiagramUI.BinaryChooser.Object.Show(this, "dll");
            if (DiagramUI.BinaryChooser.Object.Name == null)
            {
                return;
            }
            textBoxDB.Text = DiagramUI.BinaryChooser.Object.Name + "";
            id = DiagramUI.BinaryChooser.Object.Id;
            string[] names = LibraryObjectWrpapper.GetNames(id);
            if (names == null)
            {
                return;
            }
            comboBoxName.Items.Clear();
            foreach (string it in names)
            {
                comboBoxName.Items.Add(it);
            }
            buttonBrowse.Enabled = false;
        }
    }
}