using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using MercatorApi;
using MercatorUi;
using MercatorExtensions;
using MercatorDatabase;
using MercatorController;

// <CompileWithRoslyn />

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {
        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BeforeSave += billingEngine_BeforeSave;
        }

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BeforeSave -= billingEngine_BeforeSave;
        }

        void billingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            for (int i = 1; i <= 9; i++)
            {
                if (billingEngine.PIEDS.Value<int>("typ_paiem" + i) == 21) // 21 = n° du mode de paiement "Remboursement"
                {
                    if (billingEngine.PIEDS.Value<double>("tot_paiem" + i).CompareTo(0d, 2) > 0)
                    {
                        Dialogs.Stop("Le montant sur le mode \"Remboursement\" doit être négatif !");
                        e.CancelSave = true;
                        return;
                    }
                    if (billingEngine.PIEDS.Value<double>("tot_paiem" + i).CompareTo(0d, 2) < 0)
                    {
                        MercatorPayTerm.PayTermMollie payTermMollie = MercatorUi.Globals.PayTerms.OfType<MercatorPayTerm.PayTermMollie>().FirstOrDefault();
                        if (payTermMollie == null)
                        {
                            Dialogs.Stop("Option MOLLIE non présente !");
                            e.CancelSave = true;
                            return;
                        }
                        string transactionId = xFunctions.xMollie(billingEngine.PIEDS, "id", 1);
                        if (transactionId == "")
                        {
                            Dialogs.Stop("Le premier mode de paiement n'est pas Mollie !");
                            e.CancelSave = true;
                            return;
                        }
                        var r = payTermMollie.Refund(transactionId, -billingEngine.PIEDS.Value<double>("tot_paiem" + i), billingEngine.Journal + "_" + billingEngine.Piece, out string error);
                        string msg = error ?? ("Statut = " + r.status.ToString());
                        if (!Dialogs.AnswerYesNo("Le résultat du remboursement Mollie est :\r\n" + msg + "\r\nContinuer ?"))
                        {
                            e.CancelSave = true;
                            return;
                        }
                    }
                }
            }
        }
    }
}