using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MySQL.Grt;
using MySQL.GUI.Workbench.Plugins;
using MySQL.GUI.Workbench.Plugins.Db;

namespace MySQL.GUI.Workbench.Plugins.DbMySQL
{
  using Pages = LinkedList<PageInfo>;
  using System.Collections;
  using ValidationControl = MySQL.GUI.Workbench.Plugins.Sync.ValidationControl;

  public partial class DiffSQLGeneratorWizardPages : WizardPluginPages
  {
    private DbMySQLSQLExport pluginBE;
    private SchemaObjectFilter user_filter;
    private SchemaObjectFilter table_filter;
    private SchemaObjectFilter view_filter;
    private SchemaObjectFilter routine_filter;
    private SchemaObjectFilter trigger_filter;

    public DiffSQLGeneratorWizardPages(WizardPlugin wizard)
      : base(wizard)
    {
      pluginBE = new DbMySQLSQLExport(wizard.GrtManager);

      InitializeComponent();

      AddPage(optionsPanel,
        InitOptionsPage,
        CleanupOptionsPage,
        ProcessOptionsPage,
        null,
        null,
        "SQL Export Options",
        "Select SQL Export Options");

      AddPage(filterPanel,
        null,
        CleanupFilterPage,
        ProcessFilterPage,
        null,
        null,
        "SQL Export Filter",
        "Select Objects to Export");


      InitializeFilters();
    }

    private SchemaObjectFilter CreateDbObjectsFilter(String objectTypeName, GrtStringListModel model, GrtStringListModel exc_model)
    {
      SchemaObjectFilter filter = new SchemaObjectFilter(pluginBE.get_grt_manager());
      filter.Parent = filterPanel;
      filter.Dock = DockStyle.Top;
      filter.SetObjectType(objectTypeName);
      filter.SetupModels(model, exc_model);
      filter.Padding = new System.Windows.Forms.Padding(5);
      filter.Margin = new System.Windows.Forms.Padding(5);
      return filter;
    }

    private string InitializeFilters()
    {
      GrtStringListModel users_model;
      GrtStringListModel users_imodel;
      GrtStringListModel tables_model;
      GrtStringListModel tables_imodel;
      GrtStringListModel views_model;
      GrtStringListModel views_imodel;
      GrtStringListModel routines_model;
      GrtStringListModel routines_imodel;
      GrtStringListModel triggers_model;
      GrtStringListModel triggers_imodel;

      pluginBE.setup_grt_string_list_models_from_catalog(
        out users_model, out users_imodel, 
        out tables_model, out tables_imodel, 
        out views_model, out views_imodel, 
        out routines_model, out routines_imodel, 
        out triggers_model, out triggers_imodel);

      filterPanel.SuspendLayout();

      trigger_filter = CreateDbObjectsFilter("db.mysql.Trigger", triggers_model, triggers_imodel);
      routine_filter = CreateDbObjectsFilter("db.mysql.Routine", routines_model, routines_imodel);
      view_filter = CreateDbObjectsFilter("db.mysql.View", views_model, views_imodel);
      table_filter = CreateDbObjectsFilter("db.mysql.Table", tables_model, tables_imodel);
      user_filter = CreateDbObjectsFilter("db.User", users_model, users_imodel);

      user_filter.RefreshModels();
      table_filter.RefreshModels();
      view_filter.RefreshModels();
      routine_filter.RefreshModels();
      trigger_filter.RefreshModels();

      filterPanel.ResumeLayout();

      return "";
    }

    private String RunExport()
    {
      pluginBE.set_option("OutputFileName", outputPathTextBox.Text);
      // restrict objects importing by selected types
      pluginBE.set_option("TablesAreSelected", table_filter.TypeActivated);
      pluginBE.set_option("TriggersAreSelected", trigger_filter.TypeActivated);
      pluginBE.set_option("RoutinesAreSelected", routine_filter.TypeActivated);
      pluginBE.set_option("ViewsAreSelected", view_filter.TypeActivated);
      pluginBE.set_option("UsersAreSelected", user_filter.TypeActivated);
      pluginBE.start_export();
      return "";
    }

    #region Options Page Routines

    private string InitOptionsPage()
    {
      wizard.NextPageButton.Enabled = (outputPathTextBox.Text != "");
      List<Control> controlsToSkipOnce = new List<Control>();
      controlsToSkipOnce.Add(wizard.NextPageButton);
      wizard.ControlsToSkipOnce(controlsToSkipOnce);

      return "";
    }

    private string CleanupOptionsPage()
    {
      return "";
    }

    private string ProcessOptionsPage()
    {
      pluginBE.set_option("GenerateDrops", generateDropsCheckBox.Checked);
      pluginBE.set_option("GenerateWarnings", showWarningsCheckBox.Checked);
      pluginBE.set_option("GenerateCreateIndex", createIndexCheckBox.Checked);
      pluginBE.set_option("NoUsersJustPrivileges", dontCreateUserJustPrivilegesCheckBox.Checked);
      pluginBE.set_option("GenerateInserts", generateInsertsCheckBox.Checked);
      pluginBE.set_option("OmitSchemata", omitSchemataCheckBox.Checked);

      return "";
    }

    #endregion

    #region Filter Page Routines

    private string InitFilterPage()
    {
      return "";
    }

    private string CleanupFilterPage()
    {
      return "";
    }

    private string ProcessFilterPage()
    {
      return RunExport();
    }

    #endregion


    private void browseButton_Click(object sender, EventArgs e)
    {
      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.DefaultExt = "sql";
      saveFileDialog.Filter = "SQL Script Files (*.sql)|*.sql|All Files (*.*)|*.*";
      saveFileDialog.Title = "Export SQL Script File";
      saveFileDialog.RestoreDirectory = true;

      DialogResult res = saveFileDialog.ShowDialog();
      if (res == DialogResult.OK)
      {
        outputPathTextBox.Text = saveFileDialog.FileName;
      }
    }

    private void outputPathTextBox_TextChanged(object sender, EventArgs e)
    {
      wizard.NextPageButton.Enabled = outputPathTextBox.Text != "";
    }

  }
}