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

namespace PDFGenerator.Presentation.Section
{
    public class FD181 : SIstogramma, ISezione
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        string Titolo = string.Empty;
        string _testotitolo;
        string _testointroduttivo;
        string _testoAlternativo;
        
        DataThread _dataThread;
        /// <summary>
        /// Testo introduttivo della sezione.
        /// </summary>
        public string TestoIntroduttivo
        {
            get
            {
                return _testointroduttivo;
            }
            set
            {
                _testointroduttivo = value;
            }
        }

        /// <summary>
        /// Testo del titolo della sezione.
        /// </summary>
        public string TestoTitolo
        {
            get
            {
                return _testotitolo;
            }
            set
            {
                _testotitolo = value;
            }
        }

        public string TestoAlternativo
        {
            get { return _testoAlternativo; }
            set { _testoAlternativo = value; }
        }


        public void setTitolo(string label)
        {
            Titolo = label;
        }

        public FD181()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected string[] setTableForIsto()
        {
            string[] Columns = new String[4];
            Columns[0] = "PercentualeESG";
            Columns[1] = "Red";
            Columns[2] = "Green";
            Columns[3] = "Blue";
            return Columns;
        }

        #region Nuove funzionalità ESG 2024
        public void writeSezione(DataThread dataThread)
        {
            dataThread.SETDATA();
            _dataThread = dataThread;
            DocumentPDF document = dataThread.DocumentPDF;
            DataSetS181AnalisiDiPortafoglioESG set = (DataSetS181AnalisiDiPortafoglioESG)dataThread.Data.DatiSezione;


            DatiTabella datitab = new DatiTabella();
            DatiTabella datitabObiettivo = new DatiTabella();
            DatiTabella datiRating = new DatiTabella();
            DatiTabella datiTest = new DatiTabella();
            DatiTabella datiCaratteristiche = new DatiTabella();
            DatiTabella datiHeaderCaratteristiche = new DatiTabella();
            DatiTabella datiPercSostenibilita = new DatiTabella();

            float left = document.getMargineLeft();
            float right = document.getLastPos();
            Tabella tab = new Tabella(document.getMargineLeft(), document.getLastPos());

            // Titolo della sezione
            logger.Info("Titolo della sezione " + _testointroduttivo);
            document.setLastPos(-40);
            document.setChapterHeader(string.Concat(_testointroduttivo, "<br>"), 0, 520, 8);


            // VECCHIE SEZIONI CHE C'ERANO ANCHE PRIMA DI ESG 2024 E MODIFICATE
            writeSezione_PreferenzeFattoriSostenibilita(set, document);
            writeSezione_PercentualePortafoglio(set, document);

            ///////////////////////////////////////
            // NUOVE SEZIONI ESG 2024
            writeSezione_UlterioriPreferenzeSostenibilità(set, document);
            writeSezione_SFDR_Taxonomy(set, document);
            writeSezione_PrincipalAdverseImpact(set, document);
            ///////////////////////////////////////

            writeSezione_ProdottiLineaPreferenzeSostenibilita(set, document);
            //document.setLastPos(8);
            document.setLastPos(9);


            #region Titolo Seconda tabella

            switch (dataThread.Rete)
            {
                case "F":
                    {
                        _testoAlternativo = _testoAlternativo.Replace("$/Banca/$", "Fideuram");
                        break;
                    }
                case "S":
                    {
                        //Rosaspina Andrea//
                        _testoAlternativo = _testoAlternativo.Replace("$/Banca/$", "Fideuram S.p.A., commercializzato tramite la rete di private banker Sanpaolo Invest");
                        //_testoAlternativo = _testoAlternativo.Replace("$/Banca/$", "Sanpaolo Invest");
                        break;
                    }
                case "W":
                    {
                        _testoAlternativo = _testoAlternativo.Replace("$/Banca/$", "IW Private Investments");
                        break;
                    }
            }

            logger.Info("Titolo della seconda tabella " + _testointroduttivo);
            //document.setLastPos(55);  //-20
            // è stato chiesto più spazio tra Prodotti in linea con le... e la nota alternativa
            document.setLastPos(55);
            document.setChapterHeader(string.Concat(_testoAlternativo, "<br>"), 0, 520, 8);
            logger.Info("Fine Titolo della seconda tabella " + _testointroduttivo);
            #endregion

            #region Seconda Tabella

            /**************** Scrittura di solo header della tabella ***************/
            //logger.Info("Scrittura di solo header della tabella Titolo della seconda tabella " + _testointroduttivo);
            //DataSet dsFattoriDistribuzioni = new DataSet();
            //DataTable dtFattoriDistribuzioni = new DataTable();
            //dtFattoriDistribuzioni.Columns.Add("Fattori ESG", typeof(System.String));
            //dtFattoriDistribuzioni.Columns.Add("Distribuzione (%)", typeof(System.String));

            //dsFattoriDistribuzioni.Tables.Add(dtFattoriDistribuzioni);
            //DataRow dr = dtFattoriDistribuzioni.NewRow();
            //dr[0] = "";
            //dr[1] = "";

            //dtFattoriDistribuzioni.Rows.Add(dr);

            //datiHeaderCaratteristiche.table = dsFattoriDistribuzioni.Tables[0];

            //datiHeaderCaratteristiche.setY(document.getLastPos() + 10);
            //datiHeaderCaratteristiche.setX(document.getMargineLeft());

            //datiHeaderCaratteristiche.setHeader(true);

            //datiHeaderCaratteristiche.setCellSpace(10);

            //datiHeaderCaratteristiche.setCell(120, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, false);
            //datiHeaderCaratteristiche.setCell(390, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, false);
            //document.InsertTable(tab.getTabella(datiHeaderCaratteristiche));
            /**************** Scrittura di solo header della tabella ***************/

            writeSezione_IntestazioneGrafico(document,left);

            ceTe.DynamicPDF.Merger.ImportedPage page1 = document.getCurrentPage();


            /**************** Per Test quella buona - per miglioramento abilitare il codice "Nuova Versione in corso di verifiche"  **********************/
            logger.Info("Inizio impostazione grafico");
            List<IValoriSerie> listaValoriPesoRelativo = new List<IValoriSerie>();
            foreach (var item in set.Tables["dtCaratteristiche"].AsEnumerable())
            {

                IValoriSerie IvaloriSeriePesoRelativo = new IValoriSerie();
                IvaloriSeriePesoRelativo.chiave = item[0].ToString();
                IvaloriSeriePesoRelativo.valore = Convert.ToDecimal(item[1]);
                IvaloriSeriePesoRelativo.colore = (System.Drawing.Color)item["ColorePercentuale"]; //  System.Drawing.Color.FromArgb((int)item["Red"], (int)item["Green"], (int)item["Blue"]);
                listaValoriPesoRelativo.Add(IvaloriSeriePesoRelativo);

            }
            //List<IValoriSerie> listaValoriPesoRelativo_2 = new List<IValoriSerie>();
            //foreach (var item in set.Tables["dtCaratteristiche"].AsEnumerable())
            //{
            //                    IValoriSerie IvaloriSeriePesoRelativo = new IValoriSerie();
            //    IvaloriSeriePesoRelativo.chiave = item[0].ToString()+"_2";
            //    IvaloriSeriePesoRelativo.valore = Convert.ToDecimal(item[1]);
            //    IvaloriSeriePesoRelativo.colore = (System.Drawing.Color)item["ColoreSostenibilita"]; //  System.Drawing.Color.FromArgb((int)item["Red"], (int)item["Green"], (int)item["Blue"]);
            //    listaValoriPesoRelativo_2.Add(IvaloriSeriePesoRelativo);

            //}
            //double altezzaGraficoPesoRischio_Prodotto = 15 * (tabellaDati.Datasource.Rows.Count + 0.5);
            //IstoFarfallaS133BIS graficoCaratteristiche = new IstoFarfallaS133BIS(750, 150);  //92 //85

            IstoFarfallaS133BIS graficoCaratteristiche = new IstoFarfallaS133BIS(750, 150);  //92 //85
            graficoCaratteristiche.DocumentPDF = document;
            graficoCaratteristiche.PositionX = 184; // 145;
            graficoCaratteristiche.PositionY = Convert.ToInt32(document.getLastPos()); // + 22); //32 //38 //42 //542;
            graficoCaratteristiche.AxisY_LineColor = true;
            graficoCaratteristiche.AxisY_Hide = false;
            graficoCaratteristiche.AxysX_Increment = Convert.ToDecimal(1);
            graficoCaratteristiche.AxisY_DashStyle = Dundas.Charting.WebControl.ChartDashStyle.DashDot;
            graficoCaratteristiche.isShadowOffset = false;    // Abilita l'ombreggiatura delle barre per ESG è fissato a 40 
            graficoCaratteristiche.isPixelPointWidth = "40"; // 40"; // serve per dimensionare l'altezza delle barre
            graficoCaratteristiche.BorderLineColor = System.Drawing.Color.FromArgb(0, 0, 0);
            graficoCaratteristiche.BorderLineWidth = 10;
            SerieIstoFarfallaS133BIS seriePesoRelativo = new SerieIstoFarfallaS133BIS();
            //SerieIstoFarfallaS133BIS seriePesoRelativo_2 = new SerieIstoFarfallaS133BIS();

            seriePesoRelativo = new SerieIstoFarfallaS133BIS();
            seriePesoRelativo.Name = "Caratteristiche%";
            seriePesoRelativo.Values = listaValoriPesoRelativo;
            //seriePesoRelativo.ElementPosition = new Dundas.Charting.WebControl.ElementPosition(1F, -5F, 50.7F, 105F);
            seriePesoRelativo.ElementPosition = new Dundas.Charting.WebControl.ElementPosition(0F, -5F, 50.7F, 120F);  // ultimo parametro per aumentare lo spazio tra le barre
            seriePesoRelativo.AxisY_CustumizedByValues = false;
            seriePesoRelativo.ReverseAxisY = false;
            seriePesoRelativo.Axisy_Interval = 10;
            seriePesoRelativo.PixelPointWidth = 40;
            seriePesoRelativo.ShowLabelAsValue = false;

            // seriePesoRelativo_2 = new SerieIstoFarfallaS133BIS();
            //seriePesoRelativo_2.Name = "Sostenibilita%";
            //seriePesoRelativo_2.Values = listaValoriPesoRelativo_2;
            //seriePesoRelativo.ElementPosition = new Dundas.Charting.WebControl.ElementPosition(1F, -5F, 50.7F, 105F);
            //seriePesoRelativo_2.ElementPosition = new Dundas.Charting.WebControl.ElementPosition(1F, -5F, 50.7F, 120F);  // ultimo parametro per aumentare lo spazio tra le barre
            //seriePesoRelativo_2.AxisY_CustumizedByValues = false;
            //seriePesoRelativo_2.ReverseAxisY = false;
            //seriePesoRelativo_2.Axisy_Interval = 10;
            //seriePesoRelativo.PixelPointWidth = 20;
            //seriePesoRelativo_2.ShowLabelAsValue = false;

            graficoCaratteristiche.SerieList.Add(seriePesoRelativo);
            //graficoCaratteristiche.SerieList.Add(seriePesoRelativo_2);


            logger.Info("Inizio grafico caratteristiche");
            // Disegno il grafico
            graficoCaratteristiche.Draw();  //// Pino Test 15/09/2022
            logger.Info("Fine grafico caratteristiche ");


            // Inserisco riga bianca per effetto distanziatore
            logger.Info("Inizio inserimento riga bianca sotto caratteristiche");
            //page1.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft(), document.getLastPos() + 22, 500, 6, 0, new RgbColor(255, 255, 255)));
            //           page1.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft(), document.getLastPos() + 22, 300, 6, 0, new RgbColor(255, 255, 255)));

            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();

            //         page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft(), document.getLastPos() + 168, 500, 6, 0, new RgbColor(255, 255, 255)));

            #region RIGHE  "di cui in linea con le Preferenze di Sostenibilità"
            //////////////////////////////////////////
            // inserimento righe di non sostenibilità

            int widthLabel = 765;
            int startX = 178;

            // BARRA NUMERI DA 0 a 100 Percentuali
            for (int i = 0; i < 110; i += 10)
            {
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(i.ToString() + ",00",
                    startX, document.getLastPos() + 146, widthLabel, 20, Globals.OpenTypeFontVerdana,
                    7, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));
                startX = startX + 36;
            }

            string labelNonSostenibilita = "di cui in linea con le\nPreferenze di Sostenibilità"; //
            //" + GetPercentualeSostenibilita("Ambientali", set.Tables["dtCaratteristiche"]).ToString() ;
            //float y = document.getLastPos() + 58;
            float lineHeight = 20; // Altezza della riga

            float xLabelSost = left+9;
            // AMBIENTALI
            if (TitoloAttivo("Ambientali", set.Tables["dtCaratteristiche"]))
            {
                foreach (string linea in labelNonSostenibilita.Split('\n'))
                {
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(labelNonSostenibilita.ToString(),
                        xLabelSost, document.getLastPos() + 38, 765, lineHeight, Globals.OpenTypeFontVerdana,
                        6, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));
                }
                /////////////////////
            }

            // SOCIALI
            if (TitoloAttivo("Sociali", set.Tables["dtCaratteristiche"]))
            {
                foreach (string linea in labelNonSostenibilita.Split('\n'))
                {
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(labelNonSostenibilita.ToString(),
                        xLabelSost, document.getLastPos() + 83, 765, lineHeight, Globals.OpenTypeFontVerdana,
                        6, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));
                    // y -= lineHeight; // Spostare la posizione y per la riga successiva
                }
                ///// PERCENTUALE SOSTENIBILITA' ///// 
                /////////////////////////////////////
            }
            // BUONA GOVERNACE
            if (TitoloAttivo("di", set.Tables["dtCaratteristiche"]))
            {
                foreach (string linea in labelNonSostenibilita.Split('\n'))
                {
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(labelNonSostenibilita.ToString(),
                        xLabelSost, document.getLastPos() + 125, 765, lineHeight, Globals.OpenTypeFontVerdana, 6, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));
                    //y -= lineHeight; // Spostare la posizione y per la riga successiva
                }
                ///// PERCENTUALE SOSTENIBILITA' ///// 
                //object percSostGov = GetPercentualeSostenibilita("Governance", set.Tables["dtCaratteristiche"]);
                //page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(percSostSoc.ToString(),
                //        xLabelSost, document.getLastPos() + 80, 780, lineHeight, Globals.OpenTypeFontVerdana,
                //        8, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));
                /////////////////////////////////////
            }
            //////////////////////////////////////////
            #endregion

            #region rettangoli sostenibilità

            int numeroRiga = 1;
            foreach (DataRow row in set.Tables["dtCaratteristiche"].Rows)
            {
                double W_PercentualeSostenibilita = 3.63;
                double percESG = Convert.ToDouble(row["PercentualeESG"]);

                if (percESG > 0 && percESG < 5)
                {
                    W_PercentualeSostenibilita = 3.30;
                }
                if (percESG > 5 && percESG < 10)
                {
                    W_PercentualeSostenibilita = 3.54;
                }
                if (percESG > 10 && percESG < 15)
                {
                    W_PercentualeSostenibilita = 3.55;
                }
                if (percESG > 20 && percESG < 30)
                {
                    W_PercentualeSostenibilita = 3.61;
                }
                if (percESG > 30 && percESG < 35)
                {
                    W_PercentualeSostenibilita = 3.612;
                }
                if (percESG > 35 && percESG < 40)
                {
                    W_PercentualeSostenibilita = 3.613;
                }
                if (percESG > 40 && percESG < 50)
                {
                    W_PercentualeSostenibilita = 3.615;
                }
                if (percESG > 50 && percESG < 55)
                {
                    W_PercentualeSostenibilita = 3.615;
                }

                if (percESG > 55 && percESG < 60)
                {
                    W_PercentualeSostenibilita = 3.615;
                }

                if (percESG > 60 && percESG < 65)
                {
                    W_PercentualeSostenibilita = 3.615;
                }

                if (percESG > 65 && percESG < 70)
                {
                    W_PercentualeSostenibilita = 3.618;
                }

                if (percESG > 70 && percESG < 75)
                {
                    W_PercentualeSostenibilita = 3.62;
                }

                if (percESG > 75 && percESG < 80)
                {
                    W_PercentualeSostenibilita = 3.625;
                }

                if (percESG > 80 && percESG < 85)
                {
                    W_PercentualeSostenibilita = 3.625;
                }

                if (percESG > 85 && percESG < 90)
                {
                    W_PercentualeSostenibilita = 3.63;
                }

                if (percESG > 90 && percESG < 95)
                {
                    W_PercentualeSostenibilita = 3.635;
                }
                if (percESG > 95)
                {
                    W_PercentualeSostenibilita = 3.64;
                }
                if (!row["ColoreSostenibilita"].ToString().Equals("0"))
                {
                    System.Drawing.Color col = (System.Drawing.Color)row["ColoreSostenibilita"];
                    float Y_RettangoloSostenibilita = 0;
                    switch (numeroRiga)
                    {

                        case 1:
                            //incrementa = 53;
                            Y_RettangoloSostenibilita = 52.5f;
                            break;
                        case 2:
                            // incrementa = 95;
                            Y_RettangoloSostenibilita = 94.2f;
                            break;
                        case 3:
                            //incrementa = 136;
                            Y_RettangoloSostenibilita = 135.5f;
                            break;
                    }
                    double perc = (Convert.ToDouble(row["PercentualeSostenibilita"])) * W_PercentualeSostenibilita;
                    //129
                    double x = left + 146;

                    /////////////////////////////////////
                    ///// RETTANGOLO SOSTENIBILITA' ///// 
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle((float)x,
                       document.getLastPos() + Y_RettangoloSostenibilita - 22, (float)perc,
                       6, 0, new RgbColor(col.R, col.G, col.B)));
                    /////////////////////////////////////

                }

                numeroRiga++;

            }

            #endregion
            /****** Inserimento tabella con le due colonne e con la colonna delle Caratteristiche riempita ********/
            logger.Info("Inizio inserimento tabella caratteristiche");
            datiCaratteristiche.table = set.Tables["dtCaratteristiche"];
            //Tabella tabellaCaratteristiche = new Tabella(document.getMargineLeft() + 15, document.getLastPos() + 85, 0, -2, 0.009F);
            //Tabella tabellaCaratteristiche = new Tabella(document.getMargineLeft() +15 , document.getLastPos() + 89, 0, -2, 0.009F);
            Tabella tabellaCaratteristiche = new Tabella(left, document.getLastPos() + 67, 0, -2, 0.009F);

            tabellaCaratteristiche.Datasource = datiCaratteristiche.table;
            tabellaCaratteristiche.Header = false;
            tabellaCaratteristiche.DimensioneLinea = 0;
            tabellaCaratteristiche.AltezzaCella = 45; //25;
            tabellaCaratteristiche.Colonne.Add(new Colonna("CaratteristicheESG", "Caratteristiche", 0, TipoAllineamento.SINISTRA, false, 0, false, TipoColonna.TESTO));
            tabellaCaratteristiche.Colonne.Add(new Colonna("Titolo", "", 110, TipoAllineamento.SINISTRA, true, 7, false, TipoColonna.TESTO));
            tabellaCaratteristiche.Colonne.Add(new Colonna("PercentualiMostrate", "Distribuzione (%)", 26, TipoAllineamento.DESTRA, false, 6, false, TipoColonna.TESTO));
            tabellaCaratteristiche.DrawESG(datiCaratteristiche, document);
            logger.Info("Fine inserimento tabella caratteristiche");
            /***********************************************************************************************************/

            #endregion

            InsertPallinoFattore(document, "Ambientali", set, "logo-ambientaleSM_182.png", "logo-ambientaleGray.png", 41, left);
            InsertPallinoFattore(document, "Sociali", set, "logo-socialeSM_182.png", "logo-socialeGray.png", 85, left);
            InsertPallinoFattore(document, "di", set, "logo-governanceSM_182.png", "logo-governanceGray.png", 132,left);

            //document.setLastPos(180);  //120;
            document.addPage();
        }

        void InsertPallinoFattore(DocumentPDF doc, string elemento, DataSet set, string IconaOK, string IconaGray, int incrementa, float indent)
        {

            string nameImage = System.IO.Path.Combine(CACHE.pathGraf,
                TitoloAttivo(elemento, set.Tables["dtCaratteristiche"]) ?
                IconaOK : IconaGray);

            ceTe.DynamicPDF.PageElements.Image img = new Image(nameImage, indent-7, doc.getLastPos() - 22 + incrementa);
            img.SetSize(15, 15); // diminuisco size

            doc.getCurrentPage().Elements.Add(img);
        }

        void InsertPercentualiAmbientali(ceTe.DynamicPDF.Merger.ImportedPage page, DataSet set,
            float x, float y, float w, float h)
        {
            object perc = GetPercentuale("Ambientali", set.Tables["dtCaratteristiche"]);
            object percSost = GetPercentualeSostenibilita("Ambientali", set.Tables["dtCaratteristiche"]);
            string label = perc + "-<br>-" + percSost;

            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(perc.ToString(),
                    x, y, w, h, Globals.OpenTypeFontVerdana,
                    5, TextAlign.Left, ceTe.DynamicPDF.CmykColor.Black));

        }
        public void writeSezione_IntestazioneGrafico(DocumentPDF document,float x)
        {

            document.setLastPos(1);
            DataTable dtInt = new DataTable()
            {
                Columns = { "Titolo1", "blank", "Titolo2" }
            };

            string titolo1 = Utility.setColoreTesto("Fattori ESG", _dataThread); // string.Format("<font color =\"green\">{0}</font>", "Fattori ESG");
            string titolo2 = Utility.setColoreTesto("Distribuzione (%)", _dataThread); // string.Format("<font color =\"green\">{0}</font>", "(%)");
            dtInt.Rows.Add(titolo1, "", titolo2);

            DatiTabella datiTabIntestazioneGrafico = new DatiTabella();
            datiTabIntestazioneGrafico.table = dtInt;
            Tabella tabellaIntestazioneGrafico = new Tabella(x, document.getLastPos() + 15, 0, -2, 0.2F);
            tabellaIntestazioneGrafico.Header = false;
            tabellaIntestazioneGrafico.AltezzaCella = tabellaIntestazioneGrafico.AltezzaCella - 5;
            tabellaIntestazioneGrafico.Colonne.Add(new Colonna("Titolo1", titolo1, 133, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaIntestazioneGrafico.Colonne.Add(new Colonna("blank", "", 6, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO)); //45
            tabellaIntestazioneGrafico.Colonne.Add(new Colonna("Titolo2", titolo2, 370, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO)); //45
            tabellaIntestazioneGrafico.Draw(datiTabIntestazioneGrafico, document);

        }
        public void writeSezione_PreferenzeFattoriSostenibilita(DataSet set, DocumentPDF document)
        {



            ////if (dataThread.TipoReport.ToUpper() == "DIAGNOSI") tab.HeaderFont = 8;
            ////tab.Header = true;
            ////tab.LineaFineTabella = false;
            ////tab.Datasource = datitab.table;

            datitab.table = set.Tables["dtPreferenze"];

            /**** ESG - Fix faccio una copia del datatable per impostazione della tabella - Pino ****/
            DataTable _dtCopy = set.Tables["dtPreferenze"].Copy();
            /***************************************************************************************/

            /**** ESG - Fix elimino le colonne delle preferenze impostate a null- Pino *****/
            if (datitab.table.Rows[0]["Ambientale"].Equals(DBNull.Value))
                datitab.table.Columns.Remove("Ambientale");

            if (datitab.table.Rows[0]["Sociale"].Equals(DBNull.Value))
                datitab.table.Columns.Remove("Sociale");

            if (datitab.table.Rows[0]["Governance"].Equals(DBNull.Value))
                datitab.table.Columns.Remove("Governance");
            /****************************************************************************************/

            datitab.SetRowDim(25);
            datitab.setIsLinee(2);


            document.setLastPos(2);
            Tabella tabellaDatiRap = new Tabella(document.getMargineLeft(), document.getLastPos(), 0, -2, 0.2F);
            tabellaDatiRap.Header = false;

            /***** ESG - Fix elimino le colonne delle preferenze impostate a null - Pino *****/
            tabellaDatiRap.Colonne.Add(new Colonna("Descrizione", "Preferenze di sostenibilità", 170, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));   //170  120
            tabellaDatiRap.Colonne.Add(new Colonna("Preferenza", "", 40, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO));  //40   //20
                                                                                                                                           //tabellaDatiRap.Colonne.Add(new Colonna("Ambientale", "", 100, TipoAllineamento.SINISTRA, false, 9, false,TipoColonna.IMMAGINE));
                                                                                                                                           //tabellaDatiRap.Colonne.Add(new Colonna("Sociale", "", 100, TipoAllineamento.SINISTRA, false, 9, false,TipoColonna.IMMAGINE));
                                                                                                                                           //tabellaDatiRap.Colonne.Add(new Colonna("Governance", "", 100, TipoAllineamento.SINISTRA, false, 9, false,TipoColonna.IMMAGINE));



            if (!(_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value)) && !(_dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value)) && !(_dtCopy.Rows[0]["Governance"].Equals(DBNull.Value)))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Ambientale", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE)); //100   //75
                tabellaDatiRap.Colonne.Add(new Colonna("Sociale", "", 80, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));    //100   //60
                tabellaDatiRap.Colonne.Add(new Colonna("Governance", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE)); //100   //75
            }

            if (_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value) && _dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value) && !(_dtCopy.Rows[0]["Governance"].Equals(DBNull.Value)))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Governance", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }

            if (_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value) && !(_dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value)) && _dtCopy.Rows[0]["Governance"].Equals(DBNull.Value))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Sociale", "", 80, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }

            if (!(_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value)) && _dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value) && _dtCopy.Rows[0]["Governance"].Equals(DBNull.Value))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Ambientale", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }

            if (_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value) && !(_dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value)) && !(_dtCopy.Rows[0]["Governance"].Equals(DBNull.Value)))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Sociale", "", 80, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
                tabellaDatiRap.Colonne.Add(new Colonna("Governance", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }

            if (!(_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value)) && !(_dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value)) && _dtCopy.Rows[0]["Governance"].Equals(DBNull.Value))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Ambientale", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
                tabellaDatiRap.Colonne.Add(new Colonna("Sociale", "", 80, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }

            if (!(_dtCopy.Rows[0]["Ambientale"].Equals(DBNull.Value)) && _dtCopy.Rows[0]["Sociale"].Equals(DBNull.Value) && !(_dtCopy.Rows[0]["Governance"].Equals(DBNull.Value)))
            {
                tabellaDatiRap.Colonne.Add(new Colonna("Ambientale", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
                tabellaDatiRap.Colonne.Add(new Colonna("Governance", "", 100, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.IMMAGINE));
            }
            /*******************************************************************************/


            tabellaDatiRap.Draw(datitab, document);

        }

        public void writeSezione_PercentualePortafoglio(DataSet set, DocumentPDF document)
        {
            DatiTabella datiPercSostenibilita = new DatiTabella();
            document.setLastPos(2);
            datiPercSostenibilita.table = set.Tables["dtPreferenze"];
            Tabella tabellaDatiPercSost = new Tabella(document.getMargineLeft(), document.getLastPos(), 0, -2, 0.2F);
            tabellaDatiPercSost.Header = false;

            tabellaDatiPercSost.Colonne.Add(new Colonna("DescPercSostenibilita", "Soglia di sostenibilità", 170, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaDatiPercSost.Colonne.Add(new Colonna("ValPercSostenibilita", "", 120, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO)); //45

            tabellaDatiPercSost.Draw(datiPercSostenibilita, document);
        }

        public void writeSezione_UlterioriPreferenzeSostenibilità(DataSet set, DocumentPDF document)
        {
            // ESG 2024 - Nuova sezione -PS-
            document.setLastPos(2);
            DatiTabella datiUlterioriPreferenze = new DatiTabella();
            datiUlterioriPreferenze.table = set.Tables["dtUlterioriPrefSostenibilita"]; 
            Tabella tabellaUlterioriPreferenze = new Tabella(document.getMargineLeft(), document.getLastPos(), 0, -2, 0.2F);
            tabellaUlterioriPreferenze.Header = false;

            tabellaUlterioriPreferenze.Colonne.Add(new Colonna("NomeCampo", "", 170, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaUlterioriPreferenze.Colonne.Add(new Colonna("Risposta", "", 120, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO)); //45

            tabellaUlterioriPreferenze.Draw(datiUlterioriPreferenze, document);
        }

        public void writeSezione_SFDR_Taxonomy(DataSet set, DocumentPDF document)
        {
            // ESG 2024 - Nuova sezione -PS-
            document.setLastPos(2);
            DatiTabella datiSFRD_Taxonomy = new DatiTabella();
            datiSFRD_Taxonomy.table = set.Tables["dtSFDR_Taxonomy"];
            Tabella tabellaDatiSFRD_Tax = new Tabella(document.getMargineLeft(), document.getLastPos(), 0, -2, 0.2F);
            tabellaDatiSFRD_Tax.Header = false;
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("TitoloSFDR", "", 170, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("ValoreSFDR", "", 50, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO)); //45
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("TitoloTaxonomy", "", 190, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("ValoreTaxonomy", "", 50, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO)); //45

            tabellaDatiSFRD_Tax.Draw(datiSFRD_Taxonomy, document);

        }

        protected ceTe.DynamicPDF.Imaging.ImageData GetImageData(string imgUrl)
        {
            ImageData imgData = null;
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                imgData = ImageData.GetImage(wc.DownloadData(imgUrl));
            }
            return imgData;
        }

        public void writeSezione_PrincipalAdverseImpact(DataSet set, DocumentPDF document)
        {
            try
            {
                DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable();
                dt1.Columns.Add("Titolo"); dt1.Columns.Add("Image"); dt1.Columns.Add("Descrizione");
                dt2.Columns.Add("Titolo"); dt2.Columns.Add("Image"); dt2.Columns.Add("Descrizione");

                bool primaRiga = true;
                foreach (DataRow row in set.Tables["dtPai"].Rows)
                {
                    if (primaRiga == true)
                    {
                        dt1.ImportRow(row);
                        primaRiga = false;
                    }
                    dt2.ImportRow(row);
                }

                DatiTabella datiPrincipalAdverseImpact_1 = new DatiTabella();
                datiPrincipalAdverseImpact_1.table = dt1;
                Tabella tabellaPAI = new Tabella(document.getMargineLeft(), document.getLastPos() + 49, 0, -2, 0.2F);
                tabellaPAI.Header = false;
                tabellaPAI.AltezzaCella = 45;
                tabellaPAI.Colonne.Add(new Colonna("Titolo", "", 170, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
                tabellaPAI.Draw(datiPrincipalAdverseImpact_1, document);

                if (set.Tables["dtPai"].Rows.Count > 1)
                {
                    //document.setLastPos(1);
                    DatiTabella datiPrincipalAdverseImpact_2 = new DatiTabella();
                    datiPrincipalAdverseImpact_2.table = dt2;
                    datiPrincipalAdverseImpact_2.table.Columns.Remove("Titolo");
                    Tabella tabellaPAI_2 = new Tabella(document.getMargineLeft() + 170, document.getLastPos() + 27, 0, -2, 0.2F);
                    if (dt2.Rows[1]["Descrizione"].Equals("")) // sta solo ambientali 
                    {
                        tabellaPAI_2 = new Tabella(document.getMargineLeft() + 170, document.getLastPos() + 40, 0, -2, 0.2F);
                        if (!dt2.Rows[0]["Descrizione"].ToString().Contains("Preferisco non dettagliare"))
                        {
                            tabellaPAI_2.Colonne.Add(new Colonna("Image", "", 20, TipoAllineamento.DESTRA, false, 9, false, TipoColonna.IMMAGINE)); //45
                            tabellaPAI_2.Colonne.Add(new Colonna("Descrizione", "", 310, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO)); //45
                        }
                        else
                        {
                            tabellaPAI_2.Colonne.Add(new Colonna("Descrizione", "", 310, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO)); //45
                            tabellaPAI_2.X = tabellaPAI_2.X + 10;
                        }
                    }
                    if (dt2.Rows[0]["Descrizione"].Equals("")) // sta solo sociali
                    {
                        tabellaPAI_2 = new Tabella(document.getMargineLeft() + 170, document.getLastPos() + 15, 0, -2, 0.2F);
                        tabellaPAI_2.Colonne.Add(new Colonna("Image", "", 20, TipoAllineamento.DESTRA, false, 9, false, TipoColonna.IMMAGINE)); //45
                        tabellaPAI_2.Colonne.Add(new Colonna("Descrizione", "", 310, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO)); //45
                    }
                    if (dt2.Rows[0]["Descrizione"].Equals("") && dt2.Rows[1]["Descrizione"].Equals("-"))
                    {
                        tabellaPAI_2 = new Tabella(document.getMargineLeft() + 180, document.getLastPos() + 15, 0, -2, 0.2F);
                        //tabellaPAI_2.Colonne.Add(new Colonna("Image", "", 20, TipoAllineamento.DESTRA, false, 9, false, TipoColonna.IMMAGINE)); //45
                        tabellaPAI_2.Colonne.Add(new Colonna("Descrizione", "", 20, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO)); //45
                    }
                    if (!dt2.Rows[0]["Descrizione"].Equals("") && !dt2.Rows[1]["Descrizione"].Equals(""))
                    {
                        tabellaPAI_2 = new Tabella(document.getMargineLeft() + 170, document.getLastPos() + 27, 0, -2, 0.2F);
                        tabellaPAI_2.Colonne.Add(new Colonna("Image", "", 20, TipoAllineamento.DESTRA, false, 9, false, TipoColonna.IMMAGINE)); //45
                        tabellaPAI_2.Colonne.Add(new Colonna("Descrizione", "", 310, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO)); //45
                    }



                    tabellaPAI_2.Header = false;
                    tabellaPAI.AltezzaCella = 12;

                    //tabellaPAI_2.Colonne.Add(new Colonna("Titolo", "", 170, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO));

                    tabellaPAI_2.LineeTabella = false;
                    tabellaPAI_2.LineaFineTabella = false;

                    tabellaPAI_2.Draw(datiPrincipalAdverseImpact_2, document);
                }
            }
            catch (Exception ex)
            {
            }

            //document.setLastPos(10);
        }

        public void writeSezione_ProdottiLineaPreferenzeSostenibilita(DataSet set, DocumentPDF document)
        {
            //document.setLastPos(3);
            Tabella tab = new Tabella(document.getMargineLeft(), document.getLastPos());
            DatiTabella datitabObiettivo = new DatiTabella();
            datitabObiettivo.table = set.Tables["dtObiettivo"];

            datitabObiettivo.SetRowDim(25);
            datitabObiettivo.setIsLinee(3);
            datitabObiettivo.setCell(170, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
            datitabObiettivo.setCell(350, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false, 7);

            datitabObiettivo.setY(document.getLastPos() + 50);
            datitabObiettivo.setX(document.getMargineLeft());

            document.InsertTable(tab.getTabella(datitabObiettivo));
        }

        private bool TitoloAttivo(string titolo, DataTable dt)
        {
            bool result = false;
            foreach (DataRow row in dt.Rows)
            {
                if (row["Titolo"].ToString().StartsWith(titolo))
                {
                    result = (bool)row["Sostenibilita"];
                    break;
                }
            }
            return result;
        }

        private object GetPercentualeSostenibilita(string titolo, DataTable dt)
        {
            object result = 0;
            foreach (DataRow row in dt.Rows)
            {
                if (row["Titolo"].ToString().Contains(titolo))
                {
                    result = (object)row["PercentualeSostenibilita"];
                    break;
                }
            }
            return result;
        }

        private object GetPercentuale(string titolo, DataTable dt)
        {
            object result = 0;
            foreach (DataRow row in dt.Rows)
            {
                if (row["Titolo"].ToString().Contains(titolo))
                {
                    result = (object)row["PercentualeESG"];
                    break;
                }
            }
            return result;
        }
        #endregion

    }
}