using System;
using System.Collections.Generic;
using System.Linq;
using Consulenza.ExternalServices;
using Consulenza.ReportWriter.Business;
using Consulenza.ReportWriter.Business.Entity;
using Consulenza.ReportWriter.Business.OBJ_PDF;
using System.Data;
using Consulenza.ReportWriter.Business.Headers;
using Consulenza.ReportCommon;

namespace Consulenza.ReportWriter.Manager.Section.Immobiliare.MonitoraggioNucleo
{
    /// <summary>   
    /// Patrimonio Immobiliare Stima delle imposte (id 41) Fabbricati
    /// </summary>
    //public class S9 : Monitoraggio.S9 { 
    //    public S9(EnvironmentFacade environmentFacade, int idSection)
    //        : base(environmentFacade, idSection)
    //    {

    //    }
    //}

    public class S9 : Entity.Section
    {
        public S9(EnvironmentFacade environmentFacade, int idSection)
            : base(environmentFacade, idSection)
        {
            try
            {
                Draw();
            }
            catch (Exception ex)
            {
                SectionLogger.Write("S9", ex.Message, SectionLoggerMessageLevel.E, EnvironmentFacade.ReportEnvironment);
            }
        }

        /// <summary>
        /// Scheda2. Patrimonio immobiliare –Sintesi (distribuzione del patrimonio complessivo del cliente diviso per tipologia e cointestatari)
        /// </summary>
        protected override sealed void Draw()
        {
            //MonitoraggioImmobiliarePageHeaderManager.DrawIndexPage(Elements, 210);
            var width = 345;
            int xSx = 35;
            int xDx = 400;

            var dataset = GetDataSet();
            var _text = dataset.Tables[0];
            var _data = dataset.Tables[1];

            List<int> indiciPagina = _data.AsEnumerable()
                .Select(z => z.Field<int>("idREP_IMM_MON_Pagina")).Distinct().ToList();

            foreach (var indice in indiciPagina)
            {
                string paragrafoSx = "";
                string paragrafoDx = "";
                string labelSx = "";
                string labelDx = "";
                string testo = "";
                AddElement(new PagePDF(PagePDF.PagePDFType.Generic));

                FormattedTextAreaPDF testoSx = null;
                FormattedTextAreaPDF testoDx = null;
                if (_text.AsEnumerable().Where(x => x.Field<int>("idREP_IMM_MON_Pagina") == indice).Count() > 0)
                {
                    foreach (var row in _text.AsEnumerable().Where(x => x.Field<int>("idREP_IMM_MON_Pagina") == indice).ToList())
                    {
                        testo = "";
                        labelSx = "";
                        labelDx = "";

                        testo = "<B>" + row.Field<string>("titolo") + "</B><br><br>";

                        for (int i = 1; i <= 15; i++)
                            testo += row.Field<string>("testo" + i) != null ? "<p>" + row.Field<string>("testo" + i) + "</p><br>" : "";

                        // SPLIT PARAGRAFO
                        if (testo.Contains("SPLITPARAGRAPH"))
                        {
                            var listaSplit = StringExtensions.SplitAtOccurence(testo, "SPLITPARAGRAPH", 1);
                            labelSx = listaSplit[0] + "</p>";
                            labelDx += "<br><p>";
                            listaSplit.RemoveRange(0, 1);
                            foreach (var stringa in listaSplit)
                                labelDx += stringa;

                            paragrafoSx += labelSx;
                            paragrafoDx += labelDx;
                        }
                        else
                        {
                            if (row.Field<string>("posizione").Equals("R"))
                                paragrafoDx += testo;
                            else
                                paragrafoSx += testo;
                        }
                    }
                    AddElement(new ResetterYPDF(60));
                    testoSx = new FormattedTextAreaPDF(paragrafoSx, 35, width) { FontSize = 9, FontColor = ColorPDF.Immobiliare_Grigio_TestoStandard, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify };
                    testoDx = new FormattedTextAreaPDF(paragrafoDx, 410, width) { FontSize = 9, FontColor = ColorPDF.Immobiliare_Grigio_TestoStandard, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify };
                    AddElement(testoSx);
                    AddElement(new ResetterYPDF(65));
                    AddElement(testoDx);

                    if (getNota(_text.Rows[0]).Length > 0)
                    {
                        var nota = new FormattedTextAreaPDF(getNota(_text.Rows[0]), 60, width) { Y = EnvironmentFacade.RendererFacade.YLowerLimit - 18, FontSize = 6, FontColor = ColorPDF.Nero, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify };
                        AddElement(nota);
                    }



                }
                if (_data.AsEnumerable().Where(x => x.Field<int>("idREP_IMM_MON_Pagina") == indice).Count() > 0
                    && _data.AsEnumerable().Where(x => x.Field<int>("idREP_IMM_MON_Pagina") == indice && x.Field<string>("descrizioneImage") != null).Count() > 0)
                {
                    foreach (var image in _data.AsEnumerable().Where(x => x.Field<int>("idREP_IMM_MON_Pagina") == indice).ToList())
                    {
                        string titoloStatico = image.Field<string>("descrizioneImage") + "<br>";
                        var titoloGrafico = new FormattedTextAreaPDF(titoloStatico, 50, width * 2) { FontSize = 10, FontColor = ColorPDF.Immobiliare_Grigio_TitoloPiccolo, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify };
                        var cartina = new ImagePDF(35, 0.465F, image.Field<string>("bytesImage") + ".jpeg");
                        AddElement(new ResetterYPDF(50));
                        AddElement(titoloGrafico);
                        AddElement(cartina);
                        if (getNota(image).Length > 0)
                        {
                            string testoNota = getNota(image);
                            IEnumerable<DataRow> rows = _data.AsEnumerable()
                            .Where(x => x.Field<string>("nota1") != null);
                            int count = testoNota.Replace("<br>", "\t").Count(x => x == '\t');
                            var nota = new FormattedTextAreaPDF(testoNota, 60, width) { Y = EnvironmentFacade.RendererFacade.YLowerLimit - 13 - (5 * count), FontSize = 6, FontColor = ColorPDF.Nero, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify };
                            AddElement(nota);
                        }
                    }


                }
            }
        }

        protected string getNota(DataRow row)
        {
            string nota = "";
            for (int i = 1; i <= 5; i++)
                if (row.Field<string>("nota" + i) != null)
                    nota += row.Field<string>("nota" + i) + "<br>";
            return nota;
        }


        /// <summary>
        /// Recupera i dati necessari alla Section restituendo un DataTable.
        /// </summary>
        /// <returns></returns>
        protected override DataTable GetDataTable()
        {
            return null;
        }

        /// <summary>
        /// Recupera i dati necessari alla Section restituendo un DataSet.
        /// </summary>
        /// <returns></returns>
        protected override DataSet GetDataSet()
        {
            #region Definizione dei parametri

            var parametri = new List<Parametro>
            {
                new Parametro
                {
                    Direction = ParameterDirection.Input,
                    DbType = DbType.Int32,
                    ParameterName = "IdReport",
                    //Value = EnvironmentFacade.ReportEnvironment.ReportId
                    // Impostato a -1 per prendere quello dei Report Monitoraggio Immobiliare
                    Value = EnvironmentFacade.ReportEnvironment.ReportId - 1
                },
                new Parametro
                {
                    Direction = ParameterDirection.Input,
                    DbType = DbType.Int32,
                    ParameterName = "IdSezione",
                    Value = IdSection
                }
            };

            #endregion

            return DataAccess.ExecuteDataSetStoredProcedure(DBProvider.SqlServerReportModeler, "REP_Monitoraggio_Immobiliare_S9_Mappa_Rischio_Sismico", parametri);
        }
    }

}
