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

// <CompileWithRoslyn />

namespace Main
{
    public class Customizer
    {
        public void DownloadAndDispatchPeppolMessages()
        {
            Globals.MercatorTasksToMain.Log("Instantiatie van de PeppolBoxHelper");
            using (MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper peppolBoxHelper = new MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper())
            {
                if (peppolBoxHelper.DataTable == null)
                    Globals.MercatorTasksToMain.Log("Fout bij het initialiseren van de PeppolBoxHelper: " + Api.LastError, isError: true);
                else if (!peppolBoxHelper.Download())
                    Globals.MercatorTasksToMain.Log("Fout bij het downloaden van berichten: " + Api.LastError, isError: true);
                else
                {
                    Globals.MercatorTasksToMain.Log("Bericht(en) gedownload: " + peppolBoxHelper.Messages.Count(m => m.IsNew));
                    int nActions = 0;
                    foreach (MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper.PeppolMessage message in peppolBoxHelper.Messages.Where(m => m.IsNew && m.IsInvoiceOrCreditNote))
                    {
                        MercatorPeppol.ReceivedDoc.ParseContentRet parsedContent = message.ReceivedDoc.ParseContent(Globals.Langue, MercatorPeppol.ReceivedDoc.ParseContentEnum.WithSupplierName | MercatorPeppol.ReceivedDoc.ParseContentEnum.WithPdf);
                        if (!string.IsNullOrWhiteSpace(parsedContent.Error))
                        {
                            Globals.MercatorTasksToMain.Log(string.Format("Bericht verwerken {0} : {1}", message.Id, parsedContent.Error), isError: true);
                            continue;
                        }

                        string id_actempl = null;
                        // hier worden de testen geplaatst om op basis van de inhoud van het bericht het juiste CRM-actietype te bepalen
                        if (...)
                        {
                            id_actempl = ".A-0RFCEXW"; // id_actempl gescom
                        }
                        else if (...)
                        {
                            id_actempl = ".A-KC6V639"; // id_actempl compta
                        }
                        if (id_actempl == null)
                            continue; // dit bericht wordt niet gedistribueerd

                        using (MercatorUi.Engine.Crm.ActionEngine actionEngine = MercatorUi.Engine.Crm.ActionEngine.InitNew(MercatorUi.Sig._SigEnum._none, null, id_actempl))
                        {
                            if (actionEngine == null)
                            {
                                Globals.MercatorTasksToMain.Log("ActionEngine.InitNew : " + actionEngine.LastError, isError: true);
                                continue;
                            }
                            if (actionEngine.ReadOnly)
                            {
                                Globals.MercatorTasksToMain.Log("ActionEngine.InitNew : engine is ReadOnly!");
                                continue;
                            }
                            actionEngine.ActionsRecord.OBJET = (message.ReceivedDoc.ChangeType == "INVOICE_RECEIVED" ? "Factuur" : "Creditnota") + " Peppol van " + parsedContent.SupplierName;
                            actionEngine.ActionsRecord.ENTRYID = message.Id.ToString();
                            actionEngine.ActionsRecord.MOMENT_2 = message.Moment;
                            actionEngine.ActionsRecord.MOMENT_1 = message.Moment.AddDays(5); // deadline voor de actie op 5 dagen

                            if (!actionEngine.Save())
                            {
                                Globals.MercatorTasksToMain.Log(string.Format("Bericht {0} : fout bij het bewaren van de actie: {1}", message.Id, actionEngine.LastError), isError: true);
                                continue;
                            }
                            else if (parsedContent.Pdf == null)
                            {
                                Globals.MercatorTasksToMain.Log(string.Format("Bericht {0} : geen PDF in het bericht", message.Id), isError: true);
                            }
                            else
                            {
                                if (!Api.BytesToSqlFile(parsedContent.Pdf, Api.AddBS(actionEngine.SqlFileViewDefaultDirectory) + "PeppolDoc.pdf"))
                                    Globals.MercatorTasksToMain.Log(string.Format("Bericht {0} : kan PDF niet aan actie koppelen", message.Id), isError: true);
                            }
                               
                            nActions++;
                        }
                    }
                    Globals.MercatorTasksToMain.Log("Aangemaakte actie(s): " + nActions);
                }
            }
        }
    }
}