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

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

        public void Main(MercatorUi.ICustomizers.ExecAction Action)
        {
            if (Action == MercatorUi.ICustomizers.ExecAction.DossierOpen)
                MercatorUi.Forms.Accounting.AccountingBankCodaForm.AfterCodaRecognition += new MercatorUi.Forms.Accounting.AccountingBankCodaForm.AfterCodaRecognitionEventHandler(AccountingBankCodaForm_AfterCodaRecognition);
            else if (Action == MercatorUi.ICustomizers.ExecAction.DossierClose)
                MercatorUi.Forms.Accounting.AccountingBankCodaForm.AfterCodaRecognition -= new MercatorUi.Forms.Accounting.AccountingBankCodaForm.AfterCodaRecognitionEventHandler(AccountingBankCodaForm_AfterCodaRecognition);
        }

        void AccountingBankCodaForm_AfterCodaRecognition(MercatorUi.Forms.Accounting.AccountingBankCodaForm.AfterCodaRecognitionEventArgs e)
        {
            //MercatorUi._Divers.ViewData(e.DtMouvements.Copy(), true);
            foreach (DataRow dr in e.DtMouvements.Select("nom='EUROPABANK N.V.'"))
            {
                string sbrut = Api.StrExtract(dr["communic2"].ToString(), "BRUT: ", " -").Replace(".", "").Replace(",", ".");
                string scomm = Api.StrExtract(dr["communic2"].ToString(), "COMM ", " REF").Replace(".", "").Replace("," ,".");
                double brut;
                double comm;
                if ((sbrut != "") && (scomm != "") && double.TryParse(sbrut, out brut) && double.TryParse(scomm, out comm))
                {
                    DataRow dr_new = e.DtMouvements.NewRow();
                    Api.DataRowMerge(dr_new, dr); // kopieer inhoud van dr naar dr_new
                    e.DtMouvements.Rows.InsertAt(dr_new, Api.DataRowIndexInDataTable(dr));
                    dr["montant"] = -comm;
                    MercatorUi.Forms.Accounting.AccountingBankCodaForm.CodaInsertAccount(MercatorUi.Sig._SigEnum.GEN, "650001", dr); // 650001 = compte de commissions carte de crédit - à adapter selon plan comptable
                    dr_new["montant"] = brut;
                }
            }
        }
    }
}