using betrace_carfi_erp_driver.Components;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static betrace_carfi_erp_driver.Components.Document;

namespace betrace_carfi_erp_driver.Common
{
    static class Integrator
    {
        static public bool ExecuteIntegration(IConnectorERP conERP)
        {   
            ExecuteIntegrationArticles(conERP);
            ExecuteIntegrationDocuments(conERP);
            
           
            return true;
        }

        static public bool ExecuteIntegrationArticles(IConnectorERP conERP)
        {
            try
            {
                List<Article> articles = Database.GetArticlesToIntegrate();
                if (articles.Count() > 0)
                {
                    Logger.WriteEntry(articles.Count().ToString() + " articles will be integrated.", EventLogEntryType.Information);
                    foreach (Article article in articles)
                    {
                        try
                        {
                            IntegrateArticle(conERP, article);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteEntry(ex.Message, EventLogEntryType.Error);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                Logger.WriteEntry("Error during article integration.\n " + ex.Message, EventLogEntryType.Error);
            }

            return true;
        }

        static public bool IntegrateArticle(IConnectorERP conERP, Article article)
        {
            try
            {
                if (conERP.UpdateArticle(article))
                {
                    Database.RemoveQueueArticles(article.Id);
                    Logger.WriteEntry("Article ID " + article.Id + " Code: " + article.ArticleCode + " successfully integrated.", EventLogEntryType.Information);
                }
            }
            catch (Exception ex)
            {
                Database.IncrementAttemptsArticles(article.Id);

                string listaEmail = Database.PesquisaEmailEnvio();
                Mail enviaEmail = new Mail();
                string Assunto = "Erro de Artigo na Integração Driver de ligação Primavera";
                string corpo = "<h3>" + "O Artigo " + article.ArticleCode + " Não foi integrado com sucesso.\n " + ex.Message + "</h3>";

                enviaEmail.TrataEmail(listaEmail,corpo,"BeTrace Alerta",Assunto);

                throw new Exception("Article ID " + article.Id + " Code: " + article.ArticleCode + " was not successfully integrated.\n" + ex.Message);
            }

            return true;
        }

        static public bool ExecuteIntegrationDocuments(IConnectorERP conERP)
        {
            try
            {
                List<Cab> documents = Database.GetDocumentsToIntegrate();
                if(documents.Count() > 0)
                {
                    Logger.WriteEntry(documents.Count() + " documents will be integrated.", EventLogEntryType.Information);
                    foreach (Cab document in documents)
                    {
                        try
                        {
                            IntegrateDocument(conERP, document);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteEntry(ex.Message, EventLogEntryType.Error);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                Logger.WriteEntry("Error during document integration.\n " + ex.Message, EventLogEntryType.Error);
            }

            return true;
        }

        static public bool IntegrateDocument(IConnectorERP conERP, Cab document)
        {
            try
            {
                string docNumber = String.Empty;
                List<Lin> lines = Database.GetLinesToIntegrate(document.Id);
                if (conERP.UpdateDocument(document, lines, ref docNumber))
                {
                    Database.RemoveQueueDocuments(document.Id, docNumber);
                    Logger.WriteEntry("Document " + document.Id + " successfully integrated on document " + docNumber + ".", EventLogEntryType.Information);
                }
                else
                {
                //    Logger.WriteEntry("Document " + document.Id + " successfully integrated on document " + docNumber + ".", EventLogEntryType.Information);

                }
            }
            catch (Exception ex)
            {
                Database.IncrementAttemptsDocuments(document.Id);

                string listaEmail = Database.PesquisaEmailEnvio();
                Mail enviaEmail = new Mail();
                string Assunto = "Erro Integração Driver de ligação Primavera";


                string Corpo = "<h3>" + "Documento " + document.Id + " Não foi integrado com sucesso.\n " + ex.Message + "</h3>";
               
                    
                    
                    
                    enviaEmail.TrataEmail(listaEmail,Corpo, "BeTrace Alerta", Assunto);

               


                throw new Exception("Document " + document.Id + " was not successfully integrated.\n " + ex.Message);
            }

            return true;
        }
    }
}

