using System.Collections.Generic;
using System.Data;
using Consulenza.ReportCommon;
using System;
using System.Linq;

namespace Consulenza.ReportWriter.Manager
{
    public enum SectionLoggerMessageLevel
    {
        /// <summary>
        /// Errore
        /// </summary>
        E,

        /// <summary>
        /// Warning
        /// </summary>
        W,

        /// <summary>
        /// Informazione
        /// </summary>
        I
    }

    /// <summary>
    /// Si occupa di scrivere Errori, Warning o Information in funzione del TipoReport.
    /// Per ConsulenzaBase_Proposta scrive attraverso la stored sp_ReportProposal_SectionLogger_Write nella tabella [ConsulenzaBase].[Global_Error].
    /// Per ConsulenzaEvoluta_Immobiliare scrive attraverso la stored "REP_TracciaErrore2" nella tabella [ConsulenzaEvoluta].[REP_Errore2].
    /// </summary>
    public static class SectionLogger
    {
        /// <summary>
        /// Scrive il log.
        /// </summary>
        /// <param name="classSender"></param>
        /// <param name="message"></param>
        /// <param name="level"></param>
        /// <param name="reportEnvironment"></param>
        public static void Write(string classSender, string message, SectionLoggerMessageLevel level, ReportEnvironment reportEnvironment)
        {
            var parametri = new List<Parametro>
            {
                new Parametro
                {
                    DbType = DbType.String,
                    Direction = ParameterDirection.Input,
                    ParameterName = "TipoReport",
                    Value = reportEnvironment.ReportType.ToString()
                },
                new Parametro
                {
                    DbType = DbType.Int32,
                    Direction = ParameterDirection.Input,
                    ParameterName = "ChiavePb",
                    Value = reportEnvironment.Cliente.Chiave
                },               
                new Parametro
                {
                    DbType = DbType.String,
                    Direction = ParameterDirection.Input,
                    ParameterName = "Rete",
                    Value = reportEnvironment.PrivateBanker.CodiceRete
                },
                new Parametro
                {
                    DbType = DbType.String,
                    Direction = ParameterDirection.Input,
                    ParameterName = "ClassSender",
                    Value = classSender
                },
                new Parametro
                {
                    DbType = DbType.String,
                    Direction = ParameterDirection.Input,
                    ParameterName = "Messaggio",
                    Value = message
                },
                new Parametro
                {
                    DbType = DbType.String,
                    Direction = ParameterDirection.Input,
                    ParameterName = "Livello",
                    Value = level.ToString()
                }
            };

            switch (reportEnvironment.ReportType)
            {
                case ReportType.Base_Proposta:
                    parametri.Add(new Parametro { DbType = DbType.Int32, Direction = ParameterDirection.Input, ParameterName = "ChiaveProposta", Value = reportEnvironment.Proposta.Chiave });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "StatoAdeguatezza", Value = reportEnvironment.Proposta.EsitoAdeguatezza });
                    if (reportEnvironment.Proposta.PropostaCBUnica)
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerConsulenzaUnica, "sp_ReportProposal_SectionLogger_Write", parametri);
                    else
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerConsulenzaBase, "sp_ReportProposal_SectionLogger_Write", parametri);
                    break;
                case ReportType.Immobiliare_Diagnosi:
                case ReportType.Immobiliare_DiagnosiNucleo:
                case ReportType.Immobiliare_Finalita:
                case ReportType.Immobiliare_Monitoraggio:
                case ReportType.Immobiliare_MonitoraggioNuclei:

                    var listaimmobiliEnvironment = reportEnvironment.Immobiliare.IdImmobileCatasto ?? reportEnvironment.NucleoImmobiliare.IdImmobileCatasto ?? reportEnvironment.FinalitaImmobiliare.IdImmobileCatasto ;
                    var listasezioniEnvironment = reportEnvironment.Immobiliare.IdSezione ?? reportEnvironment.NucleoImmobiliare.IdSezione ?? reportEnvironment.FinalitaImmobiliare.IdSezione;

                    parametri.Add(new Parametro { DbType = DbType.Int64, Direction = ParameterDirection.Input, ParameterName = "ChiaveNucleo", Value = reportEnvironment.NucleoImmobiliare.ChiaveNucleo });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdImmobileCatasto", Value = string.Join(";", listaimmobiliEnvironment) });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdSezione", Value = string.Join(";", listasezioniEnvironment) });
                    if(reportEnvironment.Immobiliare.ImmobiliareCEUnica || reportEnvironment.FinalitaImmobiliare.ImmobiliareCEUnica || reportEnvironment.NucleoImmobiliare.ImmobiliareCEUnica)
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerReportModeler, "REP_TracciaErrore2", parametri);
                    else
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerConsulenzaEvoluta, "REP_TracciaErrore2", parametri);
                    break;
                case ReportType.Immobiliare_MappaturaPreliminare:
                    parametri.Add(new Parametro { DbType = DbType.Int64, Direction = ParameterDirection.Input, ParameterName = "ChiaveNucleo", Value = reportEnvironment.NucleoImmobiliare.ChiaveNucleo });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdImmobileCatasto", Value = string.Join(";", reportEnvironment.MappaturaPreliminare.IdImmobileCatasto) });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdSezione", Value = string.Join(";", reportEnvironment.MappaturaPreliminare.IdSezione) });
                    if (reportEnvironment.MappaturaPreliminare.ImmobiliareCEUnica)
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerReportModeler, "REP_TracciaErrore2", parametri);
                    else
                        DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerConsulenzaEvoluta, "REP_TracciaErrore2", parametri);
                    break;
               
                case ReportType.Unica_Diagnosi:
                case ReportType.Unica_Monitoraggio:
                case ReportType.Unica_Proposta:
                case ReportType.Unica_Nucleo:
                    var listaschede = from i in reportEnvironment.ReportStruct.Schede.Select(l => l.Id).ToList() select i;
                    parametri.Add(new Parametro { DbType = DbType.Int64, Direction = ParameterDirection.Input, ParameterName = "ChiaveNucleo", Value = reportEnvironment.Cliente.Chiave});
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdImmobileCatasto", Value = string.Join(";", "") });
                    parametri.Add(new Parametro { DbType = DbType.String, Direction = ParameterDirection.Input, ParameterName = "Lista_IdSezione", Value = string.Join(";", listaschede) });

                    DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerReportModeler, "REP_TracciaErrore2", parametri);
                    break;

              
                default:
                    throw new Exception("ReportType da gestire: [Consulenza.ReportWriter.Manager.Write()]");
            }
        }
    }
}
