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.Finalita
{
    /// <summary>
    /// Scheda 11 Dettaglio cointestatari  - 22-02-2016
    /// </summary>
    public class S11 : Entity.Section
    {

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

        /// <summary>
        /// Scheda1. Patrimonio immobiliare –Distribuzione geografica (terreni) (distribuzione dei terreni del cliente divisa per Comune e categoria catastale)
        /// </summary>
        protected override sealed void Draw()
        {
            var dati = GetDataSet();

            if (dati.Tables[0].Rows.Count == 0) return;

            var testi = GetText();
            //var icona = new ImagePDF(EnvironmentFacade.RendererFacade.XLeftLimit, 0.20F, "Cointestatari.png") { AutoIncrementYWritable = false };
            var titolo = new FormattedTextAreaPDF(testi.Rows[0]["testo1"].ToString(), EnvironmentFacade.RendererFacade.XLeftLimit, 700) { FontSize = 10, FontColor = ColorPDF.Immobiliare_Grigio_TestoStandard, DeltaY = 5 };

            //AddElement(new SpacePDF(40));
            //AddElement(icona);
            //AddElement(titolo);
            //AddElement(new SpacePDF(25));

            #region Cointestatari

            #region Tabella

            var datiTabella = dati.Tables[0].AsEnumerable().OrderByDescending(o => o.Field<decimal>("ValoreImmobileTotale")).CopyToDataTable();

            foreach (DataRow ro in datiTabella.Rows)
            {
                ro["Cliente"] = ro["Cliente"].ToString().Trim() != "" ? Helper.CapitalizeWords(ro["Cliente"].ToString().ToLower()) : "-";

                ro["RagioneSociale"] = ro["RagioneSociale"].ToString().Contains("FIT") || ro["RagioneSociale"].ToString().Trim() == "" ? "-" : ro["RagioneSociale"].ToString();

            }
            var dttab = new DataView(datiTabella).ToTable(true, "RagioneSociale", "Cliente");
            var tabella = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dttab)
            {
                Style = Style.Immobiliare,
                RowsPadding = 4,
                ShowBorderLastLine = true,
                Footer = false
            };

            tabella.Columns.Add(new ColumnPDF("nominativo", 200, HorizontalAlignmentType.Sinistra, false, false, 8, ColumnType.Testo, "Cliente", "Nominativo"));
            tabella.Columns.Add(new ColumnPDF("codicefiscale", 150, HorizontalAlignmentType.Sinistra, false, false, 8, ColumnType.Testo, "RagioneSociale", "Codice Fiscale"));

            #endregion

            #endregion

            // AddElement(tabella);

            var listaOggettiDaRipetere = new List<ObjectPDF> { new SpacePDF(40), titolo, new SpacePDF(25) };
            AddElement(new RepeaterPDF(listaOggettiDaRipetere));



            AddElement(new SpacePDF(40));

            AddElement(titolo);
            AddElement(new SpacePDF(25));
            AddElement(tabella);


        }

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

        /// <summary>
        /// Recupera i dati necessari alla Section restituendo un DataSet.
        /// </summary>
        /// <returns></returns>
        protected override DataSet GetDataSet()
        {
            var ds = new DataSet();

            #region Definizione dei parametri

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

            #endregion

            return EnvironmentFacade.ReportEnvironment.FinalitaImmobiliare.ImmobiliareCEUnica ? DataAccess.ExecuteDataSetStoredProcedure(DBProvider.SqlServerConsulenzaUnica, "REP_FinalitaImmobiliare_S11_DettaglioCointestatari", parametri)
                : DataAccess.ExecuteDataSetStoredProcedure(DBProvider.SqlServerConsulenzaEvoluta, "REP_FinalitaImmobiliare_S11_DettaglioCointestatari", parametri);


        }
    }
}
