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 DSS143DistibuzionePerEmittente: IDataSection
    {
        public DSS143DistibuzionePerEmittente()
        {
        }

        #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);

                DataSetS143 ds143 = new DataSetS143();
                decimal Totale = 0;
                decimal TotPerc = 100;
                
                
                
                decimal TotaleRap = 0;
                decimal TotPercRap = 0;


                for (int r = 0; r < dt.Rows.Count; r++)   {
                    DataRow drEmit = ds143.Emittente.NewRow();
                    drEmit["TipoEmittente"] = dt.Rows[r]["TipoEmittente"].ToString();
                    drEmit["Red"] = dt.Rows[r]["Red"];
                    drEmit["Green"] = dt.Rows[r]["Green"];
                    drEmit["Blue"] = dt.Rows[r]["Blue"];
                    drEmit["Controvalore"] = num.ConvertNum(dt.Rows[r]["Controvalore"]);
                    Totale += decimal.Parse(drEmit["Controvalore"].ToString());                    
                    drEmit["Percentage"] = Decimal.Round(Convert.ToDecimal(dt.Rows[r]["Percentage"]), 2, MidpointRounding.AwayFromZero);
                    TotPerc -= decimal.Parse(drEmit["Percentage"].ToString()); 
                    ds143.Emittente.Rows.Add(drEmit);                    
                }

                if (ds143.Emittente.Rows.Count > 0)
                     ds143.Emittente.Rows[0]["Percentage"] = decimal.Parse(ds143.Emittente.Rows[0]["Percentage"].ToString()) + TotPerc;


                foreach (DataSetS143.EmittenteRow Emit in ds143.Emittente.Rows)
                {

                    if (Emit["TipoEmittente"].Equals(""))
                    {
                        DataRow drNoRap = ds143.EmittenteNoRap.NewRow();
                        drNoRap["TipoEmittente"] = "Patrimonio non rappresentabile";
                        drNoRap["Controvalore"] = Emit["Controvalore"];
                       // drNoRap["Percentage"] = Emit["Percentage"];
                        drNoRap["Red"] = Emit["Red"];
                        drNoRap["Green"] = Emit["Green"];
                        drNoRap["Blue"] = Emit["Blue"];
                        ds143.EmittenteNoRap.Rows.Add(drNoRap);
                    }
                    else
                    {
                        DataRow drRap = ds143.EmittenteRap.NewRow();
                        drRap["TipoEmittente"] = Emit["TipoEmittente"];
                        drRap["Controvalore"] = Emit["Controvalore"];
                        drRap["Percentage"] = Emit["Percentage"];
                        drRap["Red"] = Emit["Red"];
                        drRap["Green"] = Emit["Green"];
                        drRap["Blue"] = Emit["Blue"];
                        TotaleRap += decimal.Parse(Emit["Controvalore"].ToString());
                        TotPercRap += decimal.Parse(Emit["Percentage"].ToString());
                        ds143.EmittenteRap.Rows.Add(drRap);
                    }

                
                }

                // toDo FC 17/12/2013 Modifica Descrizione secondo richiesta di Scirocco Flavia
                //drtotRap["Descrizione"] = "Patrimonio rappresentato";

                DataRow drtotRap = ds143.TotaleRap.NewRow();
                drtotRap["Descrizione"] = "Totale rappresentato";              
                drtotRap["Controvalore"] = num.ConvertNum(TotaleRap);
                drtotRap["Percentage"] = num.ConvertNum(TotPercRap);
                ds143.TotaleRap.Rows.Add(drtotRap);

                //drtot["Descrizione"] = "Patrimonio Amministrato";
                DataRow drtot = ds143.Totale.NewRow();
                drtot["Descrizione"] = "Totale";
                drtot["Controvalore"] = num.ConvertNum(Totale);
                drtot["Percentage"] = num.ConvertNum(100);
                ds143.Totale.Rows.Add(drtot);



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