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

namespace PDFGenerator.BusinessLayer.DataSection
{
    class DSS142DistibuzionePerRating: IDataSection
    {
        public DSS142DistibuzionePerRating()
        {
        }

        #region IDataSection Members
        public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
        {

            try
            {
                FormatNum num = new FormatNum();

                DataSectionResult dsr = new DataSectionResult();

                DataTable dt = SectionManager.GetDataSection(tabelleSessione, querySql, dataThread);

                DataSetS142 ds142 = new DataSetS142();
                decimal Totale = 0;
                decimal TotPerc = 100;
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    DataRow dr = ds142.Rating.NewRow();
                    dr["RATING"] = dt.Rows[r]["RATING"].ToString();
                    dr["CONTROVALORE"] = num.ConvertNum(dt.Rows[r]["CONTROVALORE"]);
                    Totale += decimal.Parse(dr["CONTROVALORE"].ToString());
                    dr["Percentage"] = Decimal.Round(Convert.ToDecimal(dt.Rows[r]["Percentage"]), 2, MidpointRounding.AwayFromZero);
                    TotPerc -= decimal.Parse(dr["Percentage"].ToString());
                    dr["Red"] = dt.Rows[r]["Red"];
                    dr["Green"] = dt.Rows[r]["Green"];
                    dr["Blue"] = dt.Rows[r]["Blue"];
                    ds142.Rating.Rows.Add(dr);
                }
                if (ds142.Rating.Rows.Count > 0)
                    ds142.Rating.Rows[dt.Rows.Count - 1]["Percentage"] = decimal.Parse(ds142.Rating.Rows[dt.Rows.Count - 1]["Percentage"].ToString()) + TotPerc;

                DataRow drtot = ds142.Totale.NewRow();
                // toDo FC 05/12/2013 Modifica Descrizione secondo richiesta di Scirocco Flavia
                //drtot["Descrizione"] = "Patrimonio Amministrato";
                drtot["Descrizione"] = "Totale";
                drtot["Controvalore"] =   num.ConvertNum(Totale);
                drtot["Percentage"] = num.ConvertNum(100);
                ds142.Totale.Rows.Add(drtot);

                dsr.DatiSezione = ds142;
                dsr.Esito = ds142.Rating.Rows.Count;
                return dsr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}