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


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

            if (!((billingEngine.PIEDS["sign_img"] == DBNull.Value) || (((byte[])billingEngine.PIEDS["sign_img"]).Length == 0)))
            {
                try
                {
                    MercatorCryptorEngine.CryptorEngine.SecurityKey = getSecurityKey(billingEngine);
                    billingEngine.PIEDS["sign_img"] = MercatorCryptorEngine.CryptorEngine.Decrypt((byte[])billingEngine.PIEDS["sign_img"], false);
                    MercatorCryptorEngine.CryptorEngine.SecurityKey = null;
                }
                catch (Exception ex)
                {
                    billingEngine.PIEDS["sign_img"] = DBNull.Value;
                    MercatorUi.Dialogs.Stop("Ce document contient une signature, mais elle ne peut être décryptée. Elle sera donc supprimée !\r\n\r\n" + ex.Message + (ex.InnerException != null ? "\r\n" + ex.InnerException.Message : ""));
                    return;
                }
            }

        }

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

        private string getSecurityKey(MercatorUi.Engine.Gescom.BillingEngine billingEngine) // la clé doit faire 24 caractères de long
        {
            return "...";
        }

        void BillingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;

            if (!((billingEngine.PIEDS["sign_img"] == DBNull.Value) || (((byte[])billingEngine.PIEDS["sign_img"]).Length == 0)))
            {
                byte[] imagebytes = (byte[])billingEngine.PIEDS["sign_img"];
                string commandText = "update PIEDS_V set sign_img=@sign_img where (id=@id) and (journal=@journal) and (piece=@piece)";
                SqlCommand commandForFinalTransaction = new SqlCommand(commandText);
                MercatorCryptorEngine.CryptorEngine.SecurityKey = getSecurityKey(billingEngine);
                commandForFinalTransaction.Parameters.AddWithValue("@sign_img", MercatorCryptorEngine.CryptorEngine.Encrypt(imagebytes, false));
                MercatorCryptorEngine.CryptorEngine.SecurityKey = null;

                billingEngine.CommandForFinalTransaction = commandForFinalTransaction;
            }
        }

    }
}