using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.BusinessLayer;
using System.Collections.Generic;

namespace PDFGenerator.BusinessLayer.DataSection
{
    class DSFD151ProfiloDiRischio : IDataSection
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();


        //private int _experince;
        //public int Experince
        //{
        //    set
        //    {
        //        _experince = value;
        //    }
        //    get
        //    {
        //        return _experince;
        //    }
        //}

        public DSFD151ProfiloDiRischio()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IDataSection Members

        /// <summary>
        /// Implement the getDataSection function of the interface IDataSection.
        /// </summary>
        /// <param name="tabelleSessione"></param>
        /// <param name="querySql"> Sql Statment to be executed to return the Profile Risk of the certain customer in a certain network</param>
        /// <param name="dataThread"></param>
        /// <returns> Data Table to be shown in the report</returns>
        public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
        {
            try
            {
                DataSectionResult customerProfiloRischioDataSectionResult = new DataSectionResult();

                //Random rnd = new Random();
                DataRow customerProfiloRischioDataRow;
                DataRow customerProfilofinanziarioDataRow;
                DataRow customerProfiloDateDataRow;

                DataRow customerAdeguatezzaRiserva;
                DataRow customerAdeguatezzaILP;

                //S93 Data moved to S95
                DataRow CTVDataRow;
                DataRow CTVTotali;

                decimal risorseFinanziarie = 0;
                decimal CC = 0;
                decimal investimentiInCorso = 0;
                FormatNum formatnum = new FormatNum();
                decimal totalePartiteViaggianti = dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento;

                /**** Forzatura Direct Banck da togliere 08032023 - Pino ***/
                //querySql = "[C6MartPeriodico].PL_D3_S151ProfiloDiRischio";
                /**********************************************/

                DataTable customerProfiloRischioDataTable = SectionManager.GetDataSection(tabelleSessione, querySql, dataThread);

                DataSetS151 customerProfiloRischioRecordsDataSetS151 = new DataSetS151();

                if (dataThread.profiloIsAct && customerProfiloRischioDataTable.Rows.Count > 0)
                {
                    int profiloRischioClienteAssegnato = Convert.ToInt32(customerProfiloRischioDataTable.Rows[0]["codiceProfilo"]);

                    #region Creazione righe datatable Profilofinanziario
                    // Profilo finanziario

                    customerProfilofinanziarioDataRow = customerProfiloRischioRecordsDataSetS151.Profilofinanziario.NewRow();
                    // MIFID2 20180405
                    //customerProfilofinanziarioDataRow["Descrizione"] = "Profilo finanziario";
                    //customerProfilofinanziarioDataRow["Valore"] = customerProfiloRischioDataTable.Rows[0]["codiceProfilo"].ToString() + " " + customerProfiloRischioDataTable.Rows[0]["nomeProfilo"].ToString();


                    customerProfilofinanziarioDataRow["Descrizione"] = "Profilo finanziario*";
                    customerProfilofinanziarioDataRow["Valore"] = string.Format("<b>{0} - {1}</b>: {2}",
                        customerProfiloRischioDataTable.Rows[0]["codiceProfilo"].ToString(),
                        customerProfiloRischioDataTable.Rows[0]["nomeProfilo"].ToString(),
                        customerProfiloRischioDataTable.Rows[0]["DescrizioneProfilo"].ToString().Replace(".", ".<br>"));
                    //--MIFID2 20180405

                    customerProfiloRischioRecordsDataSetS151.Profilofinanziario.Rows.Add(customerProfilofinanziarioDataRow);

                    #endregion
                    #region Creazione righe datatable profilorischio

                    string vara = SoglieVar.ConvOld(customerProfiloRischioDataTable.Rows[0]["varMassimo"]);

                    if (dataThread.IsProffesionalClient && dataThread.RiskArea == 5)
                        vara = "50,00"; //checked



                    // VaR Massimo
                    customerProfiloRischioDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerProfiloRischioDataRow["Descrizione"] = "Rischio (VaR) massimo";
                    customerProfiloRischioDataRow["Valore"] = vara;
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerProfiloRischioDataRow);

                    // Rischo Credito Massimo

                    customerProfiloRischioDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerProfiloRischioDataRow["Descrizione"] = "Rischio Credito massimo";
                    customerProfiloRischioDataRow["Valore"] = UtilityBusinessLayer.GetRiskCreditClassName(customerProfiloRischioDataTable.Rows[0]["RiskClass"].ToString());
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerProfiloRischioDataRow);

                    // Experince

                    customerProfiloRischioDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerProfiloRischioDataRow["Descrizione"] = "Experince";
                    customerProfiloRischioDataRow["Valore"] = customerProfiloRischioDataTable.Rows[0]["Experince"].ToString();
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerProfiloRischioDataRow);


                    #region Creazione righe Indicatori Riserva e Investimento lungo periodo
                    //Adeguatezza: Riserva
                    customerAdeguatezzaRiserva = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerAdeguatezzaRiserva["Descrizione"] = "Riserva";
                    //customerAdeguatezzaRiserva["Valore"] = $"{customerProfiloRischioDataTable.Rows[0]["riserva"]}€";
                    customerAdeguatezzaRiserva["Valore"] = $"{Convert.ToDecimal(customerProfiloRischioDataTable.Rows[0]["riserva"].ToString()).ToString("#,##0.00")}€";
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerAdeguatezzaRiserva);

                    //Adegautezza: Investimento di lungo periodo
                    customerAdeguatezzaILP = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerAdeguatezzaILP["Descrizione"] = "Investimento di Lungo Periodo"; ;
                    customerAdeguatezzaILP["Valore"] = $"{customerProfiloRischioDataTable.Rows[0]["inv_l_p"]} %";
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerAdeguatezzaILP);

                    #endregion



                    // MIFID2 20180405
                    // Descrizione grado esperienza

                    customerProfiloRischioDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.NewRow();
                    customerProfiloRischioDataRow["Descrizione"] = "Livello di esperienza e conoscenza in materia di investimenti";
                    customerProfiloRischioDataRow["Valore"] = customerProfiloRischioDataTable.Rows[0]["descrizioneEsperienza"].ToString();
                    customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Add(customerProfiloRischioDataRow);
                    //--MIFID2 20180405

                    #endregion
                    #region Creazione righe datatable ProfiloDate
                    // Data Ultimo Aggiornamento
                    customerProfiloDateDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloDate.NewRow();
                    // FC 09062015
                    // Aggiornamneto Label Descrizione 
                    //New Label Da inserire  :
                    customerProfiloDateDataRow["Descrizione"] = "Data di inizio validità del profilo";
                    //customerProfiloDateDataRow["Descrizione"] = "Ultimo aggiornamento questionario";
                    customerProfiloDateDataRow["Valore"] = string.IsNullOrEmpty(customerProfiloRischioDataTable.Rows[0]["dataInizioValidita"].ToString()) ? "-" : customerProfiloRischioDataTable.Rows[0]["dataInizioValidita"].ToString();

                    customerProfiloRischioRecordsDataSetS151.ProfiloDate.Rows.Add(customerProfiloDateDataRow);

                    // MIFID2 20180405
                    //dataScadenzaProfilo
                    customerProfiloDateDataRow = customerProfiloRischioRecordsDataSetS151.ProfiloDate.NewRow();
                    customerProfiloDateDataRow["Descrizione"] = "Data di scadenza del profilo";
                    customerProfiloDateDataRow["Valore"] = string.IsNullOrEmpty(customerProfiloRischioDataTable.Rows[0]["dataScadenzaProfilo"].ToString()) ? "-" : customerProfiloRischioDataTable.Rows[0]["dataScadenzaProfilo"].ToString();
                    //--MIFID2 20180405

                    customerProfiloRischioRecordsDataSetS151.ProfiloDate.Rows.Add(customerProfiloDateDataRow);

                    #endregion

                    #region RISORSE FINANZIARIE

                    decimal PatrimonioBancaFideuramCTV_netto = dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente;

                    CTVDataRow = customerProfiloRischioRecordsDataSetS151.Dati.NewRow();
                    CTVDataRow["Descrizione"] = "Risorse finanziarie";
                    risorseFinanziarie = PatrimonioBancaFideuramCTV_netto;
                    CTVDataRow["Controvalore"] = formatnum.ConvertNum(PatrimonioBancaFideuramCTV_netto) + " €";
                    customerProfiloRischioRecordsDataSetS151.Dati.Rows.Add(CTVDataRow);
                    #endregion

                    #region CONTO CORRENTE
                    CTVDataRow = customerProfiloRischioRecordsDataSetS151.Dati.NewRow();
                    //CTVDataRow["Descrizione"] = "Conto corrente";
                    CTVDataRow["Descrizione"] = "C/C a saldo negativo";
                    CC = dataThread.ContoCorrente;
                    CTVDataRow["Controvalore"] = formatnum.ConvertNum(CC) + " €";
                    if (CC != 0)
                        customerProfiloRischioRecordsDataSetS151.Dati.Rows.Add(CTVDataRow);
                    #endregion

                    #region INVESTIMENTI IN CORSO
                    CTVDataRow = customerProfiloRischioRecordsDataSetS151.Dati.NewDatiRow();
                    CTVDataRow["Descrizione"] = Resource.PartiteViaggianti;
                    investimentiInCorso = dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento;
                    CTVDataRow["Controvalore"] = formatnum.ConvertNum(investimentiInCorso) + " €";
                    if (totalePartiteViaggianti != 0)
                        customerProfiloRischioRecordsDataSetS151.Dati.Rows.Add(CTVDataRow);
                    #endregion

                    #region TOTALE PATRIMONIO
                    CTVTotali = customerProfiloRischioRecordsDataSetS151.Totali.NewRow();
                    CTVTotali["Descrizione"] = "Totale patrimonio";
                    decimal totale = risorseFinanziarie + CC + investimentiInCorso;
                    CTVTotali["Controvalore"] = formatnum.ConvertNum(totale) + " €";
                    customerProfiloRischioRecordsDataSetS151.Totali.Rows.Add(CTVTotali);
                    #endregion

                }

                customerProfiloRischioDataSectionResult.DatiSezione = customerProfiloRischioRecordsDataSetS151;

                customerProfiloRischioDataSectionResult.Esito = customerProfiloRischioRecordsDataSetS151.ProfiloRischio.Rows.Count;
                return customerProfiloRischioDataSectionResult;
            }
            catch (Exception ex)
            {
                try
                {
                    logger.Error(String.Concat(ex.Message, " ", dataThread.CodiceFiscale));
                }
                catch { }
                throw ex;
            }
        }

        #endregion
    }
}