using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using MercatorUi;
using MercatorApi;
using System.Data;


namespace SigCli
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer,
                              MercatorUi.ICustomizers.IStringUpdater
    {
        string MercatorUi.ICustomizers.IStringUpdater.StringUpdate(string StringToModify)
        {
            string id = Api.StrExtract(StringToModify, "/*<ID>", "</ID>*/");
            if (id == "SIG_ACTIONSTREE_PARENTS_CLI") // requête pour le niveau "parent"
            {
                StringToModify = StringToModify.Replace("actions.users,", "actions.users,actions.contact,");
            }
            else if (id == "SIG_ACTIONSTREE_CHILDS_CLI") // requête pour le niveau "enfant"
            {
                StringToModify = StringToModify.Replace("actions.users,", "actions.users,actions.contact,");
            }
            return StringToModify;
        }

        public void FormLoadCustomize(Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;

            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigObjects.ActionsTree));
            if (l.Count > 0)
            {
                MercatorUi.Forms.Sig.SigObjects.ActionsTree actionTree = (MercatorUi.Forms.Sig.SigObjects.ActionsTree)l[0];
                actionTree.ActionsTreeControl.AddingNode += new MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl.AddingNodeEventHandler(ActionsTreeControl_AddingNode);
            }
        }

        public void FormClosedCustomize(Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;

            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigObjects.ActionsTree));
            if (l.Count > 0)
            {
                MercatorUi.Forms.Sig.SigObjects.ActionsTree actionTree = (MercatorUi.Forms.Sig.SigObjects.ActionsTree)l[0];
                actionTree.ActionsTreeControl.AddingNode -= new MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl.AddingNodeEventHandler(ActionsTreeControl_AddingNode);
            }
        }

        void ActionsTreeControl_AddingNode(object sender, MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl.AddingNodeEventArgs e)
        {
            MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl actionsTreeControl = (MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl)sender;
            if (actionsTreeControl.Tree.Columns.Count == 7) // 7 = le nombre de colonnes en standard - ce code ne sera exécuté qu'une seule fois -> on ajoute la colonne dans la collection Columns du Tree
            {
                DevComponents.AdvTree.ColumnHeader colContact = new DevComponents.AdvTree.ColumnHeader(_Divers.Iif_langue(Globals.Langue, "Contact", "Contactpersoon", "Contact"));
                colContact.SortingEnabled = false;
                colContact.Width.Absolute = 200;
                actionsTreeControl.Tree.Columns.Add(colContact);
            }
            _Divers.AddCellsToNode(e.Node, 1); // comme on a ajouté une colonne, il faut ajouter une Cell à chaque noeud
            e.Node.Cells[7].Text = e.Node.Dr["contact"].ToString();
            if (e.NodeType == MercatorUi.Forms.Sig.SigClasses.ActionsTreeControl.NodeEnum.Parent)
                e.Node.Cells[7].Text = "Parent=" + e.Node.Dr["contact"].ToString();
            else
                e.Node.Cells[7].Text = "Enfant=" + e.Node.Dr["contact"].ToString();
        }
    }
}