using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.Text;

using ceTe.DynamicPDF.Merger;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.BusinessLayer;

namespace PDFGenerator.Presentation.Section
{
    public class S48 : ISezione
    {


        public S48()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        DataSetS48 set;
        ImportedPage page;
        string notaGradoCopertura = string.Empty;


        public void writeSezione(DataThread dataThread)
        {
            DocumentPDF document = dataThread.DocumentPDF;
            DatiTabella datitab = new DatiTabella();
            set = (DataSetS48)dataThread.Data.DatiSezione;
            datitab.table = set.Tables["Prodotti"];


            //calcola se entra nella pagina altrimenti aggiunge una nuova pagina. In questa sezione il # di righe � fissato
            if (document.checkMargin(datitab.GetRowDim() * 4))
                document.addPage();

            page = document.getCurrentPage();

            if (dataThread.TipoReport.ToUpper() == "DIAGNOSI")
                document.setSezTitoloDiagnosi(dataThread.SezioneReport.Titolo);
            else
                document.setSezTitolo(dataThread.SezioneReport.Titolo);

            DataView view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = 'Fondi'");
            if (view.Count > 0)
            {
                if (document.checkMargin(datitab.GetRowDim() * 4))
                    document.addPage();
                document.setTitolo("Fondi");
                this.writeArea(datitab, document, "Fondi", dataThread);
            }

            datitab.table = set.Tables["Prodotti"];
            view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = 'Gestioni patrimoniali'");
            if (view.Count > 0)
            {
                if (document.checkMargin(datitab.GetRowDim() * 4))
                    document.addPage();
                document.setTitolo("Gestioni patrimoniali");
                this.writeArea(datitab, document, "Gestioni patrimoniali", dataThread);
            }

            datitab.table = set.Tables["Prodotti"];
            view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = 'Prodotti di Tipo amministrato (escluso C/C)'");
            if (view.Count > 0)
            {
                if (document.checkMargin(datitab.GetRowDim() * 4))
                    document.addPage();
                document.setTitolo("Titoli");
                this.writeArea(datitab, document, "Prodotti di Tipo amministrato (escluso C/C)", dataThread);
            }

            datitab.table = set.Tables["Prodotti"];
            view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = 'Assicurativi Unit/Index Linked'");
            if (view.Count > 0)
            {
                if (document.checkMargin(datitab.GetRowDim() * 4))
                    document.addPage();
                document.setTitolo("Assicurativi Unit/Index Linked");
                this.writeArea(datitab, document, "Assicurativi Unit/Index Linked", dataThread);
            }

            datitab.table = set.Tables["Prodotti"];
            view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = 'Portafoglio Generico'");
            if (view.Count > 0)
            {
                if (document.checkMargin(datitab.GetRowDim() * 4))
                    document.addPage();


                document.setTitolo("Portafoglio Generico");
                this.writeArea(datitab, document, "Portafoglio Generico", dataThread);
            }
        }


        /// <summary>
        /// Verifica se nel datatable passato in input � presente almeno un campo della colonna  VarProxato con valore 1.
        /// Se si aggiungo al documento un oggetto Nota di tipo VarProxato e aggiungo al valore del campo VaR gli *.
        /// La S48 pu� avere le note a fondo pagina solo per i var proxati.
        /// </summary>
        /// <param name="dataTable">fonte dati</param>
        /// <param name="idtabellaassociata">id della tabella a cui andr� associata la nota</param>
        /// <param name="document">documento</param>
        private void ImpostaNote(DataTable dataTable, string idtabellaassociata, ref DocumentPDF document)
        {
            DataRow[] drVarProxato = dataTable.Select("VarProxato=1");

            if (drVarProxato.Length > 0)
                if (!document.Note.Exists(delegate(Nota t) { return t.TipoNota == Nota.TipologiaNota.VarProxato; }))
                    document.Note.Add(new Nota(Nota.TipologiaNota.VarProxato, idtabellaassociata, Resource.Nota_VarProxato));

            //aggiungo al valore del campo VaR gli *
            foreach (DataRow row in drVarProxato)
            {
                if (row["VaR"].ToString().ToLower() != "n.c.")
                    row["VaR"] += document.Note.Find(delegate(Nota nota) { return nota.TipoNota == Nota.TipologiaNota.VarProxato; }).ToString();

            }

        }

        /// <summary>
        /// Stampa la tabella.
        /// </summary>
        /// <param name="datitab"></param>
        /// <param name="document"></param>
        /// <param name="area"></param>
        /// <param name="dataThread"></param>
        private void writeArea(DatiTabella datitab, DocumentPDF document, string area, DataThread dataThread)
        {
            page = document.getCurrentPage();

            #region Disegna Tabella

            //setta la datatable
            datitab.table = set.Tables["Prodotti"];

            DataView view = new DataView(datitab.table);
            view.RowFilter = ("TipoProdotto = '" + area + "'");
            datitab.table = view.ToTable();
            //aggiungo una riga in pi� per fare posto alla scritta continua e uno per il totale

            Tabella tabellaDati = new Tabella(document.getMargineLeft(), document.getLastPos());
            if (dataThread.TipoReport.ToUpper() == "DIAGNOSI") tabellaDati.HeaderFont = 8;
            tabellaDati.Header = true;
            tabellaDati.Datasource = datitab.table;
            tabellaDati.Id = "tabellaS48_" + area;

            #region Settaggio delle Note di pi� di pagina, delle note a piedi della tabella e degli * per i varProxati

            //Imposto le nota a pie di pagina.            
            ImpostaNote(datitab.table, tabellaDati.Id, ref document);


            //Nota a piedi della tabella
            // Imposto la nota a piedi della Tabella   
            notaGradoCopertura = datitab.table.Rows[0]["Copertura"] == DBNull.Value ? string.Empty : datitab.table.Rows[0]["Copertura"].ToString();
            if (notaGradoCopertura.Length > 0)
            {
                document.Note.Add(new Nota(Nota.TipologiaNota.GradoCopertura, tabellaDati.Id, notaGradoCopertura));
                tabellaDati.NotaTabella.Add(document.Note.Find(Nota.TipologiaNota.GradoCopertura, tabellaDati.Id).Testo);
            }


            #endregion


            switch (dataThread.ReportType().TipoReport)
            {
                case TipoReport.DIAGNOSI:
                case TipoReport.MONITORAGGIO:

                    if (dataThread.NumeroIntermediari > 1)
                    {
                        tabellaDati.Colonne.Add(new Colonna("Intermediario", "Intermediario", 65, TipoAllineamento.SINISTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("DataSottoscrizione", "Data sott.", 75, TipoAllineamento.SINISTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Descrizione", "Descrizione", 116, TipoAllineamento.SINISTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Controvalore", "Controvalore (�)", 70, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Versato", "Versato netto (�)", 56, TipoAllineamento.DESTRA, false, 7, false));
                        //tabellaDati.Colonne.Add(new Colonna("MinusPlus", "Utile/<BR>perdita �", 80, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("CreditRisk", "Rischio<BR>Credito", 65, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 65, TipoAllineamento.DESTRA, false, 7, false));
                        //tabellaDati.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 50, TipoAllineamento.DESTRA, false, 7, false));
                    }
                    else
                    {
                        tabellaDati.Colonne.Add(new Colonna("DataSottoscrizione", "Data sott.", 65, TipoAllineamento.SINISTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Descrizione", "Descrizione", 157, TipoAllineamento.SINISTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Controvalore", "Controvalore (�)", 80, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Versato", "Versato netto (�)", 80, TipoAllineamento.DESTRA, false, 7, false));
                        //tabellaDati.Colonne.Add(new Colonna("MinusPlus", "Utile/perdita �", 80, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("CreditRisk", "Rischio<BR>Credito", 65, TipoAllineamento.DESTRA, false, 7, false));
                        tabellaDati.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 65, TipoAllineamento.DESTRA, false, 7, false));
                        //tabellaDati.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 50, TipoAllineamento.DESTRA, false, 7, false));
                    }
                    break;
            }

            datitab.setCellSpace(0);
            tabellaDati.Draw(datitab, document);


            #endregion


            #region Scrivi Totale
            datitab = new DatiTabella();
            datitab.table = set.Tables["ProdottiTotale"];

            DataView view2 = new DataView(datitab.table);
            view2.RowFilter = ("TipoProdotto = '" + area + "'");
            datitab.table = view2.ToTable();

            //Imposto gli asterischi di rimando nel caso in cui sia presente la nota del grado copertura
            if (notaGradoCopertura.Length > 0)
                datitab.table.Rows[0]["Var"] = datitab.table.Rows[0]["Var"].ToString() + document.Note.Find(Nota.TipologiaNota.GradoCopertura, tabellaDati.Id).ToString();


            Tabella tabellaTotale = new Tabella(document.getMargineLeft(), document.getLastPos() + tabellaDati.AltezzaTabella + 25);
            if (dataThread.TipoReport.ToUpper() == "DIAGNOSI") tabellaTotale.HeaderFont = 8;
            tabellaTotale.SaltoPagina = false;
            tabellaTotale.Header = false;

            switch (dataThread.ReportType().TipoReport)
            {
                case TipoReport.DIAGNOSI:
                case TipoReport.MONITORAGGIO:
                    if (dataThread.NumeroIntermediari > 1)
                    {
                        tabellaTotale.Colonne.Add(new Colonna("Totale", "", 256, TipoAllineamento.SINISTRA, true, 8, true));
                        tabellaTotale.Colonne.Add(new Colonna("Controvalore", "", 70, TipoAllineamento.DESTRA, true, 8, true));
                        tabellaTotale.Colonne.Add(new Colonna("Var", "", 186, TipoAllineamento.DESTRA, true, 8, true));
                    }
                    else
                    {
                        tabellaTotale.Colonne.Add(new Colonna("Totale", "", 222, TipoAllineamento.SINISTRA, true, 8, true));
                        tabellaTotale.Colonne.Add(new Colonna("Controvalore", "", 80, TipoAllineamento.DESTRA, true, 8, true));
                        tabellaTotale.Colonne.Add(new Colonna("Var", "", 210, TipoAllineamento.DESTRA, true, 8, true));
                    }

                    break;
            }


            tabellaTotale.Draw(datitab, document);

            document.setLastPos(tabellaTotale.AltezzaTabella + tabellaDati.AltezzaTabella);


            #endregion
        }
    }
}