using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Aga.Controls.Tree;
using MySQL.Grt;
using MySQL.Utilities;

namespace MySQL.GUI.Workbench.Plugins
{
  public partial class PhysicalLayerEditor : ObjectEditorPlugin
  {
    #region Member Variables

    private MySQL.Grt.LayerEditorBE layerEditorBE { get { return editorBE as MySQL.Grt.LayerEditorBE; } }

    #endregion

    #region Constructors

    protected PhysicalLayerEditor()
    {
      InitializeComponent();
    }

    public PhysicalLayerEditor(GrtManager GrtManager, GrtValue GrtObject)
      : base(new MySQL.Grt.LayerEditorBE(GrtManager, GrtObject))
    {
      InitializeComponent();

      // Init BE

      InitializingControls = true;
      try
      {
        RefreshFormData();
      }
      finally
      {
        InitializingControls = false;
      }

      layerEditorBE.add_refresh_ui_handler(RefreshFormDataInvoke);
    }

    #endregion

    #region ObjectEditorPlugin Overrides

    public override bool ChangeGrtList(GrtManager GrtManager, GrtValue GrtList)
    {
      InitializingControls = true;
      SuspendLayout();

      try
      {
        grtManager = GrtManager;
        grtList = GrtList;

        if (editorBE != null)
          editorBE.disable_auto_refresh();
        editorBE = new MySQL.Grt.LayerEditorBE(GrtManager, GrtList);

        CallRefreshFormData();
      }
      finally
      {
        ResumeLayout();
        InitializingControls = false;
      }

      layerEditorBE.add_refresh_ui_handler(RefreshFormDataInvoke);

      Invalidate();

      return true;
    }

    #endregion

    #region IWorkbenchDocument implementation

    public override UIForm BackendClass
    {
      get { return editorBE; }
    }

    #endregion

    #region Form implemenation

    protected override void RefreshFormData()
    {
      nameTextBox.Text = layerEditorBE.get_name();
      colorEdit.Text = layerEditorBE.get_color();

      TabText = nameTextBox.Text;
    }

    #endregion

    private void nameTextBox_TextChanged(object sender, EventArgs e)
    {
      if (!InitializingControls && nameTextBox.Text != layerEditorBE.get_name())
      {
        layerEditorBE.set_name(nameTextBox.Text);
        TabText = nameTextBox.Text;
      }
    }

    private void colorDialogButton_Click(object sender, EventArgs e)
    {
      layerColorDialog.Color = ColorTranslator.FromHtml(layerEditorBE.get_color());
      if (layerColorDialog.ShowDialog() == DialogResult.OK)
      {
        Color newColor = layerColorDialog.Color;
        colorEdit.Text = ColorTranslator.ToHtml(newColor).ToLower();
        colorDialogButton.BackColor = layerColorDialog.Color;
      }
    }

    private void colorEdit_TextChanged(object sender, EventArgs e)
    {
      layerEditorBE.set_color(colorEdit.Text);
      try
      {
        colorDialogButton.BackColor = ColorTranslator.FromHtml(colorEdit.Text);
      }
      catch (Exception)
      {
        // Ignore exceptions. The color translator does not allow testing the given value first and
        // we want to give the user full control over the color.
      }
    }
  }
}