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

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

        protected override sealed void Draw()
        {
            var dati = GetDataSet();

                #region Tabella dei Progetti di Investimento
                DataTable dtOrdinamento = dati.Tables["altribeni"].AsEnumerable().OrderByDescending(o => o.Field<decimal>("ValoreCommerciale")).CopyToDataTable();
                var tabella = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dtOrdinamento, dati.Tables["altribeniFooter"])
                {
                    Style = Style.ConsulenzaUnica,
                    AlternateRow = false,
                    HeaderHeight = 30,
                     
                };

                tabella.Columns.Add(new ColumnPDF("Descrizione", 200, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Descrizione", "Descrizione") { HeaderFontSize = 7 });
                tabella.Columns.Add(new ColumnPDF("ValoreCommerciale", 130, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Valorecommerciale", "Valore commerciale (€)") { PaddingRight = 15, HeaderFontSize = 7, HeaderPaddingRight = 5 });
                tabella.Columns.Add(new ColumnPDF("Tipologia", 90, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Tipologia", "Tipologia") { HeaderFontSize = 7, PaddingLeft = 3 });
                tabella.Columns.Add(new ColumnPDF("QuotaProprieta", 100, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Quotaproprieta", "Quota di proprietà (%)") { HeaderFontSize = 7, PaddingRight = 4 });

                tabella.FooterColumns.Add(new ColumnPDF("Descrizione", 200, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7 });
                tabella.FooterColumns.Add(new ColumnPDF("ValoreCommerciale", 130, HorizontalAlignmentType.Destra, true, true, 7, ColumnType.Decimale) { HeaderFontSize = 7, PaddingRight = 15 });
                tabella.FooterColumns.Add(new ColumnPDF("vuoto", 190, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo) { HeaderFontSize = 7, PaddingLeft = 2 });

                for (int i = 0; i < dati.Tables["altribeni"].Rows.Count; i++)
                {

                    tabella.Cells[1, i].HorizontalAlignment = HorizontalAlignmentType.Destra;
                    tabella.Cells[3, i].HorizontalAlignment = HorizontalAlignmentType.Destra;

                }
                for (int i = 0; i < dati.Tables["altribeniFooter"].Rows.Count; i++)
                {

                    tabella.FooterCells[1, i].HorizontalAlignment = HorizontalAlignmentType.Destra;


                }
           
                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("altribeni"));
            ds.Tables.Add(new DataTable("altribeniFooter"));
            var patrimonioAltro = datiSeiUnico.patrimonioUnit().patrimonioAltro;
            var listaAltriBeni = patrimonioAltro.listaProdotti;

            #region Altri beni

            var dtAltriBeni = new DataTable();
            ds.Tables["altribeni"].Columns.Add(new DataColumn("Descrizione", typeof(string)));
            ds.Tables["altribeni"].Columns.Add(new DataColumn("ValoreCommerciale", typeof(decimal)));
            ds.Tables["altribeni"].Columns.Add(new DataColumn("Tipologia", typeof(string)));
            ds.Tables["altribeni"].Columns.Add(new DataColumn("QuotaProprieta", typeof(string)));


                foreach (var item in listaAltriBeni)
                {
                    ds.Tables["altribeni"].Rows.Add(
                        item.descrizione,
                        item.ctv,
                        item.tipologia,
                        item.quotaProprieta==0 ? "-" : item.quotaProprieta.ToString()
                        );

                }

            #endregion

                #region Totale altri beni
                ds.Tables["altribeniFooter"].Columns.Add(new DataColumn("Totale", typeof(string)));
                ds.Tables["altribeniFooter"].Columns.Add(new DataColumn("ValoreCommerciale", typeof(decimal)));
                ds.Tables["altribeniFooter"].Columns.Add(new DataColumn("vuoto", typeof(string)));

                if (ds.Tables["altribeni"].Rows.Count != 0)
                {
                    ds.Tables["altribeniFooter"].Rows.Add(
                        "Totale altro patrimonio",
                        Helper.FormatCurrency(patrimonioAltro.totalePatrimonio.ToString()),
                       ""
                        );

                }

            #endregion

            return ds;
        }

    }
}
