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.Sync;

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

  public partial class SQLScriptSyncWizardPages : WizardPluginPages
  {
    private DbMySQLScriptSync pluginBE;
    private DiffTreeControl diffTreeControl;

    public SQLScriptSyncWizardPages(WizardPlugin wizard)
      : base(wizard)
    {
      pluginBE = new DbMySQLScriptSync(wizard.GrtManager);

      InitializeComponent();

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


      AddPage(treePanel,
        InitTreePage,
        CleanupTreePage,
        ProcessTreePage,
        null,
        null,
        "SQL Diff Tree",
        "Review the Diff Tree and Choose Application Direction");

      AddPage(generatedScriptPanel,
        InitGeneratedScriptPage,
        CleanupGeneratedScriptPage,
        ProcessGeneratedScriptPage,
        null,
        null,
        "SQL Script",
        "Review and Edit the SQL Script");
    }

    private string InitializeFilterPage()
    {
      return "";
    }

    #region Options Page Routines

    private string InitOptionsPage()
    {
      wizard.NextPageButton.Enabled = (inputPathTextBox.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("InputFileName1", inputPathTextBox.Text);
      //pluginBE.set_option("OutputFileName", outputPathTextBox.Text);
      //pluginBE.start_sync();
      return "";
    }

    #endregion

    #region Filter Page Routines

    private string InitFilterPage()
    {
      return "";
    }

    private string CleanupFilterPage()
    {
      return "";
    }

    private string ProcessFilterPage()
    {
      //pluginBE.set_option("OutputFileName", outputPathTextBox.Text);
      ////pluginBE.set_option("TableFilterList", table_filter);
      //pluginBE.start_export();
      return "";
    }

    #endregion

    #region Diff Tree Page routines

    private string InitTreePage()
    {
      if (diffTreeControl != null)
        diffTreeControl.Parent = null;  // release old control

      diffTreeControl = new DiffTreeControl(pluginBE, 
        null, new MySQL.Grt.GrtValue(), new MySQL.Grt.GrtValue());
      diffTreeControl.Parent = treePanel;
      diffTreeControl.Dock = DockStyle.Fill;
      diffTreeControl.Padding = new System.Windows.Forms.Padding(5);
      diffTreeControl.Margin = new System.Windows.Forms.Padding(5);
      return "";
    }

    private string CleanupTreePage()
    {
      return "";
    }

    private string ProcessTreePage()
    {
      return "";
    }

    #endregion

    #region Generated Script Page routines

    private string InitGeneratedScriptPage()
    {
      scriptTextBox.Text = diffTreeControl.generate_diff_tree_script();
      pluginBE.apply_changes_to_model();
      return "";
    }

    private string CleanupGeneratedScriptPage()
    {
      return "";
    }

    private string ProcessGeneratedScriptPage()
    {
      return "";
    }

    #endregion


    private void inputBrowseButton_Click(object sender, EventArgs e)
    {
      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.DefaultExt = "sql";
      openFileDialog.Filter = "SQL Script Files (*.sql)|*.sql|All Files (*.*)|*.*";
      openFileDialog.Title = "Input Script File";
      openFileDialog.RestoreDirectory = true;

      if (openFileDialog.ShowDialog() == DialogResult.OK)
      {
        inputPathTextBox.Text = openFileDialog.FileName;
      }
    }

    private void inputPathTextBox_TextChanged(object sender, EventArgs e)
    {
      wizard.NextPageButton.Enabled = inputPathTextBox.Text != ""
        && System.IO.File.Exists(inputPathTextBox.Text);
    }

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

      if (saveFileDialog.ShowDialog() == DialogResult.OK)
      {
        String err = pluginBE.save_script_to_file(scriptTextBox.Text, saveFileDialog.FileName);
        if (err.Length > 0)
          System.Windows.Forms.MessageBox.Show(err, "Error saving file", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

  }
}