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 System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Xml.Linq;
using System.Web.UI.WebControls;
using NLog;
using System.IO;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace PDFGenerator.Presentation.Section
{

    // N.B. : questa classe è completamente diversa dalla S182
    public class FD182 : ISezione
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        string Titolo = string.Empty;
        string _testotitolo;
        string _testointroduttivo;
        string _testoAlternativo;
        public int widthImporti = 60;
        public int widthData = 50;
        public int fontsize = 6;
        public int headerFontSize = 5;
        

        /// <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 FD182()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public const int DimColProdotto = 108;
        public const int DimColE = 51;
        public const int DimColS = 51;
        public const int DimColG = 51;
        public const int DimColCatSFDR = 80;
        public const int DimColRating = 80;
        public const int DimColControvalore = 85;
        public const int DimPrimaColonnaTotale = DimColProdotto + DimColE + DimColS + DimColG + DimColCatSFDR + DimColRating;

        #region ISezione Members
        public void writeSezione(DataThread dataThread)
        {
            dataThread.SETDATA();
            DocumentPDF document = dataThread.DocumentPDF;
            DataRow totaleEvo; string descrizioneTotEvo = "";
            DataRow totaleBase; string descrizioneTotBase = "";

            decimal decTotaleEvo = 0;
            decimal decTotaleBase = 0;

            #region DIVIDO I DATI IN 2 DATATABLE

            DataSetFD182DettaglioProdottiESG set = (DataSetFD182DettaglioProdottiESG)dataThread.Data.DatiSezione;
            DatiTabella datitab = new DatiTabella();
            datitab.table = set.Tables["dtProdotti"];

            DataTable dtPortafoglioEvo = new DataTable();
            DataTable dtPortafoglioBase = new DataTable();

            var Columns = set.Tables["dtProdotti"].Columns.Cast<DataColumn>()
                                 .Select(p => new DataColumn(p.ColumnName, p.DataType))
                                 .ToArray();
            var Columns_2 = set.Tables["dtProdotti"].Columns.Cast<DataColumn>()
                                 .Select(p => new DataColumn(p.ColumnName, p.DataType))
                                 .ToArray();
            dtPortafoglioBase.Columns.AddRange(Columns);
            dtPortafoglioEvo.Columns.AddRange(Columns_2);

            for (int i = 0; i < datitab.table.Rows.Count; i++)
            {
                DataRow row = datitab.table.Rows[i];

                if (row["Portafoglio"].ToString().Equals("Portafoglio Direct Evo"))
                {
                    dtPortafoglioEvo.ImportRow(row);
                }
                else
                {
                    dtPortafoglioBase.ImportRow(row);
                }
            }
            #endregion

            #region PRELEVO I 2 TOTALI

            try
            {
                totaleEvo = set.Tables["dtTotali"].Select("Descrizione like '%DIRECT%'").FirstOrDefault();
                decTotaleEvo = Convert.ToDecimal(totaleEvo["Totale"]);
                descrizioneTotEvo = totaleEvo["Descrizione"].ToString();
            }
            catch { }
            try
            {
                totaleBase = set.Tables["dtTotali"].Select("Descrizione like '%BASE%'").FirstOrDefault();
                decTotaleBase = Convert.ToDecimal(totaleBase["Totale"]);
                descrizioneTotBase = totaleBase["Descrizione"].ToString();
            }
            catch { }
            #endregion

            #region TITOLO SEZIONE
            // Titolo della sezione

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

            logger.Info("Titolo della sezione " + _testointroduttivo);
            document.setLastPos(-40);
            document.setChapterHeader(string.Concat(_testointroduttivo, "<br>"), 0, 520, 8);
            float posizioneY = 0;
            posizioneY = posizioneY + 25;
            #endregion

            // Nuova Tabella ESG 2024
            // stampo i dati per Portafoglio Direct Evo e Portafoglio Base in 2 tabelle distinte
            //document.setLastPos(1);

            if (dtPortafoglioEvo != null && dtPortafoglioEvo.Rows != null && dtPortafoglioEvo.Rows.Count > 0)
            {
                writeSezionePortafoglio(document, dtPortafoglioEvo, set, dataThread, posizioneY, decTotaleEvo, "Portafoglio Direct Evo", 159, 253.32f);
            }
            else
            {
                if (dtPortafoglioBase.Rows.Count > 0)
                {
                    writeSezionePortafoglio(document, dtPortafoglioBase, set, dataThread, posizioneY, decTotaleBase, "Portafoglio Base", 159, 255.32f);
                }
                else
                {
                    writeSezionePortafoglio(document, dtPortafoglioBase, set, dataThread, posizioneY, decTotaleBase, "Portafoglio Base", 159, 253.32f);
                }
            }
        }

        void writeSezionePortafoglio(DocumentPDF document, DataTable dtDati, DataSetFD182DettaglioProdottiESG set, DataThread dataThread, float posizioneY,
            object decTotale, string portafoglio, float X_PalliniESG, float Y_PalliniESG)
        {

            if (dtDati.Rows.Count > 0)
            {
                //document.setLastPos(2);

                // INTESTAZIONE DESCRIZIONE PORTAFOGLIO
                DataTable dtIntestazione = new DataTable();
                dtIntestazione.Columns.Add("Des");
                dtIntestazione.Rows.Add(string.Format("<font color =\"{0}\"><b>{1}<b/></font>", "black", portafoglio));
                Tabella tabella = new Tabella(document.getMargineLeft(), document.getLastPos() + posizioneY, 30, -2, 0.2F);
                DatiTabella datiTabellaInt = new DatiTabella();
                tabella.Header = false;
                tabella.Colonne.Add(new Colonna("Des", Utility.setColoreTesto("Descrizione", dataThread), 500, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO));
                datiTabellaInt.table = dtIntestazione;
                tabella.Draw(datiTabellaInt, document);
                document.setLastPos(1);
                int k = 10;

                // INTESTAZIONE TABELLA
                Tabella tabellaDatiInstestazione = new Tabella(document.getMargineLeft(), document.getLastPos() + posizioneY - k, 30, -2, 0.2F);
                DatiTabella datiTabellaIntestazione = new DatiTabella();
                datiTabellaIntestazione.table = set.Tables["dtIntestazione"];
                tabellaDatiInstestazione.Header = false;
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colProdotto", Utility.setColoreTesto("Prodotto", dataThread), DimColProdotto, TipoAllineamento.SINISTRA, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colE", "", DimColE, TipoAllineamento.SINISTRA, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colS", "", DimColS, TipoAllineamento.SINISTRA, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colG", "", DimColG, TipoAllineamento.SINISTRA, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colCategoriaSFDR", Utility.setColoreTesto("Categoria SFDR", dataThread), DimColCatSFDR, TipoAllineamento.CENTRATO, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colESGRating", Utility.setColoreTesto("ESG Rating", dataThread), DimColRating, TipoAllineamento.CENTRATO, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Colonne.Add(new Colonna("colControvalore", Utility.setColoreTesto("Controvalore<br>(€)", dataThread), DimColControvalore, TipoAllineamento.DESTRA, true, 8, true, TipoColonna.TESTO));
                tabellaDatiInstestazione.Draw(datiTabellaIntestazione, document);
                posizioneY = (tabellaDatiInstestazione.AltezzaCella * datiTabellaIntestazione.table.Rows.Count) + 15;

                Y_PalliniESG = tabellaDatiInstestazione.Y - tabellaDatiInstestazione.AltezzaCella-k;
                // INSERIMENTO PALLINI ESG PRIMA TABELLA
                InsertPALLINI_ESG(document, X_PalliniESG, Y_PalliniESG);


                // DATI TABELLA
                Tabella tabellaDati_2 = new Tabella(document.getMargineLeft(), document.getLastPos() + posizioneY, 30, -2, 0.2F);
                DatiTabella datiTabellaDati_2 = new DatiTabella();
                datiTabellaDati_2.table = dtDati;
                tabellaDati_2.Header = false;
                tabellaDati_2.Colonne.Add(new Colonna("NomeProdotto", "", DimColProdotto, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO));
                tabellaDati_2.Colonne.Add(new Colonna("preferenzaE", "", DimColE, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO));
                tabellaDati_2.Colonne.Add(new Colonna("preferenzaS", "", DimColS, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO));
                tabellaDati_2.Colonne.Add(new Colonna("preferenzaG", "", DimColG, TipoAllineamento.SINISTRA, false, 8, false, TipoColonna.TESTO));
                tabellaDati_2.Colonne.Add(new Colonna("CatSFDR", "", DimColCatSFDR, TipoAllineamento.CENTRATO, false, 9, false, TipoColonna.TESTO));
                tabellaDati_2.Colonne.Add(new Colonna("ESGRating", "", DimColRating, TipoAllineamento.CENTRATO, false, 8, false, TipoColonna.IMMAGINE));
                tabellaDati_2.Colonne.Add(new Colonna("Controvalore", "", DimColControvalore, TipoAllineamento.DESTRA, false, 8, false, TipoColonna.TESTO));
                tabellaDati_2.SaltoPaginaForzato = true;
                tabellaDati_2.SezioneDiProvenienza = "FD182";
                tabellaDati_2.Draw(datiTabellaDati_2, document);
                //tabellaDati_2.DrawESG(datiTabellaDati_2, document);
                posizioneY = (tabellaDati_2.AltezzaCella * datiTabellaDati_2.table.Rows.Count) + 20;

                // TOTALE
                DataTable dtTotale = new DataTable();
                dtTotale.Columns.Add("Des");
                dtTotale.Columns.Add("Tot");
                decimal value = Convert.ToDecimal(decTotale);
                dtTotale.Rows.Add("TOTALE " + portafoglio.ToUpper(), value.ToString("C2"));
                Tabella tabellaTot = new Tabella(document.getMargineLeft(), document.getLastPos() + posizioneY + 33, 30, -2, 0.2F);
                DatiTabella datiTabellaTot = new DatiTabella();
                tabellaTot.Header = false;
                tabellaTot.Colonne.Add(new Colonna("Des", "", DimPrimaColonnaTotale, TipoAllineamento.SINISTRA, true, 8, true, TipoColonna.TESTO));
                tabellaTot.Colonne.Add(new Colonna("Tot", "", DimColControvalore, TipoAllineamento.DESTRA, true, 8, true, TipoColonna.TESTO));
                datiTabellaTot.table = dtTotale;
                tabellaTot.Draw(datiTabellaTot, document);
                document.setLastPos(2);
                document.addPage();

            }
        }

        void InsertPALLINI_ESG(DocumentPDF document, float X, float Y)
        {

            System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            var imageE = new ceTe.DynamicPDF.PageElements.Image(System.IO.Path.Combine(CACHE.pathGraf,"logo-ambientale.png"), X, Y, 0.2F);
            var imageS = new ceTe.DynamicPDF.PageElements.Image(System.IO.Path.Combine(CACHE.pathGraf,"logo-sociale.png"), X + 51, Y, 0.2F);
            var imageG = new ceTe.DynamicPDF.PageElements.Image(System.IO.Path.Combine(CACHE.pathGraf,"logo-governance.png"), X + 102, Y, 0.2F);
            document.getCurrentPage().Elements.Add(imageE);
            document.getCurrentPage().Elements.Add(imageS);
            document.getCurrentPage().Elements.Add(imageG);

        }
        protected int calculateWidth()
        {
            int tot = 0;
            tot += widthImporti;
            tot += widthImporti;
            tot += widthImporti;
            tot += widthImporti;
            tot += widthImporti;
            tot += widthImporti;
            tot += widthImporti;
            return tot;

        }

        #endregion

    }
}