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

// <CompileWithRoslyn />

namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {
        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            Forms.Billing.BillingForm billingForm = (Forms.Billing.BillingForm)clickedButton.Form;

            MercatorPayTerm.PayTermMollie payTermMollie = Globals.PayTerms.OfType<MercatorPayTerm.PayTermMollie>().FirstOrDefault();
            if (payTermMollie == null)
            {
                Dialogs.Stop("Option MOLLIE non présente !");
                return;
            }
            string transactionId = xFunctions.xMollie(billingForm.BillingEngine.PIEDS, "id", 1);
            if (transactionId == "")
            {
                Dialogs.Stop("Le premier mode de paiement n'est pas Mollie !");
                return;
            }
            double d = Dialogs.AskDouble("Montant à rembourser ?", 0, "### ##0.00");
            if (d == double.MinValue)
                return;

            var r = payTermMollie.Refund(transactionId, d, billingForm.BillingEngine.Journal + "_" + billingForm.BillingEngine.Piece, out string error);
            if (error != null)
                Dialogs.Stop(error);
            else
                Dialogs.Stop("Statut = " + r.status.ToString());
        }
    }
}