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

public class DSS2PatrimonioFinanziario : 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);

        DataSetS2 ds2 = new DataSetS2();
        DataRow dr;

        toBeFound.Tipo = "MacroAssetClass";
        ds2.MacroAssetClass.Columns["Controvalore"].Caption = "Controvalore <br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";
        ds2.MacroAssetClass.Columns["Percentuale"].Caption = "Peso <br> (%)";
        
        decimal patrimonioFinanziarioCTV = 0;
        decimal totalePartiteViaggianti = 0;
        decimal cc = 0;
        string banca = string.Empty;

        //V
        decimal residuo = 100;
        decimal valoreMaggiore = 0;
        DataRow tuplaMaggiore = null;
        decimal residuoControvalore = dataThread.TotaleS6 - ( dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento) - dataThread.PatrimonioNonRappresentabile;
        
        //if (dataThread.ContoCorrente < 0)
        //    residuoControvalore -= dataThread.ContoCorrente;
        
        DataRow tuplaMaggioreControvalore = null;
        decimal valereMaggioreControvalore = 0;
        
        //

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                dr = ds2.MacroAssetClass.NewRow();
                dr["Descrizione"] = row["assetclassname"];
                //V
                decimal temp = decimal.Round(Convert.ToDecimal(row["Controvalore"]),2);
                //dr["Controvalore"] = row["Controvalore"];
                dr["Controvalore"] = temp.ToString();

                if (temp > valereMaggioreControvalore)
                {
                    valereMaggioreControvalore = temp;
                    tuplaMaggioreControvalore = dr;
                }
                residuoControvalore -= temp;


                decimal tempPerc = Convert.ToDecimal(row["Percentuale"]);
                tempPerc = decimal.Round(tempPerc, 2);
                if (tempPerc > valoreMaggiore)
                {
                    valoreMaggiore = tempPerc;
                    tuplaMaggiore = dr;
                }
                residuo -= tempPerc;
                
                dr["Percentuale"] = tempPerc.ToString();
                //
                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;
                //V
                ctvTotale += temp; //(Decimal)row["Controvalore"];
                ds2.MacroAssetClass.Rows.Add(dr);
                if (patrimonioFinanziarioCTV == 0)
                    patrimonioFinanziarioCTV = (decimal)row["patrimonioFinanziarioCTV"];
                if (totalePartiteViaggianti == 0)
                    totalePartiteViaggianti = dataThread.PartiteViaggiantiInvestimento+ dataThread.PartiteViaggiantiDisinvestimento;
                if (cc == 0)
                    cc = dataThread.ContoCorrente;
                if (banca == string.Empty)
                    banca = (string)row["banca"];
            }
            //V
            if (residuo != 0 && tuplaMaggiore != null)
            {
                decimal temp = Convert.ToDecimal(tuplaMaggiore["Percentuale"]);
                temp += residuo;
                tuplaMaggiore["Percentuale"] = temp.ToString();
            }

            if (residuoControvalore != 0 && tuplaMaggioreControvalore != null)
            {
                decimal temp = Convert.ToDecimal(tuplaMaggioreControvalore["Controvalore"]);
                temp += residuoControvalore;
                tuplaMaggioreControvalore["Controvalore"] = temp.ToString();
                ctvTotale += residuoControvalore;
            }

            //

            bool totalePresente = true;

            DataSetS2.PatrimonioNonRappresentabileRow drPatrimonioNonRappresentabile;
            drPatrimonioNonRappresentabile = ds2.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 = dataThread.PatrimonioNonRappresentabile;

            //Se il c/c BF è negativo il Patrimonio non rappresentabile è calcolato al netto del c/c.
            //M
            //if (dataThread.ContoCorrente < 0)
            //    ctvNonRappr += dataThread.ContoCorrente;
            //
            
            
            //V 13/07/2007 Problema dei 13 centesimi
            if (ctvNonRappr <= 0)
            {
                drPatrimonioNonRappresentabile.ControValore = 0;
                ctvNonRappr = 0;
            }
            else
            {
                drPatrimonioNonRappresentabile.ControValore = ctvNonRappr;
                ds2.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);
            }
            //if (System.Math.Abs(ctvNonRappr) < 1)
            //{
            //    drPatrimonioNonRappresentabile.ControValore = 0;
            //    ctvNonRappr = 0;
            //}
            //else
            //{
            //    drPatrimonioNonRappresentabile.ControValore = ctvNonRappr;
            //    ds2.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);
            //}

            DataSetS2.PartiteViaggiantiRow drPartiteViaggiantiRow;
            drPartiteViaggiantiRow = ds2.PartiteViaggianti.NewPartiteViaggiantiRow();
            drPartiteViaggiantiRow.Descrizione = Resource.PartiteViaggianti;
            //"Investimenti in corso";
            drPartiteViaggiantiRow.ControValore = totalePartiteViaggianti;

            if (drPartiteViaggiantiRow.ControValore > 0)
                ds2.PartiteViaggianti.Rows.Add(drPartiteViaggiantiRow);


            DataSetS2.ContoCorrenteBancaFideuramRow drContoCorrenteBancaFideuramRow;
            drContoCorrenteBancaFideuramRow = ds2.ContoCorrenteBancaFideuram.NewContoCorrenteBancaFideuramRow();
            // MZ:
            drContoCorrenteBancaFideuramRow.Descrizione = "C/C a saldo negativo";// + banca;
            drContoCorrenteBancaFideuramRow.ControValore = cc;

            if (drContoCorrenteBancaFideuramRow.ControValore < 0)
                ds2.ContoCorrenteBancaFideuram.Rows.Add(drContoCorrenteBancaFideuramRow);

            string totRappr = "TOTALE RAPPRESENTATO";
            if (drContoCorrenteBancaFideuramRow.ControValore >= 0 && drPartiteViaggiantiRow.ControValore <= 0 && ctvNonRappr == 0)
            {
                totalePresente = false;
                totRappr = "TOTALE";
            }


            DataRow drTot = ds2.MacroAssetTotale.NewRow();
            drTot["Totale"] = totRappr;
            drTot["Controvalore"] = ctvTotale;
            drTot["Percentuale"] = 100.00;
            ds2.MacroAssetTotale.Rows.Add(drTot);


            if (totalePresente)
            {
                DataSetS2.TotaleRow drTotale;
                drTotale = ds2.Totale.NewTotaleRow();
                drTotale.Descrizione = "TOTALE";
                //modifica luca 11/09/08
                //i dati in caso di cc positivo sono già comprensivi del c/c quindi non va sommato di nuovo
                //drTotale.ControValore = (ctvTotale + drPatrimonioNonRappresentabile.ControValore + drContoCorrenteBancaFideuramRow.ControValore);
                drTotale.ControValore = (ctvTotale + ctvNonRappr) + totalePartiteViaggianti;
                if (drContoCorrenteBancaFideuramRow.ControValore < 0)
                    drTotale.ControValore += drContoCorrenteBancaFideuramRow.ControValore;
                //fine modifica luca 11/09/08
                ds2.Totale.Rows.Add(drTotale);
            }

            #region Risorse Percentuali
            Decimal valorePrecentuale = 0;
            DataSetS2.RisorsePercentualiRow rowRisorsePercentuali = ds2.RisorsePercentuali.NewRisorsePercentualiRow();

            if (dataThread.Patrimoniofinanziarioctv == 0)
                rowRisorsePercentuali.ValorePercentuale = 0;
            else
            {
                valorePrecentuale = (ctvTotale / patrimonioFinanziarioCTV) * 100;
                rowRisorsePercentuali.ValorePercentuale = Math.Round(valorePrecentuale, 2, MidpointRounding.ToEven);
            }
            ds2.RisorsePercentuali.AddRisorsePercentualiRow(rowRisorsePercentuali);

            #endregion





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

        dsr.DatiSezione = ds2;
        dsr.Esito = dt.Rows.Count;
        if (ctvTotale == 0)
            dsr.Esito = 0;
      
        return dsr;
    }

    #endregion
}