using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MySQL.Grt;
using MySQL.Grt.Db;
using MySQL.Utilities;
using MySQL.GUI.Workbench.Plugins;
using MySQL.GUI.Workbench.Plugins.DbMySQLScriptSync;
//using Aga.Controls.Tree;
using Aga.Controls.Tree.NodeControls;

namespace MySQL.GUI.Workbench.Plugins.Sync
{
  public class BackendInitFailedException : ApplicationException
  {
  }

  public partial class DiffTreeControl : UserControl
  {
    SimpleGrtTreeModel diffTreeModel;
    TreeModel diffTreeBE;
    MySQL.GUI.Workbench.Plugins.DbMySQLScriptSync.DbMySQLScriptSync pluginBE;

    private void InitializeDiffTree(
      List<String> schemataNames, GrtValue catalog1, GrtValue catalog2)
    {
      diffTreeBE = pluginBE.init_diff_tree(schemataNames, catalog1, catalog2);
      diffTreeModel = new SimpleGrtTreeModel(diffTreeView, diffTreeBE, nodeIcon);

      nodeIcon.ValueNeeded += new EventHandler<NodeControlValueEventArgs>(nodeIconNeeded);
      nodeIcon.VirtualMode = true;
      nodeIcon2.ValueNeeded += new EventHandler<NodeControlValueEventArgs>(nodeIcon2Needed);
      nodeIcon2.VirtualMode = true;

      applyDirectionIcon.ValueNeeded += new EventHandler<NodeControlValueEventArgs>(directionIconNeeded);
      applyDirectionIcon.VirtualMode = true;

      dbChangedIcon.ValueNeeded += new EventHandler<NodeControlValueEventArgs>(dbChangedIconNeeded);
      dbChangedIcon.VirtualMode = true;
      modelChangedIcon.ValueNeeded += new EventHandler<NodeControlValueEventArgs>(modelChangedIconNeeded);
      modelChangedIcon.VirtualMode = true;

      diffTreeModel.AddColumn(modelObjectName, 12, false);
      diffTreeModel.AddColumn(dbObjectName, 16, false);

      //diffTreeModel.EnableRowColor(10);
      diffTreeView.Model = diffTreeModel;
    }

    private void getIconFromBE(object sender, NodeControlValueEventArgs e, int column)
    {
      if (e.Node != null && e.Node.Tag != null)
      {
        // Use the GRT Icon manager to get the correct icon
        GrtTreeNode node = e.Node.Tag as GrtTreeNode;

        if (node != null)
        {
          int iconId = diffTreeBE.get_field_icon(node.NodeId, column, IconSize.Icon16);
          Image icon = GrtIconManager.get_instance().get_icon(iconId);

          if (icon != null)
            e.Value = icon;
        }
      }
    }

    private void nodeIconNeeded(object sender, NodeControlValueEventArgs e)
    {
      getIconFromBE(sender, e, 10);
    }

    private void modelChangedIconNeeded(object sender, NodeControlValueEventArgs e)
    {
      getIconFromBE(sender, e, 11);
    }

    private void dbChangedIconNeeded(object sender, NodeControlValueEventArgs e)
    {
      getIconFromBE(sender, e, 15);
    }

    private void nodeIcon2Needed(object sender, NodeControlValueEventArgs e)
    {
      getIconFromBE(sender, e, 14);
    }

    private void directionIconNeeded(object sender, NodeControlValueEventArgs e)
    {
      getIconFromBE(sender, e, 13);
    }
    
    public DiffTreeControl(MySQL.GUI.Workbench.Plugins.DbMySQLScriptSync.DbMySQLScriptSync be,
      List<String> schemataNames, GrtValue catalog1, GrtValue catalog2)
    {
      pluginBE = be;
      InitializeComponent();
      InitializeDiffTree(schemataNames, catalog1, catalog2);
    }

    private void diffTreeNodeDblClick(object sender, Aga.Controls.Tree.TreeNodeAdvMouseEventArgs e)
    {
      Aga.Controls.Tree.NodeControls.NodeControl c = e.Control;
      if (c == applyDirectionIcon)
      {
        Aga.Controls.Tree.TreeNodeAdv node = diffTreeView.CurrentNode;
        pluginBE.set_next_apply_direction((node.Tag as GrtTreeNode).NodeId);
      }
    }

    public String generate_diff_tree_script()
    {
      return pluginBE.generate_diff_tree_script();
    }

    public String generate_diff_tree_report()
    {
      return pluginBE.generate_diff_tree_report();
    }

    public void update_model_old_names()
    {
      pluginBE.update_model_old_names();
    }
  }
}