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

// <CompileWithRoslyn />

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

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

        private void billingEngine_GettingBareme(object sender, MercatorUi.Engine.Gescom.BillingEngine.GettingBaremeEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            double totQ = billingEngine.LignesVRecords.Where(l => l.ID_ARTICLE == e.LigneVRecord.ID_ARTICLE).Sum(l => l.Q);
            double totQRayon = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_RAYON == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_ID_RAYON).Sum(l => l.Q);
            double totQFamille = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_FAMIL == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_ID_FAMIL).Sum(l => l.Q);
            double totQSsFam = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_SSFAM == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_ID_SSFAM).Sum(l => l.Q);
            double totQCat1 = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT1 == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_CAT1).Sum(l => l.Q);
            double totQCat2 = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT2 == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_CAT2).Sum(l => l.Q);
            double totQCat3 = billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT3 == billingEngine.StockRecords[e.LigneVRecord.ID_ARTICLE].S_CAT3).Sum(l => l.Q);
            double totQOther = billingEngine.LignesVRecords.Where(l => !string.IsNullOrEmpty(l.ID_ARTICLE)).Sum(l => l.Q);

            if ((totQ.CompareTo(e.Qart, Globals.N_DEC_Q) != 0) || (totQRayon.CompareTo(e.Qrayon, Globals.N_DEC_Q) != 0) || (totQFamille.CompareTo(e.Qfamille, Globals.N_DEC_Q) != 0) || (totQSsFam.CompareTo(e.Qssfam, Globals.N_DEC_Q) != 0) || (totQCat1.CompareTo(e.Qcat1, Globals.N_DEC_Q) != 0)
                || (totQCat2.CompareTo(e.Qcat2, Globals.N_DEC_Q) != 0) || (totQCat3.CompareTo(e.Qcat3, Globals.N_DEC_Q) != 0) || (totQOther.CompareTo(e.Qother, Globals.N_DEC_Q) != 0))
            {
                e.Qart = totQ;
                e.Qcat1 = totQCat1;
                e.Qcat2 = totQCat2;
                e.Qcat3 = totQCat3;
                e.Qrayon = totQRayon;
                e.Qfamille = totQFamille;
                e.Qssfam = totQSsFam;
                e.Qother = totQOther;

                MercatorUi.Engine.Gescom.BillingEngine.AfterApplyPricingInfoEventHandler afterApplyPricingInfoEventHandler = null;
                afterApplyPricingInfoEventHandler = (s, e2) =>
                {
                    billingEngine.AfterApplyPricingInfo -= afterApplyPricingInfoEventHandler;
                    if ((e.DataRowLignes == e2.DataRowLignes) && (e2.PricingInfosV.Bareme != null)) // est-on toujours sur la même ligne ?
                    {
                        if (!string.IsNullOrWhiteSpace(e2.PricingInfosV.Bareme["ar_ref"].ToString()))
                        {
                            // update de andere lijnen met hetzelfde artikel
                            foreach (var l in billingEngine.LignesVRecords.Where(l => (l.ID_ARTICLE == e.LigneVRecord.ID_ARTICLE)))
                            {
                                l.PU = e.LigneVRecord.PU;
                                l.REMISE = e.LigneVRecord.REMISE;
                                l.PROMOSOLDE = true;
                            }
                        }
                        else if (!string.IsNullOrWhiteSpace(e2.PricingInfosV.Bareme["id_fam"].ToString()))
                        {
                            MercatorDatabase.STOCK stockRecord = (e2.StockRecord ?? billingEngine.StockRecords[e2.LignesVRecord.ID_ARTICLE]);
                            switch (e2.PricingInfosV.Bareme["tag"].ToString())
                            {
                                case "4":
                                case "12":
                                case "20":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_RAYON == stockRecord.S_ID_RAYON).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                                case "3":
                                case "11":
                                case "19":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_FAMIL == stockRecord.S_ID_FAMIL).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                                case "2":
                                case "10":
                                case "18":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_ID_SSFAM == stockRecord.S_ID_SSFAM).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                                case "5":
                                case "13":
                                case "21":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT1 == stockRecord.S_CAT1).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                                case "6":
                                case "14":
                                case "22":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT2 == stockRecord.S_CAT2).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                                case "7":
                                case "15":
                                case "23":
                                    billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE) && billingEngine.StockRecords[l.ID_ARTICLE].S_CAT3 == stockRecord.S_CAT3).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                                    break;
                            }
                        }
                        else
                        {
                            billingEngine.LignesVRecords.Where(l => !string.IsNullOrWhiteSpace(l.ID_ARTICLE)).ToList()
                                    .ForEach(l =>
                                    {
                                        l.REMISE = e.LigneVRecord.REMISE;
                                        l.PROMOSOLDE = true;
                                    }
                                            );
                        }

                    }
                };
                billingEngine.AfterApplyPricingInfo += afterApplyPricingInfoEventHandler;
            }
        }
    }
}