using System;
using Consulenza.ReportWriter.Business;
using Consulenza.ReportWriter.Business.OBJ_PDF;
using System.Data;
using System.Linq;
using Consulenza.ReportCommon;

namespace Consulenza.ReportWriter.Manager.Section.Unica
{
    /// <summary>
    /// S37.PatrimonioImmobiliare idSezione = 80
    /// </summary>
    public class S37 : Entity.Section
    {
        public S37(EnvironmentFacade environmentFacade, int idSection)
            : base(environmentFacade, idSection)
        {
            try
            {
                Draw();
            }
            catch (Exception ex)
            {
                SectionLogger.Write("S37", ex.Message, SectionLoggerMessageLevel.E, EnvironmentFacade.ReportEnvironment);
            }
        }

        //protected override sealed void Draw()
        //{
        //    var dati = GetDataSet();
        //    if (dati.Tables["immobili"].Rows.Count > 0)
        //    {
        //        #region Tabella immobili

        //        var tabella = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dati.Tables["immobili"], dati.Tables["immobiliFooter"])
        //        {
        //            Style = Style.ConsulenzaUnica,
        //            AlternateRow = false,
        //            HeaderHeight = 35,
        //            RowHeight = 27,
        //            HeaderTextHorizontalAlign = HorizontalAlignmentType.Sinistra
        //        };

        //        tabella.Columns.Add(new ColumnPDF("Descrizione", 190, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Descrizione", "Descrizione") { PaddingRight = 10, HeaderFontSize = 7, FontSize = 7, VerticalAlignment = VerticalAlignmentType.Centrato });
        //        tabella.Columns.Add(new ColumnPDF("Comune", 100, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Comune", "Comune") { HeaderFontSize = 7 });
        //        tabella.FooterColumns.Add(new ColumnPDF("Totale", 290, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });

        //        tabella.Columns.Add(new ColumnPDF("ValorestimatoTotale", 80, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Valorestimatototale", "Valore stimato<BR>totale (€)") { HeaderFontSize = 7, FontSize = 7 });
        //        tabella.FooterColumns.Add(new ColumnPDF("ValorestimatoTotale", 80, HorizontalAlignmentType.Destra, true, true, 7, ColumnType.Decimale) { HeaderFontSize = 7, FontSize = 7 });

        //        tabella.Columns.Add(new ColumnPDF("TipologiaDiritto", 95, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Tipologiadiritto", "Tipologia<br>diritto") { HeaderPaddingLeft = 10, PaddingLeft = 10, HeaderFontSize = 7, FontSize = 7 });
        //        tabella.Columns.Add(new ColumnPDF("Quotadiritto", 55, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Quotadiritto", "Quota di<BR>diritto (%)") { HeaderFontSize = 7, FontSize = 7, HeaderPaddingRight = 4, PaddingRight = 4 });
        //        tabella.FooterColumns.Add(new ColumnPDF("ColonnaVuota", 150, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });
        //        int i = 0;
        //        foreach (var item in dati.Tables["immobili"].Rows)
        //        {
        //            tabella.Cells[2, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
        //            //tabella.Cells[3, i].HorizontalAlignment = HorizontalAlignmentType.Centrato;
        //            tabella.Cells[4, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
        //            i++;
        //        }
        //        //tabella.Cells[0, 0].YContentOffset = -3F;
        //        AddElement(tabella);

        //        #endregion
        //    }
        //}

        protected override sealed void Draw()
        {
            var dati = GetDataSet();
            if (dati.Tables["immobili"].Rows.Count > 0)
            {
                #region Tabella immobili

                var tabella = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dati.Tables["immobili"], dati.Tables["immobiliFooter"])
                {
                    Style = Style.ConsulenzaUnica,
                    AlternateRow = false,
                    HeaderHeight = 35,
                    RowHeight = 40, 
                    HeaderTextHorizontalAlign = HorizontalAlignmentType.Sinistra
                };

                //tabella.Columns.Add(new ColumnPDF("Descrizione", 190, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Descrizione", "Descrizione") { PaddingRight = 10, HeaderFontSize = 7, FontSize = 7, VerticalAlignment = VerticalAlignmentType.Centrato });
                //tabella.Columns.Add(new ColumnPDF("Comune", 100, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Comune", "Comune") { HeaderFontSize = 7 });
                //tabella.FooterColumns.Add(new ColumnPDF("Totale", 290, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });
                //tabella.Columns.Add(new ColumnPDF("ValorestimatoTotale", 80, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Valorestimatototale", "Valore stimato<BR>totale (€)") { HeaderFontSize = 7, FontSize = 7 });
                //tabella.FooterColumns.Add(new ColumnPDF("ValorestimatoTotale", 80, HorizontalAlignmentType.Destra, true, true, 7, ColumnType.Decimale) { HeaderFontSize = 7, FontSize = 7 });
                //tabella.Columns.Add(new ColumnPDF("TipologiaDiritto", 95, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Tipologiadiritto", "Tipologia<br>diritto") { HeaderPaddingLeft = 10, PaddingLeft = 10, HeaderFontSize = 7, FontSize = 7 });
                //tabella.Columns.Add(new ColumnPDF("Quotadiritto", 55, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Quotadiritto", "Quota di<BR>diritto (%)") { HeaderFontSize = 7, FontSize = 7, HeaderPaddingRight = 4, PaddingRight = 4 });
                //tabella.FooterColumns.Add(new ColumnPDF("ColonnaVuota", 150, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });

                tabella.Columns.Add(new ColumnPDF("Descrizione", 160, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Descrizione", "Descrizione") { PaddingRight = 10, HeaderFontSize = 7, FontSize = 7 });
                tabella.Columns.Add(new ColumnPDF("Comune", 85, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Comune", "Comune") { HeaderFontSize = 7 });
                tabella.FooterColumns.Add(new ColumnPDF("Totale", 245, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });

                //tabella.Columns.Add(new ColumnPDF("TipologiaDiritto", 70, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Testo, "Tipologiadiritto", "Tipologia<br>diritto") { HeaderPaddingLeft = 10, PaddingLeft = 10, HeaderFontSize = 7, FontSize = 7 });
                tabella.Columns.Add(new ColumnPDF("TipologiaDiritto", 70, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Tipologiadiritto", "Tipologia<br>diritto") { HeaderPaddingLeft = 10, PaddingLeft = 10, HeaderFontSize = 7, FontSize = 7, VerticalAlignment = VerticalAlignmentType.Centrato });
                tabella.Columns.Add(new ColumnPDF("Quotadiritto", 54, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Decimale, "Quotadiritto", "Quota<BR>diritto (%)") { HeaderFontSize = 7, FontSize = 7, HeaderPaddingRight = 4, PaddingRight = 4 });
                tabella.FooterColumns.Add(new ColumnPDF("ColonnaVuota", 124, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, FontSize = 7 });

                tabella.Columns.Add(new ColumnPDF("ValorestimatoProquota", 77, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Decimale, "ValorestimatoProquota", "Valore stimato<BR>pro quota (€)") { HeaderFontSize = 7, FontSize = 7 });
                tabella.FooterColumns.Add(new ColumnPDF("ValorestimatoProquota", 77, HorizontalAlignmentType.Destra, true, true, 7, ColumnType.Decimale) { HeaderFontSize = 7, FontSize = 7 });

                tabella.Columns.Add(new ColumnPDF("ValorestimatoTotale", 77, HorizontalAlignmentType.Destra, false, false, 7, ColumnType.Decimale, "Valorestimatototale", "Valore stimato<BR>totale (€)") { HeaderFontSize = 7, FontSize = 7, PaddingRight = 4 });
                tabella.FooterColumns.Add(new ColumnPDF("ValorestimatoTotale", 77, HorizontalAlignmentType.Destra, true, true, 7, ColumnType.Decimale) { HeaderFontSize = 7, FontSize = 7 });

                
                int i = 0;
                foreach (var item in dati.Tables["immobili"].Rows)
                {
                    //tabella.Cells[2, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    ////tabella.Cells[3, i].HorizontalAlignment = HorizontalAlignmentType.Centrato;
                    //tabella.Cells[4, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    tabella.Cells[3, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    tabella.Cells[4, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    tabella.Cells[5, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    i++;
                }
                //tabella.Cells[0, 0].YContentOffset = -3F;
                AddElement(tabella);

                #endregion
            }
        }
        /// <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 sealed override DataSet GetDataSet()
        //{
        //    var ds = new DataSet();
        //    ds.Tables.Add(new DataTable("immobili"));
        //    ds.Tables.Add(new DataTable("immobiliFooter"));

        //    var patrimonioImmobiliare = datiSeiUnico.patrimonioUnit().patrimonioImmobiliare;
        //    var listaImmobili = patrimonioImmobiliare.listaProdotti;

        //    #region Immobili
        //    ds.Tables["immobili"].Columns.Add(new DataColumn("Descrizione", typeof(string)));
        //    ds.Tables["immobili"].Columns.Add(new DataColumn("Comune", typeof(string)));
        //    ds.Tables["immobili"].Columns.Add(new DataColumn("ValorestimatoTotale", typeof(decimal)));
        //    ds.Tables["immobili"].Columns.Add(new DataColumn("TipologiaDiritto", typeof(string)));
        //    ds.Tables["immobili"].Columns.Add(new DataColumn("Quotadiritto", typeof(decimal)));

        //    foreach (var item in listaImmobili)
        //    {
        //        ds.Tables["immobili"].Rows.Add(
        //            item.descrizione,
        //            item.comune + " ("+item.provincia+")",
        //            item.ctv,
        //            item.tipologiaCatastale != null ? item.tipologiaCatastale : "-",
        //            item.quotaDiritto
        //            );
        //    }

        //    #endregion

        //    #region Totale Immobili
        //    ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("Totale", typeof(string)));
        //    ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("ValorestimatoTotale", typeof(decimal)));
        //    ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("ColonnaVuota", typeof(string)));

        //    ds.Tables["immobiliFooter"].Rows.Add(
        //        "Totale patrimonio immobiliare",
        //         Helper.FormatCurrency(patrimonioImmobiliare.totalePatrimonio.ToString()),
        //         string.Empty
        //        );


        //    #endregion

        //    return ds;
        //}

        protected sealed override DataSet GetDataSet()
        {
            var ds = new DataSet();
            ds.Tables.Add(new DataTable("immobili"));
            ds.Tables.Add(new DataTable("immobiliFooter"));

            var patrimonioImmobiliare = datiSeiUnico.patrimonioUnit().patrimonioImmobiliare;
            var listaImmobili = patrimonioImmobiliare.listaProdotti;

            #region Immobili
            ds.Tables["immobili"].Columns.Add(new DataColumn("Descrizione", typeof(string)));
            ds.Tables["immobili"].Columns.Add(new DataColumn("Comune", typeof(string)));
            ds.Tables["immobili"].Columns.Add(new DataColumn("TipologiaDiritto", typeof(string)));
            ds.Tables["immobili"].Columns.Add(new DataColumn("Quotadiritto", typeof(decimal)));
            ds.Tables["immobili"].Columns.Add(new DataColumn("ValorestimatoProquota", typeof(decimal)));
            ds.Tables["immobili"].Columns.Add(new DataColumn("ValorestimatoTotale", typeof(decimal)));

            decimal dTotaleProquota = 0;
            foreach (var item in listaImmobili)
            {
                //if (item.tipologiaCatastale == "Nuda proprietà")
                //    item.tipologiaCatastale = "Nuda";

                ds.Tables["immobili"].Rows.Add(
                    item.descrizione,
                    item.comune + " (" + item.provincia + ")",
                    item.tipologiaCatastale != null ? item.tipologiaCatastale : "-",
                    item.quotaDiritto,
                    item.valoreStimatoProQuota,
                    item.ctv
                    );
                dTotaleProquota = dTotaleProquota + item.valoreStimatoProQuota;
            }

            #endregion

            #region Totale Immobili
            ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("Totale", typeof(string)));
            ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("ColonnaVuota", typeof(string)));
            ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("ValorestimatoProquota", typeof(decimal)));
            ds.Tables["immobiliFooter"].Columns.Add(new DataColumn("ValorestimatoTotale", typeof(decimal)));

            ds.Tables["immobiliFooter"].Rows.Add(
                "Totale patrimonio immobiliare",
                string.Empty,
                Helper.FormatCurrency(dTotaleProquota.ToString()),
                Helper.FormatCurrency(patrimonioImmobiliare.totalePatrimonio.ToString())
                );


            #endregion

            return ds;
        }

    }
}
