using Consulenza.ReportWriter.Business.Entity;
using Consulenza.ReportWriter.Business.OBJ_PDF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Consulenza.ReportWriter.Business.Headers
{
    public static class MonitoraggioImmobiliarePageHeaderManager
    {

        public static void modifyHeader(int sectionId, string stringValue)
        {
            PageHeader header = GetHeader(sectionId);
            header.ChapterName = stringValue;
        }
        public static PageHeader GetHeader(int sectionId)
        {
            return headers.Where(x => x.SectionId == sectionId).FirstOrDefault();
        }

        private static List<PageHeader> headers = new List<PageHeader>()
        {
            new PageHeader(201, 0, "S1", null, 0, 0, 0, null, null),
            new PageHeader(202, 202, "S2", null, 0, 0, 0, null, "Indice"),
            // Originale impostato a 0 - Modificato  impostato a -4
            new PageHeader(203, 202, "S3", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Introduzione"),
            new PageHeader(204, 202, "S4", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Focus Italia"),
            new PageHeader(205, 202, "S5", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Focus principali città"),
            new PageHeader(206, 202, "S6", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Focus territoriale"),
            new PageHeader(207, 202, "S7", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Aspettative future"),
            new PageHeader(208, 202, "S8", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Ambiti di opportunità"),
            new PageHeader(209, 202, "S9", "contestoescenari.png", 32, 32, -4, "Contesto e scenari", "Mappa del rischio sismico in Italia"),

            new PageHeader(213, 213, "S23", null, 0, 0, 0, null, "Indice"),
            // Originale impostato a -1 - Modificato  impostato a
            new PageHeader(210, 213, "S10", "fiscalita.png", 25, 32, -1, "Fiscalità", "Principali novità derivanti dalla Legge di Bilancio " + DateTime.Now.Year),
            new PageHeader(211, 213, "S11", "fiscalita.png", 25, 32, -1, "Fiscalità", "Scadenzario"),

            new PageHeader(231, 231, "S25", null, 0, 0, 0, null, "Indice"),
            new PageHeader(221, 231, "S13", "mappatura.png", 32, 32, -2, "Mappatura strategica del patrimonio immobiliare",
                "Sintesi"),
            new PageHeader(222, 231, "S14", "mappatura.png", 32, 32, -2, "Mappatura strategica del patrimonio immobiliare",
                "Distribuzione geografica"),
            new PageHeader(234, 231, "S28", "mappatura.png", 32, 32, -2, "Mappatura strategica del patrimonio immobiliare",
                "Estero"),

            new PageHeader(232, 232, "S26", null, 0, 0, 0, null, "Indice"),
            new PageHeader(223, 232, "S15", "analisidelrischio.png", 32, 32, -2, "Analisi del rischio", "Diversificazione del patrimonio complessivo"),
            new PageHeader(224, 232, "S16", "analisidelrischio.png", 32, 32, -2, "Analisi del rischio", "Sintesi"),
            new PageHeader(225, 232, "S17", "analisidelrischio.png", 32, 32, -2, "Analisi del rischio", "Dettaglio"),

            new PageHeader(233, 233, "S27", null, 0, 0, 0, null, "Indice"),
            new PageHeader(226, 233, "S18", "dettaglio.png", 50, 28, 0, "Dettaglio immobili", "Immobili ad uso abitativo e strumentali"),
            new PageHeader(227, 233, "S19", "dettaglio.png", 50, 28, 0, "Dettaglio immobili", "Immobili d’investimento"),
            new PageHeader(228, 233, "S20", "dettaglio.png", 50, 28, 0, "Dettaglio immobili", "Immobili con finalità non specificata"),
            new PageHeader(229, 233, "S21", "detagglio2.png", 32, 32, 0, "Dettaglio immobili", "Immobili con diritti e/o quote di diritto variate"),
            new PageHeader(230, 233, "S22", "dettaglio3.png", 32, 32, 0, "Dettaglio immobili", "Anomalie catastali"),
            
            new PageHeader(214, 214, "S24", null, 0, 0, 0, null, "Indice"),
            new PageHeader(212, 214, "S12", "Glossario.png", 32, 32, -2, "Glossario", null),


            //new PageHeader(227, 227, "S27", null, 0, 0, 0, null, "Indice"),
            
        };

        public static List<int> AllSections { get; set; }

        private static List<IndexPageRecord> GetIndexData()
        {
            List<IndexPageRecord> results = new List<IndexPageRecord>();
            foreach (int section in AllSections)
            {
                if (section >= 202 && section <= 209)
                {
                    results.Add(new IndexPageRecord() { Image = "contestoescenari.png", Name = "Contesto e scenari", IndexPageSectionId = 203 });
                }
                else if (section >= 210 && section <= 211 || section == 213)
                {
                    results.Add(new IndexPageRecord() { Image = "fiscalita.png", Name = "Fiscalità", IndexPageSectionId = 210 });
                }
                else if (section >= 221 && section <= 222 || section == 231)
                {
                    results.Add(new IndexPageRecord() { Image = "mappatura.png", Name = "Mappatura strategica del patrimonio immobiliare", IndexPageSectionId = 231 });
                }
                else if (section >= 223 && section <= 225 || section == 232)
                {
                    results.Add(new IndexPageRecord() { Image = "analisidelrischio.png", Name = "Analisi del rischio", IndexPageSectionId = 232 });
                }
                else if (section >= 226 && section <= 229 || section == 233)
                {
                    results.Add(new IndexPageRecord() { Image = "dettaglio.png", Name = "Dettaglio immobili", IndexPageSectionId = 233 });
                }

                else if (section== 230)
                {
                    results.Add(new IndexPageRecord() { Image = "warning.png", Name = "Dettaglio immobili", IndexPageSectionId = 233 });
                }

                else if (section == 212 || section == 214)
                {
                    results.Add(new IndexPageRecord() { Image = "Glossario.png", Name = "Glossario", IndexPageSectionId = 214 });
                }
            }
            results = results.GroupBy(x => x.Name).Select(x => x.First()).ToList();
            return results;
        }

        public static void DrawIndexPage(List<ObjectPDF> elements, int sectionId)
        {
            var indexRecords = GetIndexData();
            int yPosition = 100;
            foreach (var indexRecord in indexRecords)
            {
                int imageWidth = 20;
                int imageHeight = 20;
                int imageyOffset = 0;
                int imageXPosition = 0;
                int textXPosition = 20;
                switch (indexRecord.Image)
                {
                    case "contestoescenari.png":
                        imageWidth = 37;
                        imageHeight = 30;
                        imageXPosition = 14;
                        textXPosition = 95;
                        //yPosition = 90;
                        break;

                    case "fiscalita.png":
                        imageWidth = 20;
                        imageHeight = 28;
                        imageXPosition = 22;
                        textXPosition = 96;
                        //yPosition = 95;
                        break;

                    case "mappatura.png":
                        imageWidth = 31;
                        imageHeight = 24;
                        imageXPosition = 15;
                        textXPosition = 92;
                        break;

                    case "analisidelrischio.png":
                        imageWidth = 35;
                        imageHeight = 29;
                        imageXPosition = 15;
                        textXPosition = 94;
                        break;

                    case "dettaglio.png":
                        imageWidth = 44;
                        imageHeight = 31;
                        imageyOffset = -2;
                        imageXPosition = 11;
                        textXPosition = 94;
                        break;

                    case "Glossario.png":
                        imageWidth = 31;
                        imageHeight = 31;
                        imageyOffset = -2;
                        imageXPosition = 17;
                        textXPosition = 94;
                        break;
                }

                elements.Add(new ImagePDF(imageXPosition, 0, indexRecord.Image) { Width = imageWidth, Height = imageHeight, DeltaY = yPosition + imageyOffset });
                elements.Add(new RectanglePDF(20, (indexRecord.IndexPageSectionId != sectionId ? 45 : 415), new ColorPDF(153, 148, 133)) { X = 61, DeltaY = yPosition + 4, AutoIncrementYWritable = false });
                elements.Add(new FormattedTextAreaPDF(FormatIndexPageString(indexRecord.Name, indexRecord.IndexPageSectionId == sectionId), textXPosition) { DeltaY = yPosition + 4, AutoIncrementYWritable = false, FontSize = 15 });
                var guid = Guid.NewGuid().ToString();
                elements.Add(new FormattedTextAreaPDF("<font color='5C5951'>pag.</font>", 660) { DeltaY = yPosition + 4, AutoIncrementYWritable = false, FontSize = 15 });
                elements.Add(new FormattedTextAreaPDF(string.Format("<font><b><font color='AE9651'>{0}</font></b>", guid), 495) { DeltaY = yPosition + 4 - 9, AutoIncrementYWritable = false, FontSize = 15, Width = 225, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Right });
                var indexSectionId = headers.Where(x => x.ImagePath == indexRecord.Image).FirstOrDefault().BelongToIndex;
                IndexPagesPageNumbersToReplace.Add(new Tuple<int, string>(indexSectionId, guid));
                yPosition += 50;
            }
        }

        public static List<Tuple<int, string>> IndexPagesPageNumbersToReplace = new List<Tuple<int, string>>();

        private static string FormatIndexPageString(string text, bool currentSection)
        {
            string result = null;
            if (!currentSection)
            {
                result = string.Format("<b><font color='FFFFFF'>{0}<font></b><font color='5C5951'>{1}</font>", text[0].ToString(), text.Remove(0, 1));
            }
            else
            {
                result = string.Format("<b><font color='FFFFFF'>{0}<font></b><font color='FFFFFF'>{1}</font>", text[0].ToString(), text.Remove(0, 1));
            }
            return result;
        }
    }
}
