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

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated,
        MercatorUi.ICustomizers.IBillingEngineClosed
    {
        #region engine

        bool _passPaiem = false;

        #region customEngine
        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.AfterApplyCustomerSupplier += new MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventHandler(BillingEngine_AfterApplyCustomerSupplier);
            BillingEngine.BeforePayment += new EventHandler(billingEngine_BeforePayment);
            BillingEngine.BeforeSave += new MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventHandler(BillingEngine_BeforeSave);
        }
        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.AfterApplyCustomerSupplier -= new MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventHandler(BillingEngine_AfterApplyCustomerSupplier);
            BillingEngine.BeforePayment -= new EventHandler(billingEngine_BeforePayment);
            BillingEngine.BeforeSave -= new MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventHandler(BillingEngine_BeforeSave);
        }
        #endregion
        #region methodEngine
        private void BillingEngine_AfterApplyCustomerSupplier(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            //Ceci est là pour forcer la mise à jour des prix et le repassage par les modules de beforepaiem
            if ((billingEngine.BillingForm != null) && (billingEngine.BillingForm.Payments != null) && !billingEngine.BillingForm.Payments.ReadOnly)
            {
                billingEngine.PIEDS["tot_ttc_dv"] = 0;
                _passPaiem = false;
                billingEngine.UpdateAmounts();
            }
        }
        
        private void billingEngine_BeforePayment(object sender, EventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;

            if (billingEngine.Context != MercatorUi.Engine.Gescom.BillingEngine.ContextEnum.UserInterface)
                return;

            bool ok = true;

            if (billingEngine.CLI == null)
            {
                new MercatorUi._BaseClasses.TimerOneShot(timer_Tick_validation, billingEngine);
                return;
            }

            if (!test(billingEngine))
                ok = false;

            if (ok)
            {
                _passPaiem = true;
                billingEngine.UpdateAmounts();
            }
            else
            {
                new MercatorUi._BaseClasses.TimerOneShot(timer_Tick_validation, billingEngine);
            }
        }
        private void BillingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;

            if (billingEngine.Context != MercatorUi.Engine.Gescom.BillingEngine.ContextEnum.UserInterface)
                return;

            if (!_passPaiem)
            {
                if (!test(billingEngine))
                    e.CancelSave = true;
                billingEngine.UpdateAmounts();
            }
        }

        #endregion
        #region subMethodEngine

        private void test(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {

            ...
        }

        private void timer_Tick_validation(object sender, EventArgs e)
        {
            MercatorUi._BaseClasses.TimerOneShot timer = (MercatorUi._BaseClasses.TimerOneShot)sender;
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)timer.Tag;
            if ((billingEngine.BillingForm != null) && !billingEngine.BillingForm.Payments.ReadOnly)
            {
                billingEngine.PIEDS["tot_ttc_dv"] = 0;
                _passPaiem = false;
                billingEngine.UpdateAmounts();
            }
            _Divers.SuperFocus(billingEngine.BillingForm.LinesEditor);
        }
        #endregion
        #endregion
    }
}