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


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.GetType() == typeof(MercatorUi.Forms.Accounting.AccountingBankDomicilForm))
            {
                e.Form.Shown += new EventHandler(Form_Shown);
                e.Form.FormClosed += new FormClosedEventHandler(Form_FormClosed);
            }
        }

        void Form_Shown(object sender, EventArgs e)
        {
            MercatorUi.Forms.Accounting.AccountingBankDomicilForm accountingBankDomicilForm = (MercatorUi.Forms.Accounting.AccountingBankDomicilForm)sender;
            accountingBankDomicilForm.BeforeCreateFile += new MercatorUi.Forms.Accounting.AccountingBankDomicilForm.BeforeCreateFileEventHandler(accountingBankDomicilForm_BeforeCreateFile);
            accountingBankDomicilForm.Shown -= new EventHandler(Form_Shown); // désinscrire l'évènement
        }

        void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            MercatorUi.Forms.Accounting.AccountingBankDomicilForm accountingBankDomicilForm = (MercatorUi.Forms.Accounting.AccountingBankDomicilForm)sender;
            accountingBankDomicilForm.FormClosed -= new FormClosedEventHandler(Form_FormClosed);
            accountingBankDomicilForm.BeforeCreateFile -= new MercatorUi.Forms.Accounting.AccountingBankDomicilForm.BeforeCreateFileEventHandler(accountingBankDomicilForm_BeforeCreateFile);
        }

        void accountingBankDomicilForm_BeforeCreateFile(object sender, MercatorUi.Forms.Accounting.AccountingBankDomicilForm.BeforeCreateFileEventArgs e)
        {
            MercatorUi.Forms.Accounting.AccountingBankDomicilForm accountingBankDomicilForm = (MercatorUi.Forms.Accounting.AccountingBankDomicilForm)sender;
            string reference = Api.JustStem(e.Fichier);
            e.Xml.Replace("<PmtInfId>MERCATOR-", "<PmtInfId>M" + Api.Right(reference, 7) + "-");
        }
    }
}