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

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

        DataSetS154 ds154 = new DataSetS154();
        DataRow dr;

        toBeFound.Tipo = "MacroAssetClass";
        ds154.MacroAssetClass.Columns["Controvalore"].Caption = "Controvalore <br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";
        ds154.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 = ds154.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"];
                ds154.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;

            DataSetS154.PatrimonioNonRappresentabileRow drPatrimonioNonRappresentabile;
            drPatrimonioNonRappresentabile = ds154.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;
                ds154.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);
            }
            //if (System.Math.Abs(ctvNonRappr) < 1)
            //{
            //    drPatrimonioNonRappresentabile.ControValore = 0;
            //    ctvNonRappr = 0;
            //}
            //else
            //{
            //    drPatrimonioNonRappresentabile.ControValore = ctvNonRappr;
            //    ds2.PatrimonioNonRappresentabile.Rows.Add(drPatrimonioNonRappresentabile);
            //}

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

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


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

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


            if ( dataThread.TotalSelfNegCurrentAccountValue!=0){
                drContoCorrenteBancaFideuramRow = ds154.ContoCorrenteBancaFideuram.NewContoCorrenteBancaFideuramRow();
                drContoCorrenteBancaFideuramRow.Descrizione = "Liquidità negativa \"Linee GP Eligo\"";
                drContoCorrenteBancaFideuramRow.ControValore = dataThread.TotalSelfNegCurrentAccountValue;
                ds154.ContoCorrenteBancaFideuram.Rows.Add(drContoCorrenteBancaFideuramRow); 
            }
                


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


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


            if (totalePresente)
            {
                DataSetS154.TotaleRow drTotale;
                drTotale = ds154.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
                ds154.Totale.Rows.Add(drTotale);
            }

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

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

            #endregion





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

        dsr.DatiSezione = ds154;
        dsr.Esito = dt.Rows.Count;
        if (ctvTotale == 0)
            dsr.Esito = 0;

        return dsr;
    }

    #endregion
}