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 System.Text;
using PDFGenerator.BusinessLayer.DataSection;
using System.Collections.Generic;
using PDFGenerator.BusinessLayer;
using PDFGenerator;
/// <summary>
/// Summary description for DSS32PatrimonioFinanziario
/// </summary>
public class DSS163PatrimonioFinanziario : IDataSection
{
    #region IDataSection Members
    public StructColor toBeFound = new StructColor();

    public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
    {
        DataSectionResult dsr = new DataSectionResult();

        Decimal ctvTotale = 0;

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

        DataSetS163 ds163 = new DataSetS163();
        DataRow dr;

        toBeFound.Tipo = "MacroAssetClass";



        ds163.MacroAssetClass.Columns["Controvalore"].Caption = "Controvalore <br> € &nbsp; &nbsp; &nbsp; &nbsp;";
        ds163.MacroAssetClass.Columns["Percentuale"].Caption = "Peso <br> (%)";
        //V
        decimal patrimonioTerziCTV = Convert.ToDecimal(dataThread.Patrimonioterzictv);
        patrimonioTerziCTV = decimal.Round(patrimonioTerziCTV, 2);
        decimal residuoCTV = patrimonioTerziCTV;
        DataRow tuplaCTVMaggiore = null;
        decimal valoreCTVMaggiore = 0;
        //
        foreach (DataRow row in dt.Rows)
        {
            dr = ds163.MacroAssetClass.NewRow();
            dr["Descrizione"] = row["assetclassname"];
            //V
            decimal temp = Convert.ToDecimal(row["Controvalore"]);
            temp = decimal.Round(temp, 2);
            if (temp > valoreCTVMaggiore)
            {
                valoreCTVMaggiore = temp;
                tuplaCTVMaggiore = dr;
            }
            residuoCTV -= temp;
            //

            dr["Controvalore"] = temp.ToString();
            dr["Percentuale"] = row["Percentuale"];

            if (patrimonioTerziCTV == 0)
                patrimonioTerziCTV = dataThread.Patrimonioterzictv;

            toBeFound.Codice = row["AssetClassId"].ToString().Trim();
            int foundIdx = ClsFunzioniGeneriche.findRightColors(toBeFound);

            dr["Red"] = ClsFunzioniGeneriche.Colors[foundIdx].Red;
            dr["Green"] = ClsFunzioniGeneriche.Colors[foundIdx].Green;
            dr["Blue"] = ClsFunzioniGeneriche.Colors[foundIdx].Blue;

            ctvTotale += temp;
            ds163.MacroAssetClass.Rows.Add(dr);
        }

        //V
        if (residuoCTV != 0 && tuplaCTVMaggiore != null)
        {
            decimal temp = Convert.ToDecimal(tuplaCTVMaggiore["Controvalore"]);
            temp += residuoCTV;
            ctvTotale += residuoCTV;
            tuplaCTVMaggiore["Controvalore"] = temp.ToString();
        }
        //     
        DataSetS163.PatrimonioNonRappresentabileRow drPatrimonioNonRappresentabile;
        drPatrimonioNonRappresentabile = ds163.PatrimonioNonRappresentabile.NewPatrimonioNonRappresentabileRow();
        drPatrimonioNonRappresentabile.Descrizione = Resource.PatrimonioNonRappresentabile_label;
        //"Patrimonio non rappresentabile";


        //Arrotondamento i totali vengono presi da query differenti e avolte c'è la differenza di un decimale
        Decimal ctvNonRappr = patrimonioTerziCTV - ctvTotale;
        if (ctvNonRappr <= 0)
        {
            drPatrimonioNonRappresentabile.ControValore = 0;
            ctvNonRappr = 0;
        }
        else
        {
            drPatrimonioNonRappresentabile.ControValore = ctvNonRappr;
            ds163.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);
        }

        //drPatrimonioNonRappresentabile.ControValore = dataSectionParameter.PatrimonioBancaFideuramCTV - ctvTotale;
        //if (drPatrimonioNonRappresentabile.ControValore != 0)
        //    ds32.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);

        //Se non vi è Patrimonio non rappresentabile, 
        //le righe “Patrimonio non rappresentabile” e “Totale” non devono essere riportate. 
        //Sostituire la voce Totale rappresentato con Totale
        if (ctvNonRappr != 0)
        {
            DataSetS163.TotaleRow drTotale;
            drTotale = ds163.Totale.NewTotaleRow();
            drTotale.Descrizione = "TOTALE";
            drTotale.ControValore = (ctvTotale + drPatrimonioNonRappresentabile.ControValore);
            ds163.Totale.Rows.Add(drTotale);
        }


        DataRow drTot = ds163.MacroAssetTotale.NewRow();
        if (drPatrimonioNonRappresentabile.ControValore > 0)
            drTot["Totale"] = "TOTALE RAPPRESENTATO";
        else
            drTot["Totale"] = "TOTALE ";

        drTot["Controvalore"] = ctvTotale;
        drTot["Percentuale"] = 100.00;
        ds163.MacroAssetTotale.Rows.Add(drTot);




        //Normalizzo le percentuali
        FormatNum conv = new FormatNum();
        if (dt.Rows.Count != 0)
            conv.NormalizePerc(ds163.MacroAssetClass, "Percentuale");
        //========================================================

        dsr.DatiSezione = ds163;
        dsr.Esito = dt.Rows.Count;

        return dsr;
    }

    #endregion
}