using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PDFGenerator.BusinessLayer.DataSection
{
    public class DSS332FviCS80 : 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);

            DataSetS332 dS332 = new DataSetS332();
            DataRow dr;

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

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

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

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


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

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

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


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


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

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

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

                #endregion





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

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

            return dsr;
        }

        #endregion
    }
}