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 WeifenLuo.WinFormsUI.Docking;
using MySQL.Grt;
using MySQL.Grt.Db;
using MySQL.Utilities;

namespace MySQL.GUI.Workbench.Plugins
{
	public partial class DbObjectEditorPages : DockContent
	{
    private GrtManager grtManager;
    private DBObjectEditorBE dbObjectEditorBE;

    private ObjectRoleListBE objectRoleListBE;
    private SimpleGrtListModel objectRoleListModel;

    private ObjectPrivilegeListBE objectPrivsListBE;

    private RoleTreeBE roleTreeBE;
    private SimpleGrtTreeModel roleTreeModel;

		protected DbObjectEditorPages()
		{
			InitializeComponent();
		}

		public DbObjectEditorPages(GrtManager GrtManager, DBObjectEditorBE DbObjectEditorBE)
			: this()
		{
			grtManager = GrtManager;
      dbObjectEditorBE = DbObjectEditorBE;

      // initialize roles based on passed object
      roleTreeBE = new RoleTreeBE(DbObjectEditorBE.get_catalog());

      roleTreeModel = new SimpleGrtTreeModel(allRolesTreeView, roleTreeBE);
      roleTreeModel.AddColumn(allRolesNameNodeControl, (int)RoleTreeBE.Columns.Name, false);
      allRolesTreeView.Model = roleTreeModel;

      objectRoleListBE = new ObjectRoleListBE(dbObjectEditorBE);
      objectRoleListModel = new SimpleGrtListModel(rolesTreeView, objectRoleListBE);
      objectRoleListModel.AddColumn(roleNameNodeControl, (int)ObjectRoleListBE.Columns.Name, false);
      rolesTreeView.Model = objectRoleListModel;

      objectPrivsListBE = objectRoleListBE.get_privilege_list();

      RefreshPrivilegesList();
    }

    public System.Windows.Forms.TabPage PrivilegesTabPage
    {
      get { return privilegesTabPage; }
    }


    private void RefreshPrivilegesList()
    {
      privCheckedListBox.Items.Clear();

      int count = objectPrivsListBE.count();
      String caption;
      int enabled;

      for (short i = 0; i < count; i++)
      {
        objectPrivsListBE.get_field(new NodeId(i), (int)ObjectPrivilegeListBE.Columns.Name, out caption);
        objectPrivsListBE.get_field(new NodeId(i), (int)ObjectPrivilegeListBE.Columns.Enabled, out enabled);
        privCheckedListBox.Items.Add(caption, enabled != 0);
      }
    }


    private void roleAssignButton_Click(object sender, EventArgs e)
    {
      foreach (Aga.Controls.Tree.TreeNodeAdv node in allRolesTreeView.SelectedNodes)
      {
        GrtTreeNode aNode = node.Tag as GrtTreeNode;

        objectRoleListBE.add_role_for_privileges(roleTreeBE.get_role_with_id(aNode.NodeId));
      }
      objectRoleListModel.RefreshModel();
    }

    private void roleRemoveButton_Click(object sender, EventArgs e)
    {
      foreach (Aga.Controls.Tree.TreeNodeAdv node in rolesTreeView.SelectedNodes)
      {
        GrtListNode aNode = node.Tag as GrtListNode;

        objectRoleListBE.remove_role_from_privileges(roleTreeBE.get_role_with_id(aNode.NodeId));
      }
      objectRoleListModel.RefreshModel();
    }

    private void privCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
    {
      objectPrivsListBE.set_field(new NodeId((short)e.Index), (int)ObjectPrivilegeListBE.Columns.Enabled,
        e.NewValue == CheckState.Checked ? 1 : 0);
    }

    private void allRolesTreeView_SelectionChanged(object sender, EventArgs e)
    {
      if (allRolesTreeView.SelectedNode == null)
        roleAssignButton.Enabled = false;
      else
        roleAssignButton.Enabled = true;
    }

    private void rolesTreeView_SelectionChanged(object sender, EventArgs e)
    {
      if (rolesTreeView.SelectedNode == null)
      {
        roleRemoveButton.Enabled = false;
        objectRoleListBE.set_selected(new NodeId());
      }
      else
      {
        GrtListNode aNode = rolesTreeView.SelectedNode.Tag as GrtListNode;

        roleRemoveButton.Enabled = true;
        objectRoleListBE.set_selected(aNode.NodeId);
      }
      RefreshPrivilegesList();
    }
  }
}
