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

namespace Billing
{
    public class Customizer : MercatorSignPad.ISignPadPenguin
    {
        public SignPadPenguinRet FormDescriptorForOkSign(Form form)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)form;
            Dictionary<string, string> dicoNotEmpty = new Dictionary<string, string>
                {
                    { "NOM", _Divers.Iif_langue(Globals.Langue, "Name", "Naam", "Nom") },
                    { "EMAIL", _Divers.Iif_langue(Globals.Langue, "Email", "Email", "Email") },
                    { "FONCTION", _Divers.Iif_langue(Globals.Langue, "Function", "Functie", "Fonction") },
                    { "NUM_GSM", _Divers.Iif_langue(Globals.Langue, "Mobile", "GSM", "GSM") },
                };
            foreach (KeyValuePair<string, string> kvp in dicoNotEmpty)
            {
                if (string.IsNullOrWhiteSpace(billingForm.BillingEngine.PIEDS[kvp.Key].ToString()))
                {
                    return new SignPadPenguinRet
                    {
                        CancelMessage = string.Format(_Divers.Iif_langue(Globals.Langue,
                                            "The \"{0}\" field is mandatory!",
                                            "Het veld \"{0}\" is verplicht!",
                                            "Le champ \"{0}\" est obligatoire !"), kvp.Value)
                    };
                }
            }

            Regex regex = new Regex(@"^\+?[1-9]\d{1,14}$");
            if (!regex.IsMatch(billingForm.BillingEngine.PIEDS["NUM_GSM"].ToString()))
            {
                return new SignPadPenguinRet
                {
                    CancelMessage = _Divers.Iif_langue(Globals.Langue,
                        "The mobile phone number must start with + and contain only digits!",
                        "Het GSM nummer moet beginnen met + en mag alleen cijfers bevatten!",
                        "Le numéro de GSM doit commencer par + et ne contenir que des chiffres !")
                };
            }

            MercatorUi._BaseClasses.SqlFileView sqlFileView = billingForm.MovableControls.Values.OfType<MercatorUi._BaseClasses.SqlFileView>().FirstOrDefault(p => !p.ReadOnly);
            if (sqlFileView == null)
                return new SignPadPenguinRet { CancelMessage = "No SqlFileView!" };

            string signedPdfFileName = string.Format("{0}_{1}_signed.pdf", billingForm.BillingEngine.Journal, billingForm.BillingEngine.PiedsVRecord.DATE.ToShortDateString().Replace("/", "_"));

            MercatorSigning.OkSign.FormDescriptor.FormHelper formHelper1 = new MercatorSigning.OkSign.FormDescriptor.FormHelper
            {
                FieldMarker = "_Signature_",
                FieldWidth = 175,
                FieldHeight = 70,
                FieldSigningOptions = MercatorSigning.OkSign.FormDescriptor.FormHelper.SigningOptionsEnum.Tan | MercatorSigning.OkSign.FormDescriptor.FormHelper.SigningOptionsEnum.Pen,
                SignerInfoName = billingForm.BillingEngine.PiedsVRecord.NOM,
                SignerInfoMobile = billingForm.BillingEngine.PiedsVRecord.NUM_GSM,
                SignerInfoActingAs = billingForm.BillingEngine.PiedsVRecord.FONCTION,
                SignerInfoEmail = billingForm.BillingEngine.PiedsVRecord.EMAIL
            };
            MercatorSigning.OkSign.FormDescriptor formDescriptor = new MercatorSigning.OkSign.FormDescriptor
            {
                SendToMeEmail = "info@mercator.eu", // niet vereist als het te gebruiken e-mailadres dat van het OkSign-account is.
                Logo = "https://www.mercator.eu/assets/images/logo.png", // niet vereist als het te gebruiken logo het logo in het OkSign-account is
                FileName = signedPdfFileName // naam van het bestand dat zichtbaar is voor de ondertekenaar. Mag een vrije string bevatten die eindigt op .pdf
            };
            formDescriptor.FormHelpers.Add(formHelper1);

            return new SignPadPenguinRet
            {
                FormDescriptor = formDescriptor,
                SignedFileName = Api.AddBS(sqlFileView.BasePath) + signedPdfFileName
            };
        }
    }
}