using System;
using System.Collections.Generic;
using System.Text;
using ceTe.DynamicPDF.Text;
using System.Collections;
using ceTe.DynamicPDF;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer.DataSection;
using System.Data;
using PDFGenerator.BusinessLayer;
using ceTe.DynamicPDF.PageElements;

namespace PDFGenerator.Presentation.Section
{
    class S142 : SIstogramma, ISezione
    {
        public void writeSezione(DataThread dataThread)
        {
            DocumentPDF document = dataThread.DocumentPDF;            
            DataSetS142 set = (DataSetS142)dataThread.Data.DatiSezione;
            DatiTabella datitab = new DatiTabella(set.Rating);

            float Xtable = document.getMargineLeft();
            float Ytable = document.getLastPos();
            int dFont = 9;
            int wCol1 = 100;
            int wCol2 = 95;
            int wCol3 = 65;
            int hGrafico = 190; //150;

            
            if (document.checkMargin(250))         
                            document.addPage();
              
            

            document.setSezTitolo(dataThread.SezioneReport.Titolo);
            document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo.Replace("/$Banca$/", dataThread.NomeRete).Replace("Sanpaolo Invest", "Fideuram S.p.A., commercializzati tramite la rete di private banker Sanpaolo Invest"), 0, 520, 8);

            Ytable = document.getLastPos();

            #region  GRAFICO
            //DatiGrafico dati = new DatiGrafico();
            //dati.dataTab = set.Rating;
            //dati.setHeight(hGrafico);
            //dati.setWidth(hGrafico);
            //Torta isto = new Torta();
            //document.InsertGrafico(isto.getGrafico(dati), Xtable + wCol1 + wCol2 + wCol3 + 50, Ytable - 11);

            /************************ Modifiche per Aladdin grafici a barre -- Pino**********************/
            DatiGrafico dati = new DatiGrafico();
            dati.dataTab = set.Rating;
            //dati.setHeight(Convert.ToSingle(datitab.getHeaderDim()) + (datitab.GetRowDim()));
            dati.setHeight(Convert.ToSingle(datitab.getHeaderDim()) + (datitab.GetRowDim() * (datitab.getNumRow() + 1) + 5)); //Modifica Andrea
            dati.setWidth(hGrafico);
            Istogramma isto = new Istogramma();
            document.InsertGrafico(isto.getGrafico(dati), 320, (Ytable - (datitab.getHeaderDim() / 2)));

            // Barra suil grafico
            document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(320, (document.getLastPos() + (datitab.GetRowDim() * (datitab.getNumRow()) + (datitab.getHeaderDim() / 2)) + 2), 199, 23, 0, new RgbColor(232, 236, 237)));

            // Scrittura del footer sul grafico
            FormatNum format = new FormatNum();
            ArrayList Labels = format.CreateCustomPerc148(dati.getValMax(),dati.getValMin());
            float passo = 40.0F;
            for (int ii = 0; ii <= Labels.Count - 1; ++ii)
                if (ii == 0)
                    document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Label(Labels[ii].ToString(), 235F + ii, (document.getLastPos() + (datitab.GetRowDim() * (datitab.getNumRow()) + (datitab.getHeaderDim() / 2)) + 5), 100, 10, Globals.OpenTypeFontVerdana, 8, ceTe.DynamicPDF.TextAlign.Right, ceTe.DynamicPDF.CmykColor.Black));
                else
                    document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Label(Labels[ii].ToString(), 245F + (passo * ii), (document.getLastPos() + (datitab.GetRowDim() * (datitab.getNumRow()) + (datitab.getHeaderDim() / 2)) + 5), 100, 10, Globals.OpenTypeFontVerdana, 8, ceTe.DynamicPDF.TextAlign.Right, ceTe.DynamicPDF.CmykColor.Black));

            /********************************************************************************************/

            #endregion

            #region Tabella Rating            


            Tabella tabellaDati = new Tabella(Xtable, Ytable);
            tabellaDati.Header = true;
            tabellaDati.Colonne.Add(new Colonna("RATING", "Rating", wCol1, TipoAllineamento.SINISTRA, false, dFont, false, TipoColonna.IMMAGINE_E_TESTO));
            tabellaDati.Colonne.Add(new Colonna("CONTROVALORE", "Controvalore €", wCol2, TipoAllineamento.DESTRA, false, dFont, false));
            tabellaDati.Colonne.Add(new Colonna("Percentage", "Peso %", wCol3, TipoAllineamento.DESTRA, false, dFont, false));            
            tabellaDati.Draw(datitab, document);
            //SIMBOLI
            int nPallino = 1;
            float yPallino = Ytable + tabellaDati.AltezzaCella;
            float xPallino = Xtable;
            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
            foreach (DataRow drSimbolo in tabellaDati.Datasource.Rows)  {
                RgbColor color =  
                    new RgbColor(byte.Parse(drSimbolo["Red"].ToString()), byte.Parse(drSimbolo["Green"].ToString()), byte.Parse(drSimbolo["Blue"].ToString()));
                ceTe.DynamicPDF.PageElements.Circle simbolo =
                    new ceTe.DynamicPDF.PageElements.Circle(xPallino, yPallino, 10 / 1.8F, color, color);    /*10 / 1.414F*/                
                page.Elements.Add(simbolo);
                yPallino += tabellaDati.AltezzaCella;
                nPallino++;
            }
            Ytable += (datitab.GetRowDim() * (datitab.getNumRow() + 1)) + document.getSectionSpace(); 
            //TOTALE
            tabellaDati = new Tabella(Xtable, Ytable);
            tabellaDati.Header = false;
            tabellaDati.Colonne.Add(new Colonna("Descrizione", "", wCol1, TipoAllineamento.SINISTRA, true, dFont, true));
            tabellaDati.Colonne.Add(new Colonna("Controvalore", "", wCol2, TipoAllineamento.DESTRA, true, dFont, true));
            tabellaDati.Colonne.Add(new Colonna("Percentage", "", wCol3, TipoAllineamento.DESTRA, true, dFont, true));
            tabellaDati.Draw(new DatiTabella(set.Totale), document);
            

            #endregion


            document.setLastPos(hGrafico);
          


        }
    }
}