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;
using NLog;

namespace PDFGenerator.Presentation.Section
{
    public class S144 : ISezione
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public void writeSezione(DataThread dataThread)
        {
            dataThread.SETDATA();
            DocumentPDF document = dataThread.DocumentPDF;
            DataSetS144 set = (DataSetS144)dataThread.Data.DatiSezione;
            DatiTabella datitab = new DatiTabella(set.Scadenza);


            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.Scadenza;
            //dati.setHeight(hGrafico);
            //dati.setWidth(hGrafico);
            //Torta isto = new Torta();
            //document.InsertGrafico(isto.getGrafico(dati), Xtable + wCol1 + wCol2 + wCol3 + 50, Ytable - 11);
            #endregion

            #region Tabella Rating
           

            Tabella tabellaDati = new Tabella(Xtable, Ytable);
            tabellaDati.Header = true;
            tabellaDati.Colonne.Add(new Colonna("Scadenza", "Scadenza", 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);
                page.Elements.Add(simbolo);
                yPallino += tabellaDati.AltezzaCella;
                nPallino++;
            }
            Ytable += (datitab.GetRowDim() * (datitab.getNumRow() + 1)) + document.getSectionSpace();


            /************************ Modifiche per Aladdin grafici a barre -- Pino**********************/
            DatiGrafico dati = new DatiGrafico();
            dati.dataTab = set.Scadenza;
            //dati.setHeight(Convert.ToSingle(datitab.getHeaderDim()) + (datitab.GetRowDim()));
            dati.setHeight(Convert.ToSingle(datitab.getHeaderDim()) + (datitab.GetRowDim() * (datitab.getNumRow() + 1) + 5));
            dati.setWidth(hGrafico);
            Istogramma isto = new Istogramma();
            document.InsertGrafico(isto.getGrafico(dati), 320, document.getLastPos() - (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), 190, 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));

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



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

            /*** TEST - Verifica Tempistica 09/11/2023 - Pino ****/
            /*** Inserimento log per verifica tempistica della sezione ***/
            //DateTimeOffset dateOffsetValueFin = DateTimeOffset.Parse(DateTime.Now.ToString("hh:mm:ss.ffff"));
            //var tDiff = dateOffsetValueFin - dateOffsetValueIni;
            //logger.Trace("S144 " + tDiff);
            /**************************************************************/

        }
    }
}