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 DSS32PatrimonioFinanziario : 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);

            DataSetS32 ds32 = new DataSetS32();
            DataRow dr;

            toBeFound.Tipo = "MacroAssetClass";
            

            
            ds32.MacroAssetClass.Columns["Controvalore"].Caption = "Controvalore <br> � &nbsp; &nbsp; &nbsp; &nbsp;";
            ds32.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 = ds32.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;
                ds32.MacroAssetClass.Rows.Add(dr);
            }

            //V
            if (residuoCTV != 0 && tuplaCTVMaggiore != null)
            {
                decimal temp = Convert.ToDecimal(tuplaCTVMaggiore["Controvalore"]);
                temp += residuoCTV;
                ctvTotale += residuoCTV;
                tuplaCTVMaggiore["Controvalore"] = temp.ToString();
            }
            //     
            DataSetS32.PatrimonioNonRappresentabileRow drPatrimonioNonRappresentabile;
            drPatrimonioNonRappresentabile = ds32.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;
                ds32.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)
            {
                DataSetS32.TotaleRow drTotale;
                drTotale = ds32.Totale.NewTotaleRow();
                drTotale.Descrizione = "TOTALE";
                drTotale.ControValore = (ctvTotale + drPatrimonioNonRappresentabile.ControValore);
                ds32.Totale.Rows.Add(drTotale);
            }
          

             DataRow drTot = ds32.MacroAssetTotale.NewRow();
             if( drPatrimonioNonRappresentabile.ControValore > 0)
                 drTot["Totale"] = "TOTALE RAPPRESENTATO";
             else
                 drTot["Totale"] = "TOTALE ";
                   
            drTot["Controvalore"] = ctvTotale;
            drTot["Percentuale"] = 100.00;
            ds32.MacroAssetTotale.Rows.Add(drTot);
        
          


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

            dsr.DatiSezione = ds32;
            dsr.Esito = dt.Rows.Count;
        
       return dsr;
    }

    #endregion
}