using System;
using Consulenza.ReportWriter.Business;
using Consulenza.ReportWriter.Business.OBJ_PDF;
using System.Data;
using Consulenza.ReportCommon;
namespace Consulenza.ReportWriter.Manager.Section.Unica
{
    public class S72 : Entity.Section
    {
        /// <summary>
        /// S72.MonitoraggioProventiEScadenze idSezione = 115
        /// </summary>
        public S72(EnvironmentFacade environmentFacade, int idSection)
            : base(environmentFacade, idSection)
        {
            try
            {
                Draw();
            }
            catch (Exception ex)
            {
                SectionLogger.Write("S72", ex.Message, SectionLoggerMessageLevel.E, EnvironmentFacade.ReportEnvironment);
            }
        }

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

            AddElement(new SpacePDF(15));

            #region Tabella Label
            var tabellaLabel = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dati.Tables["Label"])
            {
                Style = Style.ConsulenzaUnica,
                Header = false,
                Footer = false,
                AlternateRow = false,
                RowHeight = 25
            };
            tabellaLabel.Columns.Add(new ColumnPDF("descrizione", 190, HorizontalAlignmentType.Sinistra, true, true, 7, ColumnType.Testo, "descrizione", string.Empty) { DeltaYContent = -2 });
            tabellaLabel.Columns.Add(new ColumnPDF("valore", 330, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "valore", string.Empty) { PaddingLeft = 5, DeltaYContent = -2 });

            AddElement(tabellaLabel);
            AddElement(new SpacePDF(15));
            if (dati.Tables["TrimestreCorrente"].Rows.Count > 0)
            {
                var tabellaTrimestreCorrente = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dati.Tables["TrimestreCorrente"])
                 {
                     Style = Style.ConsulenzaUnica,
                     AlternateRow = false

                 };
                tabellaTrimestreCorrente.Columns.Add(new ColumnPDF("Data", 100, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Testo, "Data", "Data") { HeaderFontSize = 7 });
                tabellaTrimestreCorrente.Columns.Add(new ColumnPDF("Strumento", 150, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Strumento", "Strumento") { HeaderFontSize = 7 });
                tabellaTrimestreCorrente.Columns.Add(new ColumnPDF("Tipomovimento", 150, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "TipoMovimento", "Tipo movimento") { HeaderFontSize = 7 });
                tabellaTrimestreCorrente.Columns.Add(new ColumnPDF("Importo", 120, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Importo", "Importo (€)") { HeaderFontSize = 7, PaddingRight = 4 });
                for (int h = 0; h < dati.Tables["TrimestreCorrente"].Rows.Count; h++)
                {
                    tabellaTrimestreCorrente.Cells[3, h].HorizontalAlignment = HorizontalAlignmentType.Destra;
                }
                AddElement(tabellaTrimestreCorrente);
            }

            AddElement(new SpacePDF(15));
            if (dati.Tables["TrimestreProssimo"].Rows.Count > 0)
            {
                AddElement(new FormattedTextAreaPDF(Helper.ReplaceVariables(GetText().Rows[0]["testo1"].ToString(), EnvironmentFacade.ReportEnvironment), EnvironmentFacade.RendererFacade.XLeftLimit) { FontSize = 7, TextHorizontalAlign = ceTe.DynamicPDF.TextAlign.Justify });
                AddElement(new SpacePDF(15));
                var tabellaTrimestreProssimo = new TablePDF(EnvironmentFacade.RendererFacade.XLeftLimit, dati.Tables["TrimestreProssimo"])
                 {
                     Style = Style.ConsulenzaUnica,
                     AlternateRow = false

                 };
                tabellaTrimestreProssimo.Columns.Add(new ColumnPDF("Strumento", 250, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "Strumento", "Strumento") { HeaderFontSize = 7 });
                tabellaTrimestreProssimo.Columns.Add(new ColumnPDF("Tipomovimento", 170, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "TipoMovimento", "Tipo movimento") { HeaderFontSize = 7 });
                tabellaTrimestreProssimo.Columns.Add(new ColumnPDF("DataScadenza", 100, HorizontalAlignmentType.Sinistra, false, false, 7, ColumnType.Decimale, "DataScadenza", "Data scadenza") { HeaderFontSize = 7, PaddingRight = 4 });

                AddElement(tabellaTrimestreProssimo);
            }
             #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();

            #region dati
            var listaProventi = datiSeiUnico.monitoraggioUnit().proventiScadenze;
            #endregion

            #region Proventi

            ds.Tables.Add(new DataTable("Label"));
            ds.Tables["Label"].Columns.Add(new DataColumn("Descrizione", typeof(string)));
            ds.Tables["Label"].Columns.Add(new DataColumn("Valore", typeof(string)));
            ds.Tables["Label"].Rows.Add("Proventi ultimo trimestre (€)", Helper.FormatCurrency(listaProventi.proventiTrimestreAttuale.ToString()));
           
            #endregion

            #region trimestre in corso
           
            ds.Tables.Add(new DataTable("TrimestreCorrente"));
            ds.Tables["TrimestreCorrente"].Columns.Add(new DataColumn("Data", typeof(string)));
            ds.Tables["TrimestreCorrente"].Columns.Add(new DataColumn("Strumento", typeof(string)));
            ds.Tables["TrimestreCorrente"].Columns.Add(new DataColumn("TipoMovimento", typeof(string)));
            ds.Tables["TrimestreCorrente"].Columns.Add(new DataColumn("Importo", typeof(decimal)));
             if(listaProventi.scadenzeTrimestreAttuale != null){
                foreach (var scadenza in listaProventi.scadenzeTrimestreAttuale)
                {
                    ds.Tables["TrimestreCorrente"].Rows.Add(scadenza.dtScadenza.ToShortDateString(),scadenza.descrizione, scadenza.tipologia, Helper.FormatCurrency(scadenza.importo.ToString()));
                }
            }
            #endregion

            #region trimestre futuro
            ds.Tables.Add(new DataTable("TrimestreProssimo"));
            ds.Tables["TrimestreProssimo"].Columns.Add(new DataColumn("Strumento", typeof(string)));
            ds.Tables["TrimestreProssimo"].Columns.Add(new DataColumn("TipoMovimento", typeof(string)));
            ds.Tables["TrimestreProssimo"].Columns.Add(new DataColumn("DataScadenza", typeof(string)));

            if(listaProventi.scadenzeTrimestreSuccessivo != null){
                foreach (var scadenza in listaProventi.scadenzeTrimestreSuccessivo)
                {
                    ds.Tables["TrimestreProssimo"].Rows.Add(scadenza.descrizione, scadenza.tipologia, scadenza.dtScadenza.ToShortDateString());
                }
            }
            #endregion
            return ds;
        }
    }
}
