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

namespace Booking
{
    public class Customizer : MercatorUi.ICustomizers.IBookingEngineCreated, MercatorUi.ICustomizers.IBookingEngineClosed
    {
        public void BookingEngineCreated(MercatorUi.Engine.Cpta.BookingEngine bookingEngine)
        {
            bookingEngine.DuringSave += new MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventHandler(bookingEngine_DuringSave);
        }

        public void BookingEngineClosed(MercatorUi.Engine.Cpta.BookingEngine bookingEngine)
        {
            bookingEngine.DuringSave -= new MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventHandler(bookingEngine_DuringSave);
        }

        private void bookingEngine_DuringSave(object sender, MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventArgs e)
        {
            Int16 rupture = 3;
            StringBuilder reqSql = new StringBuilder();
            reqSql.AppendLine("declare @val_der_tot float,@val_der_tot_dv float,@tot_rupt float,@tot_rupt_dv float");
            reqSql.AppendLine("select @tot_rupt = round(tot/@rupture,@n_dec),@val_der_tot = tot-@tot_rupt,@tot_rupt_dv = round(tot_dv/@rupture,@n_dec), @val_der_tot_dv = tot_dv-@tot_rupt_dv from #lignes_c_tmp where recno = 1");
            reqSql.AppendLine("update #lignes_c_tmp set  tot = @tot_rupt,tot_dv = @tot_rupt_dv where recno = 1");


            StringBuilder req_prem_part = new StringBuilder();
            StringBuilder req_sec_part = new StringBuilder();
            req_prem_part.AppendLine("(");
            req_sec_part.AppendLine("select ");
            foreach (KeyValuePair<string, MercatorUi.Engine.Cpta.Tools.FieldDescriptor> kvp in MercatorUi.Globals.EngineCpta.FieldListLignes)
            {
                req_prem_part.Append(kvp.Key + ",");

                if (kvp.Key == "DL_ID")
                    req_sec_part.Append("right(newid(),10),");
                else if (kvp.Key == "RECNO")
                    req_sec_part.Append("recno+1,");
                else if (kvp.Key == "ECHEANCE")
                    req_sec_part.Append("dbo.add_month(echeance,1),");
                else
                    req_sec_part.Append(kvp.Key + ",");
            }
            req_prem_part.Remove(req_prem_part.Length - 1, 1).Append(") ");
            req_sec_part = req_sec_part.Remove(req_sec_part.Length - 1, 1).Append(" ");



            for (int i = 1; i < rupture - 1; i++)
            {
                reqSql.AppendLine("insert into #lignes_c_tmp "
                    + req_prem_part.ToString()
                    + req_sec_part.ToString().Replace("recno+1", "recno+1+" + i.ToString()).Replace("dbo.add_month(echeance,1)", "dbo.add_month(echeance,1+" + i.ToString() + ")")
                    + "from #lignes_c_tmp where recno = 1");
                reqSql.AppendLine("select @val_der_tot = @val_der_tot-@tot_rupt,@val_der_tot_dv = @val_der_tot_dv-@tot_rupt_dv");
            }

            reqSql.AppendLine("insert into #lignes_c_tmp "
                + req_prem_part.ToString()
                + req_sec_part.ToString().Replace(",TOT,", ",@val_der_tot,").Replace(",TOT_DV,", ",@val_der_tot_dv,")
                + "from #lignes_c_tmp where recno = 1");

            e.SqlCommand.CommandText = reqSql.ToString();
            e.SqlCommand.Parameters.AddWithValue("@rupture", rupture);
            if (!Api.SqlExec(e.SqlCommand))
                e.CancelSave = true;
            /*e.SqlCommand.CommandText = "select * from #lignes_c_tmp";
            DataSet ds = Api.Zselect(Globals.RepData, e.SqlCommand);
            _Divers.ViewData(ds);*/

        }
    }
}