using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using PDFGenerator.Presentation.Section;
using PDFGenerator.BusinessLayer.DataSection;
using System.Data;
using DataAccessLayer;
using System.Data.SqlClient;
using System.Data.Common;
using System.Configuration;

namespace PDFGenerator.BusinessLayer
{
    public class DataThread : DataThreadModel
    {
        public DataThread(DBProvider dbProvider)
        {
            Agente = "";
            Area = "";
            AreaEstesa = "";
            CapAgente = "";
            CittaAgente = "";
            CodiceFiscale = "";
            CognomeAgente = "";
            CognomeCliente = "";
            IndirizzoAgente = "";
            Intermediario = "";
            NomeAgente = "";
            NomeCliente = "";
            NomeRete = "";
            Rete = "";
            TelefonoAgente = "";
            TipoReport = "";
            ReportsType = new List<ReportType>();
            DocumentPDF = null;
            Data = null;
            SezioneReport = null;
            VaRRisorseNonAssociate = 0;
            CoperturaRisorseNonAssociate = 0;
            PatrimonioTerziContoCorrente = 0;
            DataAccess = new DataAccessDE(dbProvider);
            IsAdequatezza = null;
            IsSavingMap = false;
            IsAPC103 = false;
        }
        public ReportType ReportType()
        {
            return ReportsType.Find(
                    delegate (ReportType r)
                    {
                        return r.Descrizione.ToUpper() == TipoReport.ToUpper();
                    }
                );
        }

        //      80, 128, 129, 131, 132, 146, 151, 169,
        public bool IsProffesionalClient
        {
            get
            {
                try
                {

                    return (this.Flgprof == "S");// &&  ConfigurationManager.AppSettings["privateClient"] == "true";
                }
                catch
                {
                    return false;
                }
            }
        }

        //public DateTime ContractSubscriptionDate
        //{
        //    get
        //    {
        //        string data_year = ""; string data_month = ""; string data_day = "";
        //        using (DataAccessDE dde = new DataAccessDE(DBProvider.SqlServerStampeC6))
        //        {
        //            DataTable dx = dde.ExecuteDataTable(CommandType.Text, "Select year(data_InzioAvanzato) as y, month(data_InzioAvanzato) as m, day(data_InzioAvanzato) as d  FROM  c6mart.vcontratti where cod_fiscale = '" + this.CodiceFiscale + "' and rete = '" + this.Rete + "'", new DbParameter[0]);
        //            data_year = dx.Rows[0][0].ToString();
        //            data_month = dx.Rows[0][1].ToString();
        //            data_day = dx.Rows[0][2].ToString();

        //        }

        //        return new DateTime(Int32.Parse(data_year), Int32.Parse(data_month), Int32.Parse(data_day));
        //    }
        //}

        public DateTime ContractSubscriptionDate
        {
            get
            {
              
                    string data_y = "";
                    string data_m = "";
                    string data_d = "";
                    using (DataAccessDE dde = new DataAccessDE(DBProvider.SqlServerStampeC6))
                    {
                        DataTable dx = dde.ExecuteDataTable(CommandType.Text, "Select year(DATA_PERF) as y, month(DATA_PERF) as m, day(DATA_PERF) as d FROM  [C6MartPeriodico].[RP_vContrattiPerGenerazioneReport] where cod_fiscale = '" + this.CodiceFiscale + "' and rete = '" + this.Rete + "'", new DbParameter[0]);
                        data_y = dx.Rows[0][0].ToString();
                        data_m = dx.Rows[0][1].ToString();
                        data_d = dx.Rows[0][2].ToString();

                }
                    
                    return new DateTime(Int32.Parse(data_y), Int32.Parse(data_m), Int32.Parse(data_d));
              
            }
        }

        public DataTable getDataTable(string tableName)
        {
            //sp_getColors
            return new DataTable();
        }

        public bool clienteAdeguato(string pRete, string pCodiceFiscale, bool pPeriodico)
        {


            var parametri = new List<Parametro>
                {
                    new Parametro
                    {
                        Direction = ParameterDirection.Input,
                        DbType = DbType.String,
                        ParameterName = "Rete",
                        Value = pRete
                    },
                    new Parametro
                    {
                        Direction = ParameterDirection.Input,
                        DbType = DbType.String,
                        ParameterName = "CodiceFiscale",
                        Value = pCodiceFiscale
                    }

                };

            string sSql = "";

            if (pPeriodico)
                sSql = "[C6MartPeriodico].[PL_D_S178IndicatoriEsitoAdeguatezza]";
            else
                sSql = "[C6Mart].[PL_D_S178IndicatoriEsitoAdeguatezza]";

            DataAccessDE dataAccess = new DataAccessDE(DBProvider.SqlServerStampeC6);
            DataTable dt = dataAccess.ExecuteDataTableStoredProcedure(DBProvider.SqlServerStampeC6, sSql, parametri);

            //var dsBatch = DataAccessDE.ExecuteDataSetStoredProcedure(DBProvider.SqlServerStampeC6, "[C6MartPeriodico].[PL_D_S178IndicatoriEsitoAdeguatezza]", parametri);
            if (dt.Rows.Count > 0)
            {
                bool bRet = true;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][2].ToString() == "0")
                    {
                        bRet = false;
                        break;
                    }

                }
                return bRet;
            }
            else
                return false;

        }

    }
}