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

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

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

        void billingEngine_BeginningSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeginningSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (MercatorUi.Globals.IsMercatorPenguinServer && billingEngine.LotsRequired)
            {
                //MercatorUi.Globals.ApiLogDelegate("..."); // écrire dans le log de MercatorPenguinServer
                using (SqlCommand cmd = new SqlCommand("", e.Connection.Connection, e.Transaction))
                {
                    cmd.CommandText = @"
                        if not exists(select * from STOCK where (s_id=@id_art) and (s_lot=1))
                        begin
                            select '' as id_lot
                            return
                        end
                        if not exists(select * from ARTLOT where (id_art=@id_art) and (lib=@lib))
                        begin
                            declare @num bigint , @id_lot char(10)
                            select @num=isnull(num,0)+1 from ARTLOT where (id_art=@id_art)
                            set @id_lot=right(newid(),10)
                            insert into ARTLOT (id_lot,id_art,id_depot,num,creation,lib) values (@id_lot,@id_art,@id_depot,@num,getdate(),@lib)
                            select @id_lot as id_lot
                        end
                        else
                            select id_lot from ARTLOT where (id_art=@id_art) and (lib=@lib)";
                    foreach (var l in billingEngine.LignesARecords.Where(p => !string.IsNullOrWhiteSpace(p.LOT_INFO)))
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@id_art", l.ID_ARTICLE).SqlDbType = SqlDbType.Char;
                        cmd.Parameters.AddWithValue("@lib", l.LOT_INFO).SqlDbType = SqlDbType.Char;
                        cmd.Parameters.AddWithValue("@id_depot", billingEngine.PiedsARecord.ID_DEPOT).SqlDbType = SqlDbType.Char;
                        object ox = Api.ZselectDirect(cmd);
                        if (ox == null)
                        {
                            billingEngine.LastError = Api.LastError;
                            e.CancelSave = true;
                            return;
                        }
                        l.ID_LOT = ox.ToString();
                    }
                }
            }
        }

    }
}