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

// <CompileWithRoslyn />

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {
        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.TagString = "999999;999999-9FB99999-99CD-9F99-9B99-B9EAE99DDC99;As99ErT999XX999XxxXXxxXx9";
            billingEngine.BeforeSave += BillingEngine_BeforeSave;
            billingEngine.PdfDuplicateCreated += BillingEngine_PdfDuplicateCreated;
        }

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

        private void BillingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (billingEngine.IsNew)
            {
                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(billingEngine.PIEDS[kvp.Key].ToString()))
                    {
                        Dialogs.Stop(string.Format(_Divers.Iif_langue(Globals.Langue,
                            "The \"{0}\" field is mandatory!",
                            "Het veld \"{0}\" is verplicht!",
                            "Le champ \"{0}\" est obligatoire !"), kvp.Value));
                        List<Control> l = billingEngine.BillingForm.FindMovableControlsBySource(kvp.Key);
                        if (l.Any())
                            _Divers.FocusError(l[0]);
                        e.CancelSave = true;
                        return;
                    }
                }

                Regex regex = new Regex(@"^\+?[1-9]\d{1,14}$");
                if (!regex.IsMatch(billingEngine.PiedsVRecord.NUM_GSM))
                {
                    Dialogs.Stop(_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 !"));
                    List<Control> l = billingEngine.BillingForm.FindMovableControlsBySource("NUM_GSM");
                    if (l.Any())
                        _Divers.FocusError(l[0]);
                    e.CancelSave = true;
                    return;
                }
            }
        }


        private void BillingEngine_PdfDuplicateCreated(object sender, MercatorUi.Engine.Gescom.BillingEngine.PdfDuplicateCreatedEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (billingEngine.IsNew)
            {
                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.Eid | MercatorSigning.OkSign.FormDescriptor.FormHelper.SigningOptionsEnum.Pen | MercatorSigning.OkSign.FormDescriptor.FormHelper.SigningOptionsEnum.Itsme,
                    SignerInfoName = billingEngine.PiedsVRecord.NOM,
                    SignerInfoMobile = billingEngine.PiedsVRecord.NUM_GSM,
                    SignerInfoActingAs = billingEngine.PiedsVRecord.FONCTION,
                    SignerInfoEmail = 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 = $"Devis_{billingEngine.Piece}.pdf" // naam van het bestand dat zichtbaar is voor de ondertekenaar. Mag een vrije string bevatten die eindigt op .pdf
                };
                formDescriptor.FormHelpers.Add(formHelper1);

                MercatorUi.Wait.WaitStatic.WaitWindow("OkSign...");

                MercatorSigning.OkSign.OkSignResponse response = MercatorSigning.OkSign.UploadPdfForSignature(e.PdfFile, formDescriptor, billingEngine.TagString, out string error);;
                if (!string.IsNullOrEmpty(error))
                {
                    MercatorUi.Wait.WaitStatic.WaitClear();
                    Dialogs.Stop(error);
                }
                else
                {
                    bool isOk;
                    using (SqlCommand cmd = new SqlCommand("update PIEDS_V set OKSIGN_DOC_ID=@OKSIGN_DOC_ID,OKSIGN_SENT=getdate() where (id=@id) and (journal=@journal) and (piece=@piece)"))
                    {
                        cmd.Parameters.AddWithValue("@id", billingEngine.Id).SqlDbType = SqlDbType.Char;
                        cmd.Parameters.AddWithValue("@journal", billingEngine.Journal).SqlDbType = SqlDbType.Char;
                        cmd.Parameters.AddWithValue("@piece", billingEngine.Piece);
                        cmd.Parameters.AddWithValue("@OKSIGN_DOC_ID", response.DocId);
                        isOk = Api.SqlExec(MercatorUi.Globals.RepData, cmd);
                    }
                    if (!isOk)
                    {
                        MercatorUi.Wait.WaitStatic.WaitClear();
                    }
                    else
                    {
                        billingEngine.PIEDS["OKSIGN_DOC_ID"] = response.DocId;
                        billingEngine.PIEDS["OKSIGN_SENT"] = DateTime.Now;

                        string msg = "Beste klant<br><br>"
                                   + "Gebruik onderstaande link als u onze offerte wilt accepteren.<br>"
                                   + string.Format("<a href=\"{0}\">Link</a><br>", response.Result[0].Url)
                                   + response.Result[0].Url // URL van de eerste ondertekenaar. Foreach vereist indien meerdere ondertekenaars om een verschillende e-mail naar elke ondertekenaar te sturen.
                                   + "<br><br>"
                                   + "Wij danken u hiervoor.<br><br>"
                                   + "Sales Team";

                        Smtp mail = new Smtp
                        {
                            MailServer = Globals.Params["SMTP_HOST"],
                            Recipient = billingEngine.PiedsVRecord.EMAIL,
                            SenderEmail = Globals.CurrentUserRecord.CRM_MAIL,
                            SenderName = Globals.CurrentUserRecord.CRM_NOM,
                            Subject = billingEngine.BillingForm.Text,
                            ForceHtml = true,
                            Message = "<html><body>" + msg + "</body></html>"
                        };
                        if (!mail.SendMail())
                        {
                            MercatorUi.Wait.WaitStatic.WaitClear();
                            Dialogs.Stop(mail.Error);
                        }
                        else
                        {
                            MercatorUi.Wait.WaitStatic.WaitClear();
                            Dialogs.Stop("Offerte correct verzonden voor aanvaarding via Ok!Sign !");
                         }
                    }
                }
            }
        }
    }
}