using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using DevComponents.DotNetBar;

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {

        public void Main(MercatorUi.ICustomizers.ExecAction Action)
        {
            if (Action == MercatorUi.ICustomizers.ExecAction.DossierOpen)
            {
                Globals.Main.BaseFormCreating += new MercatorUi.Main.BaseFormCreatingEventHandler(Main_BaseFormCreating);
            }
            else if (Action == MercatorUi.ICustomizers.ExecAction.DossierClose)
            {
                Globals.Main.BaseFormCreating -= new MercatorUi.Main.BaseFormCreatingEventHandler(Main_BaseFormCreating);
            }
        }

        void Main_BaseFormCreating(object sender, MercatorUi.Main.BaseFormCreatingEventArgs e)
        {
            if (e.Form is MercatorUi.Forms.Action.CrmCalendarForm)
            {
                MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)e.Form;
                crmCalendarForm.ShowingContextMenu += crmCalendarForm_ShowingContextMenu;
                crmCalendarForm.Disposed += crmCalendarForm_Disposed;
            }
        }

        void crmCalendarForm_Disposed(object sender, EventArgs e)
        {
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)sender;
            crmCalendarForm.ShowingContextMenu -= crmCalendarForm_ShowingContextMenu;
            crmCalendarForm.Disposed -= crmCalendarForm_Disposed;
        }

        void crmCalendarForm_ShowingContextMenu(object sender, MercatorUi.Forms.Action.CrmCalendarForm.ShowingContextMenuEventArgs e)
        {
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)sender;

            ButtonItem addCustomActionItem = new ButtonItem();
            addCustomActionItem.Text = "Ajouter Action Custom";
            addCustomActionItem.Name = "addCustomActionItem";
            addCustomActionItem.Tag = crmCalendarForm;
            addCustomActionItem.Click += AddCustomActionItem_Click;
            addCustomActionItem.Disposed += addCustomActionItem_Disposed;
            e.ContextMenuItems.Add(addCustomActionItem);
        }

        private void addCustomActionItem_Disposed(object sender, EventArgs e)
        {
            ButtonItem addCustomActionItem = (ButtonItem)sender;
            addCustomActionItem.Disposed -= addCustomActionItem_Disposed;
            addCustomActionItem.Click -= AddCustomActionItem_Click;
        }

        private const string idActTempl = ".A-85FA695"; // ID dans ACTTEMPL. Attention : TYPE_PIM doit valoir 2 pour ce layout d'action (compatible calendrier)
        private const string IdCli = "INEO1     "; // ce client doit exister

        private void AddCustomActionItem_Click(object sender, EventArgs e)
        {
            ButtonItem addCustomActionItem = (ButtonItem)sender;
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)addCustomActionItem.Tag;
            DateTime startDate = crmCalendarForm.CalendarView.DateSelectionStart.GetValueOrDefault();
            DateTime endDate = crmCalendarForm.CalendarView.DateSelectionEnd.GetValueOrDefault();
            MercatorUi.Engine.Crm.ActionEngine actionEngine = MercatorUi.Engine.Crm.ActionEngine.InitNew(MercatorUi.Sig._SigEnum.CLI, IdCli, idActTempl);
            if (actionEngine.DataSet == null)
                return;
            actionEngine.ActionsRecord.MOMENT_1 = startDate;
            actionEngine.ActionsRecord.MOMENT_2 = endDate;
            actionEngine.CalendarOwnerKey = crmCalendarForm.CalendarView.SelectedOwner;
            MercatorUi.Forms.Action.ActionForm actionFormNew = new MercatorUi.Forms.Action.ActionForm(idActTempl.Substring(1), MercatorUi.Forms.Action.ActionClasses.ActionFormMode.Normal, actionEngine, null);
            actionFormNew.Show(Globals.iw);
        }
    }
}