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

namespace Consulenza.ReportWriter.Manager.Section.Immobiliare.MonitoraggioNucleo
{
    /// <summary>
    /// Scheda 20
    /// </summary>
    public class S20 : Entity.Section
    {
        public bool bPrintAltri = false;

        public S20(EnvironmentFacade environmentFacade, int idSection)
            : base(environmentFacade, idSection)
        {
            try
            {
                Draw();
            }
            catch (Exception ex)
            {
                SectionLogger.Write("S20", 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()
        {
            var dataset = GetDataSet();
            FormattedTextAreaPDF testoPagina = null;
            FormattedTextAreaPDF notaTabella = null;

            DataTable clientiTable = dataset.Tables["clienti"];

            foreach (DataTable table in dataset.Tables)
            {
                if (table.TableName != "clienti" && table != null && table.Rows.Count > 0)
                {
                    testoPagina = new FormattedTextAreaPDF("Di seguito si rappresenta l'elenco degli immobili con finalità non specificata.", 35) { FontSize = 10, FontColor = ColorPDF.Immobiliare_Grigio_TitoloPiccolo, AutoIncrementYWritable = true, Width = 740, FixedHeight = 20 };
                    notaTabella = new FormattedTextAreaPDF("Nota(*): Il \"Reddito dominicale\" dei terreni è esposto nella colonna \"Rendita catastale\"<BR>", 60, 680) { AutoIncrementYWritable = false, Y = EnvironmentFacade.RendererFacade.YLowerLimit - 23, FontSize = 6, FontColor = ColorPDF.Nero, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify, DeltaX = 5, DeltaY = 5 };
                    var listaOggettiDaRipetere = new List<ObjectPDF> { new SpacePDF(30), testoPagina, new SpacePDF(10), notaTabella };
                    AddElement(listaOggettiDaRipetere);

                    AddElement(new RepeaterPDF(listaOggettiDaRipetere));

                    const int larghezzaTabella = 730; // massima larghezza tabella
                    const int larghezzaSingoloCliente = 65;
                    const int larghezzaCategoriaCatastale = 40;
                    const int larghezzaSuperficieStimata = 50;
                    const int larghezzaRenditaCatastale = 55;
                    const int larghezzaTotale = 70;
                    const int larghezzaParziale = 70;
                    const int tabellaFissa = larghezzaTotale + larghezzaParziale + larghezzaRenditaCatastale + larghezzaSuperficieStimata + larghezzaCategoriaCatastale;
                    //La colonna descrizione è l'unica variabile.
                    var larghezzaDescrizione = larghezzaTabella - 10 - tabellaFissa - (larghezzaSingoloCliente * System.Math.Min(clientiTable.Rows.Count, 5));


                    TablePDF tablePDF = new TablePDF(35, table)
                    {
                        Style = Style.Immobiliare,
                        RowsPadding = 3,
                        Footer = false,
                        HeaderHeight = 120,
                        MinimumRowHeight = 12,
                        ShowBorderLastLine = true
                    };

                    tablePDF.Columns.Add(new ColumnPDF("AnomaliaCatastale", 10, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Immagine, "AnomaliaCatastale", "") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, ScaleColumnTypeImage = 0.1F, DeltaYContent = 2, PaddingLeft = -10 });
                    tablePDF.Columns.Add(new ColumnPDF("descrizione", larghezzaDescrizione, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "descrizione", "Immobili con finalità non specificata") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderVerticalAlignment = VerticalAlignmentType.Basso });
                    tablePDF.Columns.Add(new ColumnPDF("codTipologiaCatasto", larghezzaCategoriaCatastale, HorizontalAlignmentType.Centrato, false, false, 7, ColumnType.Testo, "CategoriaCatastale", "Categoria catastale") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });
                    tablePDF.Columns.Add(new ColumnPDF("superficie", larghezzaSuperficieStimata, HorizontalAlignmentType.Centrato, false, false, 7, ColumnType.Testo, "SuperficieStimata", "Superficie Stimata") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });
                    tablePDF.Columns.Add(new ColumnPDF("codTipologiaCatasto", larghezzaRenditaCatastale, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "renditaCatastale", "Rendita catastale*") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

                    for (int i = 1; i <= (System.Math.Min(clientiTable.Rows.Count, 5)); i++)
                        tablePDF.Columns.Add(new ColumnPDF("cliente" + i, larghezzaSingoloCliente, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "cliente" + i, clientiTable.Rows[i - 1]["cognome"].ToString() + " " + clientiTable.Rows[i - 1]["nome"].ToString()) { HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });
                    if (bPrintAltri == true)
                        tablePDF.Columns.Add(new ColumnPDF("Parziale", larghezzaParziale, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "Parziale", "Altri") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

                    tablePDF.Columns.Add(new ColumnPDF("valoreImmobileTotale", larghezzaTotale, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "Totale", "Valore stimato<br>totale (€)") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

                    for (int k = 0; k < table.Rows.Count; k++)
                    {
                        //if (table.Rows[k].Field<string>("AnomaliaCatastale") == "true")
                        //    tablePDF.Cells[2, k].Value = "Warning.png";
                        //else
                        //    tablePDF.Cells[2, k].Value = "";

                        // Old Version 20170919
                        //if (table.Rows[k].Field<string>("CategoriaCatastale").All(Char.IsDigit))
                        //{
                        //    int column = table.Rows[k].Table.Columns["CategoriaCatastale"].Ordinal;
                        //    tablePDF.Cells[column, k].Value = "-";
                        //}
                        //else if (table.Rows[k].Field<string>("CategoriaCatastale").Equals("F/5"))
                        //{
                        //    tablePDF.Cells[3, k].Value = "n.d.";
                        //    tablePDF.Cells[4, k].Value = "n.d.";
                        //}
                        //if (table.Rows[k].Field<string>("CategoriaCatastale").Substring(0, 1) == "M")
                        //    tablePDF.Cells[2, k].Value = "-";
                        //else
                        //    tablePDF.Cells[2, k].Value = table.Rows[k].Field<string>("CategoriaCatastale");

                        //if (table.Rows[k].Field<string>("renditaCatastale").Equals("") || Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")) == 0)
                        //    tablePDF.Cells[4, k].Value = "-";
                        //else
                        //    tablePDF.Cells[4, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")).ToString());

                        if (table.Rows[k].Field<string>("CategoriaCatastale").All(Char.IsDigit))
                        {
                            int column = table.Rows[k].Table.Columns["CategoriaCatastale"].Ordinal;
                            tablePDF.Cells[column, k].Value = "-";
                        }
                        else
                        {
                            if (table.Rows[k].Field<string>("CategoriaCatastale").Equals("F/5"))
                            {
                                tablePDF.Cells[3, k].Value = "n.d.";
                                tablePDF.Cells[4, k].Value = "n.d.";
                            }
                            else
                            {
                                if (table.Rows[k].Field<string>("CategoriaCatastale").Substring(0, 1) == "M")
                                    tablePDF.Cells[2, k].Value = "-";
                                else
                                    tablePDF.Cells[2, k].Value = table.Rows[k].Field<string>("CategoriaCatastale");
                            }
                        }

                        if (table.Rows[k].Field<string>("renditaCatastale").Equals("") || Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")) == 0)
                            if (table.Rows[k].Field<string>("CategoriaCatastale").Equals("F/5"))
                                tablePDF.Cells[4, k].Value = "n.d.";
                            else
                                tablePDF.Cells[4, k].Value = "-";
                        else
                            tablePDF.Cells[4, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")).ToString());


                        for (int i = 1; i <= (System.Math.Min(clientiTable.Rows.Count, 5)); i++)
                        {
                            if (Convert.ToDecimal(table.Rows[k].Field<string>("cliente" + i)) <= 0)
                                tablePDF.Cells[4 + i, k].Value = "-";
                            else
                                tablePDF.Cells[4 + i, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("cliente" + i)).ToString());
                        }

                        if (bPrintAltri != true)
                        {
                            if (Convert.ToDecimal(table.Rows[k].Field<string>("Totale")) <= 0)
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = "-";
                            else
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("Totale")).ToString());
                        }
                        else
                        {
                            if (Convert.ToDecimal(table.Rows[k].Field<string>("Parziale")) <= 0)
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = "-";
                            else
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("Parziale")).ToString());

                            if (Convert.ToDecimal(table.Rows[k].Field<string>("Totale")) <= 0)
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 6, k].Value = "-";
                            else
                                tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 6, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("Totale")).ToString());
                        }

                        // Modificato il 22/09/2017 in attesa di sapere cosa vuole Carnevali Paolo                           
                        if (table.Rows[k].Field<string>("AnomaliaCatastale") != "")
                            tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = "n.d.";

                        // Fabbricati e Terreni
                        if (table.Rows[k].Field<string>("SuperficieStimata") == null || table.Rows[k].Field<string>("SuperficieStimata") == "0,0000000")
                            tablePDF.Cells[3, k].Value = "-";
                        else
                        {
                            if (table.Rows[k].Field<string>("SuperficieStimata").Equals(""))
                                tablePDF.Cells[3, k].Value = "n.d.";
                            else
                            {
                                if (table.Rows[k].Field<string>("tipoImmobile").Equals("F"))
                                    tablePDF.Cells[3, k].Value = Helper.FormatInteger(Helper.FormatDecimal(table.Rows[k].Field<string>("SuperficieStimata"), 0)) + " mq";
                                else
                                    tablePDF.Cells[3, k].Value = Helper.FormatDecimal(table.Rows[k].Field<string>("SuperficieStimata"), 3) + " ha";
                            }
                        }

                        
                        //if (table.Rows[k].Field<string>("tipoImmobile").Equals("T") )
                        //{
                        //    if (table.Rows[k].Field<string>("SuperficieStimata").Equals(""))
                        //        tablePDF.Cells[3, k].Value = "n.d.";
                        //    else
                        //        tablePDF.Cells[3, k].Value = Helper.FormatDecimal(table.Rows[k].Field<string>("SuperficieStimata"), 3) + " ha";         
                        //}
                        //else
                        //    tablePDF.Cells[3, k].Value = Helper.FormatDecimal(table.Rows[k].Field<string>("SuperficieStimata"), 0) + " mq";

                    }

                    AddElement(tablePDF);

                    if (table.TableName != "clienti" && table != null && table.Rows.Count > 0)
                    {
                        List<string> legenda = table.AsEnumerable().Select(z => z.Field<string>("AnomaliaCatastale")).Distinct() != null ?
                            table.AsEnumerable().Select(z => z.Field<string>("AnomaliaCatastale")).Distinct().ToList() : null;
                        if (legenda != null && legenda.Count > 0)
                            AddLegend(legenda);
                    }

                }
            }

        }


        /// <summary>
        /// Recupera i dati necessari alla Section restituendo un DataSet.
        /// </summary>
        /// <returns></returns>
        protected override DataSet GetDataSet()
        {
            DataSet data = new DataSet();
            data.Tables.Add(new DataTable("clienti"));    //  U
            data.Tables["clienti"].Columns.Add(new DataColumn("nome", typeof(string)));
            data.Tables["clienti"].Columns.Add(new DataColumn("cognome", typeof(string)));
            data.Tables.Add(new DataTable("1"));    //  U
            data.Tables.Add(new DataTable("2"));    //  S
            data.Tables.Add(new DataTable("3"));    //  I
            data.Tables.Add(new DataTable("4"));    //  NS

            #region Definizione dei parametri

            var parametri = new List<Parametro>
            {
                new Parametro
                {
                    Direction = ParameterDirection.Input,
                    DbType = DbType.Int64,
                    ParameterName = "ChiaveNucleo",
                    Value = EnvironmentFacade.ReportEnvironment.NucleoImmobiliare.ChiaveNucleo
                }
            };


            #endregion

            var dsOriginal = DataAccess.ExecuteDataSetStoredProcedure(DBProvider.SqlServerConsulenzaUnica, "REP_ImmobiliareMonitoraggio_S_DettaglioFabbricatiNucleo", parametri);
            if (dsOriginal.Tables[0].Rows.Count == 0)
                return null;

            decimal nTotaleAltri = 0;
            for (int k = 0; k < dsOriginal.Tables[0].Rows.Count; k++)
                nTotaleAltri = nTotaleAltri + Convert.ToDecimal(dsOriginal.Tables[0].Rows[k]["parziale"]);

            if (nTotaleAltri > 0)
                bPrintAltri = true;

            List<string> clientiList = dsOriginal.Tables[1].AsEnumerable()
                .Select(z => z.Field<string>("Cliente")).Distinct().ToList();

            foreach (var cliente in clientiList)
            {
                data.Tables["clienti"].Rows.Add(cliente);
            }

            if (dsOriginal.Tables[0].AsEnumerable().Where(x => x.Field<int>("ordinamento") == 4).Count() > 0)
            {
                DataTable copyDataTable = dsOriginal.Tables[0].AsEnumerable().GroupBy(x => x.Field<string>("idImmobileCatasto")).Select(x => x.First()).Where(x => x.Field<int>("ordinamento") == 4).OrderBy(x => x.Field<string>("tipoimmobile")).ThenBy(x => x.Field<int>("ordfabbricati")).CopyToDataTable();
                //.GroupBy(y => y.Field<string>("idImmobilecatasto"), (key) => new { idImmobilecatasto = key, Cars = g.ToList() })).CopyToDataTable();
                for (int i = 0; i < copyDataTable.Rows.Count; i++)
                {
                    if (copyDataTable.Rows[i]["AnomaliaCatastale"].ToString() == "1")
                        copyDataTable.Rows[i]["AnomaliaCatastale"] = "Warning.png";
                    else
                        copyDataTable.Rows[i]["AnomaliaCatastale"] = "";

                    //copyDataTable.Rows[i]["codAnomalia"] = copyDataTable.Rows[i]["codAnomalia"] != DBNull.Value &&
                    //    copyDataTable.Rows[i]["codAnomalia"] != null ?
                    //    "Warning.png" : "";
                    //copyDataTable.Rows[i]["codAnomalia"] = copyDataTable.Rows[i]["quotaDiritto"] != DBNull.Value &&
                    //        copyDataTable.Rows[i]["quotaDiritto"] != null ?
                    //        "detagglio2.png" : copyDataTable.Rows[i]["codAnomalia"];

                    if (copyDataTable.Rows[i]["cliente1"] != DBNull.Value)
                        copyDataTable.Rows[i]["cliente1"] = Helper.FormatCurrency(copyDataTable.Rows[i]["cliente1"].ToString());
                    else
                        copyDataTable.Rows[i]["cliente1"] = "0";
                    if (copyDataTable.Rows[i]["cliente2"] != DBNull.Value)
                        copyDataTable.Rows[i]["cliente2"] = Helper.FormatCurrency(copyDataTable.Rows[i]["cliente2"].ToString());
                    else
                        copyDataTable.Rows[i]["cliente2"] = "0";
                    if (copyDataTable.Rows[i]["cliente3"] != DBNull.Value)
                        copyDataTable.Rows[i]["cliente3"] = Helper.FormatCurrency(copyDataTable.Rows[i]["cliente3"].ToString());
                    else
                        copyDataTable.Rows[i]["cliente3"] = "0";
                    if (copyDataTable.Rows[i]["cliente4"] != DBNull.Value)
                        copyDataTable.Rows[i]["cliente4"] = Helper.FormatCurrency(copyDataTable.Rows[i]["cliente4"].ToString());
                    else
                        copyDataTable.Rows[i]["cliente4"] = "0";
                    if (copyDataTable.Rows[i]["cliente5"] != DBNull.Value)
                        copyDataTable.Rows[i]["cliente5"] = Helper.FormatCurrency(copyDataTable.Rows[i]["cliente5"].ToString());
                    else
                        copyDataTable.Rows[i]["cliente5"] = "0";

                    if (copyDataTable.Rows[i]["Totale"] != DBNull.Value)
                        copyDataTable.Rows[i]["Totale"] = Helper.FormatCurrency(copyDataTable.Rows[i]["Totale"].ToString());
                    else
                        copyDataTable.Rows[i]["Totale"] = "0";

                    if (copyDataTable.Rows[i]["Parziale"] != DBNull.Value)
                        copyDataTable.Rows[i]["Parziale"] = Helper.FormatCurrency(copyDataTable.Rows[i]["Parziale"].ToString());
                    else
                        copyDataTable.Rows[i]["Parziale"] = "0";

                    if (copyDataTable.Rows[i]["renditacatastale"] != DBNull.Value)
                        copyDataTable.Rows[i]["renditacatastale"] = copyDataTable.Rows[i]["renditacatastale"];
                    else
                        copyDataTable.Rows[i]["renditacatastale"] = 0;

                    //copyDataTable.Rows[i]["ProQuota"] = Helper.FormatCurrency(copyDataTable.Rows[i]["ProQuota"].ToString());
                    //copyDataTable.Rows[i]["valoreImmobileTotale"] = Helper.FormatCurrency(copyDataTable.Rows[i]["valoreImmobileTotale"].ToString());

                }
                //copyDataTable.Rows[0]["codAnomalia"] = "Warning.png";
                var name = copyDataTable.Rows[0]["ordinamento"];
                DataTable dtCloned = copyDataTable.Clone();
                dtCloned.Columns[0].DataType = typeof(Int32);
                for (int i = 0; i < copyDataTable.Columns.Count; i++)
                    dtCloned.Columns[i].DataType = typeof(string);

                for (int row = 0; row < copyDataTable.Rows.Count; row++)
                {
                    object[] array = new object[copyDataTable.Columns.Count];
                    for (int column = 0; column < copyDataTable.Columns.Count; column++)
                        array[column] = copyDataTable.Rows[row][column].ToString();
                    dtCloned.Rows.Add(array);
                }

                //for (int i = 1; i <= (System.Math.Min(data.Tables["clienti"].Rows.Count, 5)); i++)
                //{
                //    dtCloned.Columns.Add(new DataColumn("cliente" + i, typeof(string)));
                //    int indexrow = 0;
                //    foreach (var row in dtCloned.AsEnumerable())
                //    {
                //        dtCloned.Rows[indexrow]["cliente" + i] = dtCloned.Rows[indexrow]["ProQuota"];
                //        indexrow++;
                //    }
                //}

                //for (int index_row = 0; index_row < dtCloned.Rows.Count; index_row++)
                //{
                //    // cerco index in tabella cliente 
                //    DataRow anotherRow = data.Tables["clienti"].AsEnumerable().FirstOrDefault(row1 => row1["nome"].Equals(dtCloned.Rows[index_row]["NomeCliente"])
                //        && row1["cognome"].Equals(dtCloned.Rows[index_row]["CognomeCliente"]));
                //    int indiceCliente = data.Tables["clienti"].Rows.IndexOf(anotherRow);

                //    for (int i = 1; i <= (System.Math.Min(data.Tables["clienti"].Rows.Count, 5)); i++)
                //    {
                //        if (i - 1 == indiceCliente)
                //            dtCloned.Rows[index_row]["cliente" + i] = dtCloned.Rows[index_row]["ProQuota"];
                //        else
                //            dtCloned.Rows[index_row]["cliente" + i] = "0";

                //        //dtCloned.Rows[index_row]["valoreImmobileTotale"] = dtCloned.Rows[index_row]["ProQuota"];
                //    }
                //}

                data.Tables[name.ToString()].Merge(dtCloned);
            }
            return data;
        }


        public void AddLegend(List<string> legenda)
        {
            AddElement(new SpacePDF(2));

            var xSx = 35;
            foreach (string valore in legenda)
            {
                var listaLegend = new List<ObjectPDF>();
                ImagePDF image = null;
                FormattedTextAreaPDF testo = null;
                switch (valore)
                {
                    case "Warning.png":
                        image = new ImagePDF(xSx, 0.1F, valore) { AutoIncrementYWritable = false };
                        testo = new FormattedTextAreaPDF("Immobile con anomalie catastali. Vedi Dettaglio immobili - Anomalie catastali.", xSx + 15) { DeltaY = 1, AutoIncrementYWritable = true };
                        listaLegend.Add(image);
                        listaLegend.Add(testo);
                        break;
                    case "detagglio2.png":
                        image = new ImagePDF(xSx, 0.15F, valore) { AutoIncrementYWritable = false };
                        testo = new FormattedTextAreaPDF("Immobile con diritti e/o quote variate. Vedi Dettaglio immobili – Immobili con diritti e/o quote di diritto variate.", xSx + 15) { DeltaY = 3, AutoIncrementYWritable = true };
                        listaLegend.Add(image);
                        listaLegend.Add(testo);
                        break;
                    case "New.png":
                        image = new ImagePDF(xSx, 0.1F, valore) { AutoIncrementYWritable = false };
                        testo = new FormattedTextAreaPDF("Immobile di nuova acquisizione.", xSx + 15) { DeltaY = 1, AutoIncrementYWritable = true };
                        listaLegend.Add(image);
                        listaLegend.Add(testo);
                        break;
                }
                AddElement(listaLegend);
                AddElement(new SpacePDF(5));
            }
        }

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


        // Draw e GetDataSet del 14.09.2017

        ///// <summary>
        ///// Scheda2. Patrimonio immobiliare –Sintesi (distribuzione del patrimonio complessivo del cliente diviso per tipologia e cointestatari)
        ///// </summary>
        //protected override sealed void Draw()
        //{
        //    var dataset = GetDataSet();
        //    FormattedTextAreaPDF testoPagina = null;
        //    FormattedTextAreaPDF notaTabella = null;

        //    DataTable clientiTable = dataset.Tables["clienti"];

        //    foreach (DataTable table in dataset.Tables)
        //    {
        //        if (table.TableName != "clienti" && table != null && table.Rows.Count > 0)
        //        {
        //            testoPagina = new FormattedTextAreaPDF("Di seguito si rappresenta l'elenco degli immobili con finalità non specificata.", 35) { FontSize = 10, FontColor = ColorPDF.Immobiliare_Grigio_TitoloPiccolo, AutoIncrementYWritable = true, Width = 740, FixedHeight = 20 };
        //            notaTabella = new FormattedTextAreaPDF("Nota(*): Il \"Reddito dominicale\" dei terreni è esposto nella colonna \"Rendita catastale\"<BR>", 60, 680) { AutoIncrementYWritable = false, Y = EnvironmentFacade.RendererFacade.YLowerLimit - 25, FontSize = 6, FontColor = ColorPDF.Nero, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify, DeltaX = 5, DeltaY = 5 };
        //            var listaOggettiDaRipetere = new List<ObjectPDF> { new SpacePDF(30), testoPagina, new SpacePDF(10), notaTabella};
        //            AddElement(listaOggettiDaRipetere);
                    
        //            AddElement(new RepeaterPDF(listaOggettiDaRipetere));

        //            const int larghezzaTabella = 730; // massima larghezza tabella
        //            const int larghezzaSingoloCliente = 65;
        //            const int larghezzaCategoriaCatastale = 40;
        //            const int larghezzaSuperficieStimata = 50;
        //            const int larghezzaRenditaCatastale = 55;
        //            const int larghezzaTotale = 80;
        //            const int tabellaFissa = larghezzaTotale + larghezzaRenditaCatastale + larghezzaSuperficieStimata + larghezzaCategoriaCatastale;
        //            //La colonna descrizione è l'unica variabile.
        //            var larghezzaDescrizione = larghezzaTabella - 10 - tabellaFissa - (larghezzaSingoloCliente * System.Math.Min(clientiTable.Rows.Count, 5));


        //            TablePDF tablePDF = new TablePDF(35, table)
        //            {
        //                Style = Style.Immobiliare,
        //                RowsPadding = 3,
        //                Footer = false,
        //                HeaderHeight = 130,
        //                MinimumRowHeight = 12,
        //                ShowBorderLastLine = true
        //            };

        //            tablePDF.Columns.Add(new ColumnPDF("codAnomalia", 10, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Immagine, "codAnomalia", "") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, ScaleColumnTypeImage = 0.1F, DeltaYContent = 2, PaddingLeft = -10});
        //            tablePDF.Columns.Add(new ColumnPDF("descrizione", larghezzaDescrizione, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "descrizione", "Immobili con finalità non specificata") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderVerticalAlignment = VerticalAlignmentType.Basso});
        //            tablePDF.Columns.Add(new ColumnPDF("codTipologiaCatasto", larghezzaCategoriaCatastale, HorizontalAlignmentType.Centrato, false, false, 7, ColumnType.Testo, "codTipologiaCatasto", "Categoria catastale") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });
        //            tablePDF.Columns.Add(new ColumnPDF("superficie", larghezzaSuperficieStimata, HorizontalAlignmentType.Centrato, false, false, 7, ColumnType.Testo, "superficie", "Superficie Stimata") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });
        //            tablePDF.Columns.Add(new ColumnPDF("codTipologiaCatasto", larghezzaRenditaCatastale, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "renditaCatastale", "Rendita catastale*") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

        //            for (int i = 1; i <= (System.Math.Min(clientiTable.Rows.Count,5)); i++)
        //                tablePDF.Columns.Add(new ColumnPDF("cliente" + i, larghezzaSingoloCliente, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "cliente" + i, clientiTable.Rows[i - 1]["cognome"].ToString() + " " + clientiTable.Rows[i - 1]["nome"].ToString()) { HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

        //            tablePDF.Columns.Add(new ColumnPDF("valoreImmobileTotale", larghezzaTotale, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "valoreImmobileTotale", "Valore stimato<br>totale (€)") { FontBold = false, HeaderFontBold = true, HeaderFontSize = 7, DeltaYContent = 1, HeaderTextVerticalDirection = true, HeaderVerticalAlignment = VerticalAlignmentType.Centrato });

        //            for (int k = 0; k < table.Rows.Count; k++)
        //            {
        //                if (table.Rows[k].Field<string>("codTipologiaCatasto").All(Char.IsDigit))
        //                {
        //                    int column = table.Rows[k].Table.Columns["codTipologiaCatasto"].Ordinal;
        //                    tablePDF.Cells[column, k].Value = "-";
        //                }
        //                else if (table.Rows[k].Field<string>("codTipologiaCatasto").Equals("F/5"))
        //                {
        //                    tablePDF.Cells[3, k].Value = "n.d.";
        //                    tablePDF.Cells[4, k].Value = "n.d.";
        //                }
        //                if (table.Rows[k].Field<string>("codTipologiaCatasto").Substring(0, 1) == "M")
        //                    tablePDF.Cells[2, k].Value = "-";
        //                else
        //                    tablePDF.Cells[2, k].Value = table.Rows[k].Field<string>("codTipologiaCatasto");

        //                if (table.Rows[k].Field<string>("renditaCatastale").Equals("") || Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")) == 0)
        //                    tablePDF.Cells[4, k].Value = "-";
        //                else
        //                    tablePDF.Cells[4, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("renditaCatastale")).ToString());

        //                for (int i = 1; i <= (System.Math.Min(clientiTable.Rows.Count, 5)); i++)
        //                {
        //                    if (Convert.ToDecimal(table.Rows[k].Field<string>("cliente"+i)) == 0)
        //                        tablePDF.Cells[4+i, k].Value = "-";
        //                    else
        //                        tablePDF.Cells[4+i, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("ProQuota")).ToString());
        //                }

        //                if (Convert.ToDecimal(table.Rows[k].Field<string>("valoreImmobileTotale")) == 0)
        //                    tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = "-";
        //                else
        //                    tablePDF.Cells[System.Math.Min(clientiTable.Rows.Count, 5) + 5, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("valoreImmobileTotale")).ToString());

        //                if (table.Rows[k].Field<string>("tipoImmobile").Equals("F"))
        //                    tablePDF.Cells[3, k].Value = Helper.FormatDecimal(table.Rows[k].Field<string>("superficie"), 0) + " mq";

        //                if (table.Rows[k].Field<string>("tipoImmobile").Equals("T"))
        //                {
        //                    if (table.Rows[k].Field<string>("superficie").Equals(""))
        //                        tablePDF.Cells[3, k].Value = "n.d.";
        //                    else
        //                        tablePDF.Cells[3, k].Value = Helper.FormatDecimal(table.Rows[k].Field<string>("superficie"), 3) + " ha";
        //                }

        //                if (!table.Rows[k].Field<string>("redditoDominicale").Equals(""))
        //                    tablePDF.Cells[4, k].Value = Helper.FormatCurrency(Convert.ToDecimal(table.Rows[k].Field<string>("redditoDominicale")).ToString());
        //            }

        //            AddElement(tablePDF);

        //            if (table.TableName != "clienti" && table != null && table.Rows.Count > 0)
        //            {
        //                List<string> legenda = table.AsEnumerable().Select(z => z.Field<string>("codAnomalia")).Distinct() != null ?
        //                    table.AsEnumerable().Select(z => z.Field<string>("codAnomalia")).Distinct().ToList() : null;
        //                if (legenda != null && legenda.Count > 0)
        //                    AddLegend(legenda);
        //            }

        //        }
        //    }

        //}


        ///// <summary>
        ///// Recupera i dati necessari alla Section restituendo un DataSet.
        ///// </summary>
        ///// <returns></returns>
        //protected override DataSet GetDataSet()
        //{
        //    DataSet data = new DataSet();
        //    data.Tables.Add(new DataTable("clienti"));    //  U
        //    data.Tables["clienti"].Columns.Add(new DataColumn("nome", typeof(string)));
        //    data.Tables["clienti"].Columns.Add(new DataColumn("cognome", typeof(string)));
        //    data.Tables.Add(new DataTable("1"));    //  U
        //    data.Tables.Add(new DataTable("2"));    //  S
        //    data.Tables.Add(new DataTable("3"));    //  I
        //    data.Tables.Add(new DataTable("4"));    //  NS

        //    #region Definizione dei parametri

        //    var parametri = new List<Parametro>
        //    {
        //        new Parametro
        //        {
        //            Direction = ParameterDirection.Input,
        //            DbType = DbType.Int64,
        //            ParameterName = "ChiaveNucleo",
        //            Value = EnvironmentFacade.ReportEnvironment.NucleoImmobiliare.ChiaveNucleo
        //        }
        //    };
              

        //    #endregion

        //    var dsOriginal = DataAccess.ExecuteDataSetStoredProcedure(DBProvider.SqlServerConsulenzaUnica, "REP_ImmobiliareMonitoraggio_S_DettaglioFabbricatiNucleo", parametri);
        //    if (dsOriginal.Tables[0].Rows.Count == 0)
        //        return null;

        //    List<string> clientiList = dsOriginal.Tables[0].AsEnumerable()
        //        .Select(z => z.Field<string>("NomeCliente") + "$" + z.Field<string>("CognomeCliente")).Distinct().ToList();

        //    foreach (var cliente in clientiList) {
        //        var nomeCognome = cliente.Split('$');
        //        data.Tables["clienti"].Rows.Add(nomeCognome[0], nomeCognome[1]);
        //    }

        //    if (dsOriginal.Tables[0].AsEnumerable().Where(x => x.Field<int>("ordinamento") == 4).Count() > 0)
        //    {
        //        DataTable copyDataTable = dsOriginal.Tables[0].AsEnumerable().GroupBy(x => x.Field<string>("idImmobileCatasto")).Select(x => x.First()).Where(x => x.Field<int>("ordinamento") == 4).OrderBy(x => x.Field<int>("Tipo")).CopyToDataTable();
        //        //.GroupBy(y => y.Field<string>("idImmobilecatasto"), (key) => new { idImmobilecatasto = key, Cars = g.ToList() })).CopyToDataTable();
        //        for (int i = 0; i < copyDataTable.Rows.Count; i++)
        //        {
        //            copyDataTable.Rows[i]["codAnomalia"] = copyDataTable.Rows[i]["codAnomalia"] != DBNull.Value &&
        //                copyDataTable.Rows[i]["codAnomalia"] != null ?
        //                "Warning.png" : "";

        //            if (copyDataTable.Rows[i]["ProQuota"] != DBNull.Value)
        //                copyDataTable.Rows[i]["ProQuota"] = Helper.FormatCurrency(copyDataTable.Rows[i]["ProQuota"].ToString());
        //            else
        //                copyDataTable.Rows[i]["ProQuota"] = "0";
        //            if (copyDataTable.Rows[i]["valoreImmobileTotale"] != DBNull.Value)
        //                copyDataTable.Rows[i]["valoreImmobileTotale"] = Helper.FormatCurrency(copyDataTable.Rows[i]["valoreImmobileTotale"].ToString());
        //            else
        //                copyDataTable.Rows[i]["valoreImmobileTotale"] = "0";

        //            //copyDataTable.Rows[i]["ProQuota"] = Helper.FormatCurrency(copyDataTable.Rows[i]["ProQuota"].ToString());
        //            //copyDataTable.Rows[i]["valoreImmobileTotale"] = Helper.FormatCurrency(copyDataTable.Rows[i]["valoreImmobileTotale"].ToString());

        //        }
        //        //copyDataTable.Rows[0]["codAnomalia"] = "Warning.png";
        //        var name = copyDataTable.Rows[0]["ordinamento"];
        //        DataTable dtCloned = copyDataTable.Clone();
        //        dtCloned.Columns[0].DataType = typeof(Int32);
        //        for (int i = 0; i < copyDataTable.Columns.Count; i++)
        //            dtCloned.Columns[i].DataType = typeof(string);

        //        for (int row = 0; row < copyDataTable.Rows.Count; row++)
        //        {
        //            object[] array = new object[copyDataTable.Columns.Count];
        //            for (int column = 0; column < copyDataTable.Columns.Count; column++)
        //                array[column] = copyDataTable.Rows[row][column].ToString();
        //            dtCloned.Rows.Add(array);
        //        }
        //        for (int i = 1; i <= (System.Math.Min(data.Tables["clienti"].Rows.Count, 5)); i++)
        //        {
        //            dtCloned.Columns.Add(new DataColumn("cliente" + i, typeof(string)));
        //            int indexrow = 0;
        //            foreach (var row in dtCloned.AsEnumerable())
        //            {
        //                dtCloned.Rows[indexrow]["cliente" + i] = dtCloned.Rows[indexrow]["ProQuota"];
        //                indexrow++;
        //            }
        //        }

        //        for (int index_row = 0; index_row < dtCloned.Rows.Count; index_row++)
        //        {
        //            // cerco index in tabella cliente 
        //            DataRow anotherRow = data.Tables["clienti"].AsEnumerable().FirstOrDefault(row1 => row1["nome"].Equals(dtCloned.Rows[index_row]["NomeCliente"])
        //                && row1["cognome"].Equals(dtCloned.Rows[index_row]["CognomeCliente"]));
        //            int indiceCliente = data.Tables["clienti"].Rows.IndexOf(anotherRow);

        //            for (int i = 1; i <= (System.Math.Min(data.Tables["clienti"].Rows.Count, 5)); i++)
        //            {
        //                if (i - 1 == indiceCliente)
        //                    dtCloned.Rows[index_row]["cliente" + i] = dtCloned.Rows[index_row]["ProQuota"];
        //                else
        //                    dtCloned.Rows[index_row]["cliente" + i] = "0";

        //                //dtCloned.Rows[index_row]["valoreImmobileTotale"] = dtCloned.Rows[index_row]["ProQuota"];
        //            }

        //        }

        //        data.Tables[name.ToString()].Merge(dtCloned);
        //    }
        //    return data;
        //}


    }
}
