using System;
using System.Collections.Generic;
using Consulenza.ReportArchive.Entity;
using Consulenza.ReportArchive;

namespace Consulenza.Unica.Business
{
    public class ReportArchiveService
    {
        public long SetDocumentPdf(DocumentoPdf documentoPdf, string idProvenienza)
        {
            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            long idPdf = 0;
            try
            {
                idPdf = reportArchiveManager.SetDocumentPdf(documentoPdf, idProvenienza);
            }
            catch (Exception ex)
            {
                insertLog(idPdf.ToString(), "SetDocumentPdf", ex);
                return -1;
            }
            return idPdf;
        }

        public int DelPdf(long idPdf, string idProvenienza)
        {

            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            int err = -1;
            try
            {
                err = reportArchiveManager.DelPdf(idPdf, idProvenienza);
            }
            catch (Exception ex)
            {
                insertLog(idPdf.ToString(), "DelPdf", ex);
                return -1;
            }
            if (err == -1)
                insertLog(idPdf.ToString(), "DelPdf", "reportArchiveManager.DelPdf(..) return -1");
            return err;
        }

        public int SetStato(long idPdf, string idstato, string idProvenienza)
        {
            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            int err = -1;
            try
            {
                err = reportArchiveManager.SetStato(idPdf, idstato, idProvenienza);
            }
            catch (Exception ex)
            {
                insertLog(idPdf.ToString(), "SetStato", ex);
                return -1;
            }
            if (err == -1)
                insertLog(idPdf.ToString(), "SetStato", "reportArchiveManager.SetStato(..) return -1");
            return err;
        }

        public List<LogPdf> GetlogPdf(long idPdf)
        {
            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            List<LogPdf> logPdfs = new List<LogPdf>();
            try
            {
                logPdfs = reportArchiveManager.GetlogPdf(idPdf);
            }
            catch (Exception ex)
            {
                logPdfs = new List<LogPdf>();
                insertLog(idPdf.ToString(), "GetlogPdf", ex);
            }
            return logPdfs;
        }

        public List<DocumentoPdf> GetPdfs(RicercaPdf ricercaPdf)
        {
            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            List<DocumentoPdf> documentoPdfs = new List<DocumentoPdf>();

            try
            {
                documentoPdfs = reportArchiveManager.GetPdfs(ricercaPdf);
            }
            catch (Exception ex)
            {
                documentoPdfs = new List<DocumentoPdf>();
                long idPdf = ricercaPdf != null ? ricercaPdf.IdPdf : 0;
                insertLog(idPdf.ToString(), "GetPdfs", ex);
            }

            return documentoPdfs;
        }

        public DocumentoPdf GetPdf(long idPdf)
        {
            ReportArchiveManager reportArchiveManager = new ReportArchiveManager();
            DocumentoPdf documentoPdf = new DocumentoPdf();
            try
            {
                documentoPdf = reportArchiveManager.GetPdf(idPdf);
            }
            catch (Exception ex)
            {
                documentoPdf = new DocumentoPdf();
                insertLog(idPdf.ToString(), "GetPdf", ex);
            }
            return documentoPdf;
        }

        private void insertLog(string idPdf, string metodo, string message)
        {
            try
            {
                List<Parametro> param = new List<Parametro>();
                param.Add(new Parametro { Value = idPdf, ParameterName = "idPdf" });
                param.Add(new Parametro { Value = metodo, ParameterName = "metodo" });
                param.Add(new Parametro { Value = message, ParameterName = "message" });
                DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerReportArchive, "INSERTERROR", param);
            }
            catch { }
        }
        private void insertLog(string idPdf, string metodo, Exception exception)
        {
            if (exception is DataBaseException)
                insertLog(idPdf, metodo, ((DataBaseException)exception).Eccezione.Message);
            else
                insertLog(idPdf, metodo, exception.Message);
        }

    }
}
