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

/// <summary>
/// Summary description for DSS6PatrimonioFinanziario
/// </summary>
public class DSS153PatrimonioFinanziario : 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);

        DataSetS153 ds153 = new DataSetS153();
        DataRow dr;
        if (dataThread.TotalNegativeCurrentAccountValue != 0)
        {
            ds153.Intermediario.Columns["Controvalore"].Caption = "Controvalore *<br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";
        }
        else
        {
            ds153.Intermediario.Columns["Controvalore"].Caption = "Controvalore <br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";
        }
        //ds6.Intermediario.Columns["Controvalore"].Caption = "Controvalore <br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";
        ds153.Intermediario.Columns["Percentuale"].Caption = "Peso <br> (%)  &nbsp;";

        toBeFound.Tipo = "Intermediario";
        int i = 1;
        string banca = string.Empty;
        decimal cc = 0;
        foreach (DataRow row in dt.Rows)
        {
            dr = ds153.Intermediario.NewRow();
            dr["Descrizione"] = row["Intermediario"];
            dr["Controvalore"] = row["Controvalore"];
            dr["Percentuale"] = row["Percentuale"];

            if (banca == string.Empty)
            {
                banca = (string)row["Banca"];
                //V
                dr["Controvalore"] = (Convert.ToDecimal(dr["Controvalore"]) + dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento).ToString();
            }
            if (cc == 0)
                cc = dataThread.ContoCorrente;

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

            if (foundIdx == 0) foundIdx = i;
            dr["Red"] = ClsFunzioniGeneriche.Colors[foundIdx].Red;
            dr["Green"] = ClsFunzioniGeneriche.Colors[foundIdx].Green;
            dr["Blue"] = ClsFunzioniGeneriche.Colors[foundIdx].Blue;

            ctvTotale += Convert.ToDecimal(dr["Controvalore"]);
            ds153.Intermediario.Rows.Add(dr);
            i += 2;
        }

        //V Ricalcolo delle percentuali
        foreach (DataRow row in ds153.Intermediario.Rows)
        {
            decimal temp = Convert.ToDecimal(row["Controvalore"]);
            decimal perc = temp / ctvTotale;
            perc = decimal.Round(perc, 4);
            perc *= 100;
            row["Percentuale"] = perc.ToString();
        }

        //V Quadratura a 100 delle percentuali
        decimal residuo = 100M;
        string descrizioneMaggiore = null;
        decimal percMaggiore = 0;
        foreach (DataRow row in ds153.Intermediario.Rows)
        {
            decimal temp = Convert.ToDecimal(row["Percentuale"]);
            if (temp > percMaggiore)
            {
                percMaggiore = temp;
                descrizioneMaggiore = row["Descrizione"].ToString();
            }
            residuo -= temp;

        }

        if (residuo != 0)
        {
            foreach (DataRow row in ds153.Intermediario.Rows)
            {
                if (row["Descrizione"].ToString() == descrizioneMaggiore)
                {
                    decimal temp = Convert.ToDecimal(row["Percentuale"]);
                    temp += residuo;
                    row["Percentuale"] = temp.ToString();
                }
            }
        }

        //DataSetS6.ContoCorrenteBancaFideuramRow drContoCorrenteBancaFideuramRow;
        //drContoCorrenteBancaFideuramRow = ds6.ContoCorrenteBancaFideuram.NewContoCorrenteBancaFideuramRow();
        //drContoCorrenteBancaFideuramRow.Descrizione = "c/c " + banca;
        //drContoCorrenteBancaFideuramRow.ControValore = cc;//(decimal)dataSectionParameter.DatiComuni.ContoCorrente.Rows[0][0];

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


        DataRow drTot = ds153.IntermediarioTotale.NewRow();

        //if(drContoCorrenteBancaFideuramRow.ControValore<0)
        //    drTot["Totale"] = "TOTALE RAPPRESENTATO";
        //else
        drTot["Totale"] = "TOTALE";

        //V            
        drTot["Controvalore"] = ctvTotale;
        dataThread.TotaleS6 = ctvTotale;
        //
        if (ctvTotale >= 0)
            drTot["Percentuale"] = 100.00;
        else
            drTot["Percentuale"] = 0.00;
        ds153.IntermediarioTotale.Rows.Add(drTot);


        //DataSetS6.TotaleRow drTotale;
        //drTotale = ds6.Totale.NewTotaleRow();
        //drTotale.Descrizione = "TOTALE";
        //drTotale.ControValore = (ctvTotale + drContoCorrenteBancaFideuramRow.ControValore);
        //drTotale.ControValore = ctvTotale;

        // Aggiungo la riga di totale solo se il la riga del cc deve essere stampata. (cc<0)
        //if(drContoCorrenteBancaFideuramRow.ControValore<0)
        //    ds6.Totale.Rows.Add(drTotale);

        //Normalizzo le percentuali
        if (ds153.Intermediario.Rows.Count > 0)
        {
            FormatNum conv = new FormatNum();
            conv.NormalizePerc(ds153.Intermediario, "Percentuale");
            dataThread.DtDSS6Intermediari = ds153.Intermediario;
        }
        else
        {
            dataThread.DtDSS6Intermediari = new DataSetS6.IntermediarioDataTable();
        }
        //========================================================

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

        return dsr;
    }

    #endregion
}