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 PDFGenerator.BusinessLayer;
using System.Collections.Generic;
using PDFGenerator;

/// <summary>
/// Summary description for DSS31PatrimonioFinanziario
/// </summary>
public class DSS31PatrimonioFinanziario : IDataSection
{
    #region IDataSection Members
    public StructColor toBeFound = new StructColor();

    public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
    {
        DataSectionResult dsr = new DataSectionResult();
      
        
            //Utils. cORAConn = new Utils.(Utils..enum_DB.OracleDB);
            Decimal ctvTotale = 0;

            DataTable dt = SectionManager.GetDataSection(tabelleSessione, querySql, dataThread);
            DataSetS31 ds31 = new DataSetS31();
            DataRow dr;

            toBeFound.Tipo = "Intermediario";
            ds31.Intermediario.Columns["Controvalore"].Caption = "Controvalore <br> (�) &nbsp; &nbsp; &nbsp; &nbsp;";
            ds31.Intermediario.Columns["Percentuale"].Caption = "Peso <br> (%)";
            int i = 1;
            //V
            decimal residuo = 100;
            DataRow tuplaMaggiore = null;
            decimal percentualeMaggiore = 0;
            //

            foreach (DataRow row in dt.Rows)
            {
                dr = ds31.Intermediario.NewRow();
                dr["Descrizione"] = row["Intermediario"];
                //V
                decimal temp = Convert.ToDecimal(row["Controvalore"]);
                temp = decimal.Round(temp, 2);
                //dr["Controvalore"] = row["Controvalore"];
                dr["Controvalore"] = temp.ToString();
                decimal tempPerc = Convert.ToDecimal(row["Percentuale"]);
                tempPerc = decimal.Round(tempPerc, 2);
                dr["Percentuale"] = tempPerc;
                if (tempPerc > percentualeMaggiore)
                {
                    tuplaMaggiore = dr;
                    percentualeMaggiore = tempPerc;
                }
                residuo -= tempPerc;
                //
                

                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 += temp; //V(Decimal)row["Controvalore"];
                ds31.Intermediario.Rows.Add(dr);
                i += 2;
            }
            //V
            if (residuo != 0 && tuplaMaggiore != null)
            {
                decimal temp = Convert.ToDecimal(tuplaMaggiore["Percentuale"]) + residuo;
                tuplaMaggiore["Percentuale"] = temp.ToString();
            }
            //

            DataRow drTot = ds31.IntermediarioTotale.NewRow();
            drTot["Totale"] = "TOTALE";
            drTot["Controvalore"] = ctvTotale; 
            drTot["Percentuale"] = 100.00;
            ds31.IntermediarioTotale.Rows.Add(drTot);

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

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

    #endregion
}