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;
using MySQL.Grt.Db;

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

    private MySQLViewEditorBE viewEditorBE { get { return editorBE as MySQLViewEditorBE; } } 
    private DbObjectEditorPages dbObjectEditorPages;

    #endregion

    #region Constructors

    public DbMysqlViewEditor(GrtManager GrtManager, GrtValue GrtList)
      : base(new MySQLViewEditorBE(GrtManager, GrtList))
    {
      InitializeComponent();

      sqlEd = new SqlEditorHelper(GrtManager, panel1, null);

      InitializingControls = true;
      try
      {
        InitFormData();

        CallRefreshFormData();
      }
      finally
      {
        InitializingControls = false;
      }
      viewEditorBE.add_refresh_ui_handler(RefreshFormDataInvoke);
    }


    #endregion

    #region IWorkbenchDocument implementation

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

    #endregion

    #region ObjectEditorPlugin Overrides

    public override bool ChangeGrtList(GrtManager GrtManager, GrtValue GrtList)
    {
      // Make sure the changes are stored
      sqlEd.Parse();

      InitializingControls = true;

      try
      {
        grtManager = GrtManager;
        grtList = GrtList;

        if (editorBE != null)
          editorBE.disable_auto_refresh();
        editorBE = new MySQLViewEditorBE(GrtManager, GrtList);

        InitFormData();

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

      viewEditorBE.add_refresh_ui_handler(RefreshFormDataInvoke);

      Invalidate();

      return true;
    }

    #endregion

    #region Form implementation

    private void DbMysqlViewEditor_Load(object sender, EventArgs e)
    {
      ActiveControl = sqlEd.sqlEditor;
    }

    protected void InitFormData()
    {
      // Add privileges tab sheet
      if (dbObjectEditorPages != null)
        mainTabControl.TabPages.Remove(dbObjectEditorPages.PrivilegesTabPage);
      dbObjectEditorPages = new DbObjectEditorPages(grtManager, viewEditorBE);
      mainTabControl.TabPages.Add(dbObjectEditorPages.PrivilegesTabPage);
    }

    protected override void RefreshFormData()
    {
      nameTextBox.Text = viewEditorBE.get_name();
      TabText = nameTextBox.Text;
      commentTextBox.Text = viewEditorBE.get_comment();

      if (!sqlEd.sqlEditor.IsDirty || sqlEd.EditorBE != viewEditorBE)
      {
        sqlEd.SetEditorBackend(viewEditorBE, viewEditorBE.set_query);
        sqlEd.sqlEditor.SqlText = viewEditorBE.get_query();
      }
    }

    private void nameTextBox_TextChanged(object sender, EventArgs e)
    {
      if (!InitializingControls && !nameTextBox.Text.Equals(viewEditorBE.get_name()))
        viewEditorBE.set_name(nameTextBox.Text);

      TabText = nameTextBox.Text;
    }

    private void commentTextBox_TextChanged(object sender, EventArgs e)
    {
      if (!InitializingControls && !commentTextBox.Text.Equals(viewEditorBE.get_comment()))
        viewEditorBE.set_comment(commentTextBox.Text);
    }

    private void undoItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.UndoRedo.Undo();
    }

    private void cutItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.Clipboard.Cut();
    }

    private void copyItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.Clipboard.Copy();
    }

    private void pasteItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.Clipboard.Paste();
    }

    private void selectAllItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.Selection.SelectAll();
    }

    private void redoItem_Click(object sender, EventArgs e)
    {
      sqlEd.sqlEditor.UndoRedo.Redo();
    }

    private void editorContextMenu_Opening(object sender, CancelEventArgs e)
    {
      // Enable/Disable menu items depending on the edit content.
      undoItem.Enabled = sqlEd.sqlEditor.UndoRedo.CanUndo;
      redoItem.Enabled = sqlEd.sqlEditor.UndoRedo.CanRedo;
      cutItem.Enabled = sqlEd.sqlEditor.Selection.Length > 0;
      copyItem.Enabled = sqlEd.sqlEditor.Selection.Length > 0;
      pasteItem.Enabled = sqlEd.sqlEditor.Clipboard.CanPaste;
      selectAllItem.Enabled = sqlEd.sqlEditor.TextLength > 0;
    }

    private void syntaxCheckLabel_Click(object sender, EventArgs e)
    {
      if (viewEditorBE.has_syntax_error())
        grtManager.get_grt().make_output_visible();
    }

    #endregion

  }
}