using ceTe.DynamicPDF;
using ceTe.DynamicPDF.Merger;
using ceTe.DynamicPDF.PageElements;
using ceTe.DynamicPDF.Text;
using PDFGenerator.BusinessLayer;
using PDFGenerator.Presentation.Section.Tables;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;
using PDFGenerator.BusinessLayer.DataSection;
using System.Drawing;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Dundas.Charting.WebControl;
using System.Web.UI;
using System.Runtime.CompilerServices;



namespace PDFGenerator.Presentation.Section
{
    public class S187 : ISezione
    {
        private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        string ambiente = CACHE.AMBIENTE;
        int circle_size = CACHE.circle_size;
        string Titolo = string.Empty;
        string _testotitolo;
        string _testointroduttivo;
        string _testoAlternativo;

        public string TestoIntroduttivo
        {
            get
            {
                return _testointroduttivo;
            }
            set
            {
                _testointroduttivo = value;
            }
        }

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

        public string TestoTitolo
        {
            get
            {
                return _testotitolo;
            }
            set
            {
                _testotitolo = value;
            }
        }

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

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

        /// <summary>
        /// Analisi Portafoglio SFDR e Taxonomy
        /// </summary>
        /// <param name="dataThread"></param>
        public void writeSezione(DataThread dataThread)
        {
            dataThread.SETDATA();
            logger.Info("Inizio sezione S187 metodo writeSezione " + dataThread.CodiceFiscale);
            DocumentPDF document = dataThread.DocumentPDF;
            DSS187AnalisPortafoglioSFDRTaxonomy set = (DSS187AnalisPortafoglioSFDRTaxonomy)dataThread.Data.DatiSezione;
            DatiTabella datitabSFRD = new DatiTabella();
            DatiTabella datitabTAX = new DatiTabella();
            Tabella tab = new Tabella(document.getMargineLeft(), document.getLastPos());
            float riferimentoTabella1 = 90;
            float riferimentoTabella2 = 90;

            int posizioneCerchio1_Y = 240;
            int posizioneCerchio2_Y = 380;

            DataTable SFRD = GetDati(set, "SFDR");
            DataTable TAX = GetDati(set, "Taxonomy");
            DataTable CERCHI = set.Tables[1];

            datitabSFRD.table = SFRD;
            datitabTAX.table = TAX;

            if (dataThread.Rete == "W")
                _testointroduttivo = _testointroduttivo.Replace("$/Banca/$", "la Società");
            else
                _testointroduttivo = _testointroduttivo.Replace("$/Banca/$", "la Banca");

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

            // SEZIONE "SFDR"
            document.setLastPos(2);
            InsertIntestazioneTabella(document, "Analisi SFDR", document.getLastPos(), 90);
            riferimentoTabella1 = WriteDati(document, datitabSFRD, document.getLastPos(), 100, 25);
            //if (ambiente != "SVILUPPO") riferimentoTabella1 -= 10;
            InsertTotaleTabella(document, document.getLastPos() + 43);

            // SEZIONE "TAX"
            document.setLastPos(2);
            InsertIntestazioneTabella(document, "Analisi Taxonomy", document.getLastPos() + 20, 90);
            //document.setLastPos(2);
            riferimentoTabella2 = WriteDati(document, datitabTAX, document.getLastPos() + 13, 100, 25);
            //if (ambiente != "SVILUPPO") riferimentoTabella2 -= 10;

            InsertTotaleTabella(document, document.getLastPos() + 50);

            //// controllo se eiste la tabella TEMP delle immagini 
            if (!System.IO.Directory.Exists(CACHE.pathGrafTemp))
            {
                System.IO.Directory.CreateDirectory(CACHE.pathGrafTemp);
            }

            // CERCHI
            //string nomefileOutput = "";
            bool primo = false;
            int c = 0;
            foreach (DataRow item in CERCHI.Rows)
            {
                string imageIN = item["PathImage"].ToString();
                decimal Valore1 = Convert.ToDecimal(item["Valore"]);
                decimal Valore2 = Convert.ToDecimal(item["ValoreDiCui"]);

                FormatNum conv = new FormatNum();
                conv.AggiornaValoriCerchi(ref Valore1, ref Valore2);

                System.Drawing.Color colorBackground = System.Drawing.Color.FromArgb(255, 255, 255);
                System.Drawing.Color colorForeControl = System.Drawing.Color.DimGray;
                System.Drawing.Color colorBar1 = (System.Drawing.Color)item["ColoreValore"];
                System.Drawing.Color colorBar2 = (System.Drawing.Color)item["ColoreValoreDiCui"];

                var bm = CreateImage(imageIN, Valore1, 420,
                    primo == false ? posizioneCerchio1_Y : posizioneCerchio2_Y, 450,
                    Valore2, colorBackground, colorForeControl, colorBar1, colorBar2,
                    primo == false ? (int)riferimentoTabella1 * 3 : (int)riferimentoTabella2 * 3);

                ceTe.DynamicPDF.PageElements.Image img_ = new
                    ceTe.DynamicPDF.PageElements.Image(bm, 450,
                    primo == false ? posizioneCerchio1_Y : posizioneCerchio2_Y);
                img_.SetDpi(CACHE.resolution, CACHE.resolution);
                img_.ID = $"S187_CIRCLE_{c}";

                document.getCurrentPage().Elements.Add(img_);

                var imge = (ceTe.DynamicPDF.PageElements.Image)document.getCurrentPage().Elements.GetPageElementByID(img_.ID);
                float x = imge.X;
                float y = imge.Y;



                primo = true;
                c++;
            }

            logger.Info("Fine sezione S187 metodo writeSezione " + dataThread.CodiceFiscale);
            document.addPage();
        }


        DataTable GetDati(DSS187AnalisPortafoglioSFDRTaxonomy set, string tabella)
        {
            return set.Tables[0].AsEnumerable().Where(row => row.Field<String>("Tabella") == tabella).CopyToDataTable();
        }

        Bitmap CreateImage(
            string ImageIN,
            decimal ValPercentuale,
            long PosX,
            long PosY,
            long Scale,
            decimal ValPercentuale2,
            System.Drawing.Color Color1,
            System.Drawing.Color Color2,
            System.Drawing.Color Color3,
            System.Drawing.Color Color4,
            int Altezza)
        {
            //Graphics g = null;
            System.Drawing.Color colorBackground = Color1; //  SystemColors.Control;
            System.Drawing.Color colorForeControl = Color2; // System.Drawing.Color.DimGray;
            System.Drawing.Color colorBar1 = Color3;
            System.Drawing.Color colorBar2 = Color4;
            using (BarreCircolari _bc = new BarreCircolari(Altezza, Altezza, "", new System.Drawing.Font("Verdana", 10), colorBackground,
                                                           colorForeControl, colorBar1, colorBar2, new System.Drawing.Rectangle(0, 0, Altezza, Altezza),
                                                           (Int32)ValPercentuale, (Int32)ValPercentuale2))
            {
                //if (ambiente != "SVILUPPO") _bc.scaleImage = 0.90f;
                _bc.pathImmagine = ImageIN;
                _bc.OnPaintNew(ImageIN, "", false);
                Bitmap outAnello = _bc.outImmagine;
                //if (ambiente != "SVILUPPO")
                //{
                    int w = circle_size, h = circle_size;
                    Bitmap nuova = new Bitmap(w, h);
                    System.Drawing.Image img = (System.Drawing.Image)outAnello;

                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new Point(0, 0), new Size(w, h));
                    using (var g1 = Graphics.FromImage(nuova))
                    {
                        g1.DrawImage(img, rect);
                    }
                    return nuova;
                //}
                //else
                //    return outAnello;
            }
        }

        void InsertIntestazioneTabella(DocumentPDF document, string Caption, float DeltaX, float DeltaY)
        {
            Tabella tabellaDatiSFRD_Tax = new Tabella(document.getMargineLeft(), DeltaX, DeltaY, -2, 0.2F);
            document.setLastPos(2);
            DatiTabella datiTabellaInstestazione = new DatiTabella();
            DataTable dtSFDR_INTESTAZIONE = new DataTable();
            dtSFDR_INTESTAZIONE.Columns.Add("Caption", typeof(System.String));
            dtSFDR_INTESTAZIONE.Columns.Add("Peso", typeof(System.String));

            datiTabellaInstestazione.table = dtSFDR_INTESTAZIONE;
            dtSFDR_INTESTAZIONE.Rows.Add(Caption, "PESO (%)");
            tabellaDatiSFRD_Tax.Header = false;
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("Caption", "", 295, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaDatiSFRD_Tax.Colonne.Add(new Colonna("Peso", "", 70, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaDatiSFRD_Tax.Draw(datiTabellaInstestazione, document);
        }

        float WriteDati(DocumentPDF document, DatiTabella datiTab, float x, float y, Int32 rowDim)
        {
            Tabella tabellaDati = new Tabella(document.getMargineLeft() + 10, x, y, -2, 0.2F);
            datiTab.SetRowDim(rowDim);
            datiTab.setIsLinee(2);
            document.setLastPos(2);
            tabellaDati.Header = false;
            tabellaDati.Colonne.Add(new Colonna("NomeFileCerchio", "", 10, TipoAllineamento.CENTRATO, false, 10, false, TipoColonna.IMMAGINE));
            tabellaDati.Colonne.Add(new Colonna("Descrizione", "", 250, TipoAllineamento.SINISTRA, false, 9, false, TipoColonna.TESTO));
            tabellaDati.Colonne.Add(new Colonna("ValMostrato", "", 90, TipoAllineamento.DESTRA, false, 9, false, TipoColonna.TESTO)); //45
            tabellaDati.Draw(datiTab, document);
            return tabellaDati.AltezzaTabella;
        }

        void InsertTotaleTabella(DocumentPDF document, float posizione)
        {

            Tabella tabellaTOT_SFRD_Tax = new Tabella(document.getMargineLeft(), posizione, 50, -2, 0.2F);
            document.setLastPos(2);
            DatiTabella datiTabellaTotale = new DatiTabella();
            DataTable dtTotle = new DataTable();
            dtTotle.Columns.Add("TOT", typeof(System.String));
            dtTotle.Columns.Add("Peso", typeof(System.String));

            datiTabellaTotale.table = dtTotle;
            dtTotle.Rows.Add("TOTALE", "100,00");
            tabellaTOT_SFRD_Tax.Header = false;
            tabellaTOT_SFRD_Tax.Colonne.Add(new Colonna("TOT", "", 270, TipoAllineamento.SINISTRA, true, 9, true, TipoColonna.TESTO));
            tabellaTOT_SFRD_Tax.Colonne.Add(new Colonna("Peso", "", 90, TipoAllineamento.DESTRA, true, 9, true, TipoColonna.TESTO));
            tabellaTOT_SFRD_Tax.Draw(datiTabellaTotale, document);
        }
    }

}