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 22
    /// </summary>
    public class S22 : Section.Immobiliare.Monitoraggio.S22
    {

        public S22(EnvironmentFacade environmentFacade, int idSection)
            : base(environmentFacade, idSection)
        {
        }

        protected override DataTable GetDataTable()
        {
            #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 dt = DataAccess.ExecuteDataTableStoredProcedure(DBProvider.SqlServerConsulenzaUnica, "REP_Nucleo_Immobiliare_S8_DettImmobiliAnomalieCatastali", parametri);


            //Riaggregazione
            //var query = (from p in dt.AsEnumerable()
            //             orderby p.Field<string>("tipoImmobile") ascending, p.Field<decimal>("ValoreStimato") descending
            //             group p by p.Field<string>("IdImmobileCatasto") into g
            //             select new
            //             {
            //                 IdImmobileCatasto = g.Key,
            //                 Descrizione = g.Select(x => x.Field<string>("Descrizione")).FirstOrDefault(),
            //                 ValoreStimato = g.Select(x => x.Field<decimal>("ValoreStimato")).FirstOrDefault(),
            //                 AnomalieRilevate = string.Join("<BR><BR>", g.Select(x => x.Field<string>("AnomalieRilevate")))
            //             }).ToList();

            // Modificata ES in data 10.08.2017
            var query = (from p in dt.AsEnumerable()
                         orderby p.Field<decimal>("ValoreStimato") descending
                         group p by p.Field<string>("IdImmobileCatasto") into g
                         select new
                         {
                             IdImmobileCatasto = g.Key,
                             Descrizione = g.Select(x => x.Field<string>("Descrizione")).FirstOrDefault(),
                             ValoreStimato = g.Select(x => x.Field<decimal>("ValoreStimato")).FirstOrDefault(),
                             AnomalieRilevate = string.Join("<BR><BR>", g.Select(x => x.Field<string>("AnomalieRilevate")))
                         }).ToList();


            var dtResult = dt.Clone();

            foreach (var item in query)
                dtResult.Rows.Add(item.IdImmobileCatasto, item.Descrizione, item.ValoreStimato, string.Empty, item.AnomalieRilevate);


            return dtResult;
        }

    }
}
