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

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

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.BeforePayment -= BillingEngine_BeforePayment;
        }

        void BillingEngine_BeforePayment(object sender, EventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            double seuil = Dialogs.AskDouble("Quel est le seuil ?", 200, billingEngine.DisplayFormatPu); // seuil vaut double.MinValue si on clique sur "Annuler"
            if (billingEngine.PiedsVRecord.TOT_BAS_DV.CompareTo(seuil, billingEngine.NDec) < 0)
            {
                double valeur = Dialogs.AskDouble("Quel est le montant des frais ?", 0, billingEngine.DisplayFormatPu);
                if (valeur != double.MinValue) // on n'a pas cliqué sur "Annuler"
                {
                    var lignesVTransp = billingEngine.LignesVRecords.Where(l => l.ID_ARTICLE == "TRANSP"); // TRANSP est le S_ID de l'article "frais de port"
                    if (lignesVTransp.Count() == 0) // pas de ligne existante avec cet article
                    {
                        DataSet ds = Api.Zselect(Globals.RepData, "select * from stock (NOLOCK) where s_id='TRANSP'");
                        if (ds == null) // si erreur SQL à ce stade, on bloque le processus de sauvegarde
                            return;
                        Api.TrimEndDataTable(ds.Tables[0]);
                        int n = billingEngine.AppendLine(true);
                        if (!billingEngine.InsertItem(ds.Tables[0].Rows[0], billingEngine.LIGNES.Rows[n]))
                            return;
                        billingEngine.LignesVRecords[n].PU = valeur;
                    }
                    else // une ligne avec l'article frais de port existe déjà : on va l'utiliser
                    {
                        MercatorDatabase.LIGNES_V l = lignesVTransp.First();
                        l.DataRow[billingEngine.VarQ] = 1;
                        l.PU = valeur;
                    }
                    billingEngine.UpdateAmounts(); // recalculer le document
                }
            }
            else if (seuil > double.MinValue) // on est sous le seuil des frais de port -> on retire les frais de port si la ligne était déjà présente
            {
                billingEngine.LignesVRecords.Remove(l => l.ID_ARTICLE == "TRANSP");
                billingEngine.UpdateAmounts(); // recalculer le document
            }
        }
    }
}