using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

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

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

        string Titolo = " ";
        DataSetS43BIS set;

        ImportedPage page;
        private bool _versatonetto_utileperdita = false;
        private bool _mostrarendimentodainizioanno = false;
        private bool _mostrarendimentodadatasottoscrizione = false;
       // private bool _mostratitolosezione = false;
        
        private TipoReport _tipologiaReport = TipoReport.MONITORAGGIO; // la S43BIS è usata solo per il monitoraggio fase 2 (monitoraggio senza albero.)


        public S43BIS()
        {

        }

        string _Area;
        string _Progetto;



        #region ISezione Members


        public void writeSezione(DataThread dataThread)
        {
            DocumentPDF document = dataThread.DocumentPDF;
            DatiTabella datitab = new DatiTabella();
            set = (DataSetS43BIS)dataThread.Data.DatiSezione;

            
            //if (document.checkMargin(datitab.GetRowDim() * 5))  {
            //    document.addPage();
            //}
            dataThread.UltimaSezioneStampata = "";
          
            page = document.getCurrentPage();
            DataView view;
          

            if (_Area == null || _Area == "Liq")
            {
                datitab.table = set.Tables["ProdottiAreeBisogno"];
                view = new DataView(datitab.table);
                view.RowFilter = ("CodiceAreaBisogno = 'Liq'");
                if (view.Count > 0)
                {
                    if (document.checkMargin(datitab.GetRowDim() * 4))
                        document.addPage();
                   
                    this.writeArea(datitab, document, "Liq", dataThread);
                    
                }
            }

            if (Area == null || Area == "Ris")
            {

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

                    this.writeArea(datitab, document, "Ris", dataThread);
                }
            }

            if (Area == null || Area == "Pre")
            {
                datitab.table = set.Tables["ProdottiAreeBisogno"];
                view = new DataView(datitab.table);
                view.RowFilter = ("CodiceAreaBisogno = 'Pre'");
                if (view.Count > 0)   {
                    if (document.checkMargin(datitab.GetRowDim() * 4))
                        document.addPage();

                    this.writeArea(datitab, document, "Pre", dataThread);
                }
            }

            if (Area == null || Area == "Inv")
            {
                datitab.table = set.Tables["ProdottiAreeBisogno"];
                view = new DataView(datitab.table);
                view.RowFilter = ("CodiceAreaBisogno = 'Inv'");
                if (view.Count > 0)  {
                    if (document.checkMargin(datitab.GetRowDim() * 4))
                        document.addPage();

                    this.writeAreaProgetto(datitab, document, "Inv", dataThread.Progetto, dataThread);                        
                }
            }

            if (Area == null || Area == "Ext")
            {
                datitab.table = set.Tables["ProdottiAreeBisogno"];
                view = new DataView(datitab.table);
                view.RowFilter = ("CodiceAreaBisogno = 'Ext'");
                if (view.Count > 0)
                {

                    if (document.checkMargin(datitab.GetRowDim() * 4))
                        document.addPage();

                        this.writeArea(datitab, document, "Ext", dataThread);
                }
            }

            if (Area == null || Area == "Na")
            {
                // Risorse non associate
                datitab.table = set.Tables["ProdottiAreeBisogno"];
                view = new DataView(datitab.table);
                view.RowFilter = ("CodiceAreaBisogno = 'Na'");
                if (view.Count > 0)   {
                    if (Area == null)
                        if (document.checkMargin(datitab.GetRowDim() * 4))
                            document.addPage();

                    this.writeArea(datitab, document, "Na", dataThread);

                }
            }
        }

        #endregion


        /// <summary>
        /// Disegna la tabella per il dettaglio monitoraggio x Area. 
        /// </summary>
        /// <param name="datitab"></param>
        /// <param name="document"></param>
        /// <param name="area"></param>
        private void writeArea(DatiTabella datitab, DocumentPDF document, string area, DataThread dataThread)
        {
            float dim = 0;
            page = document.getCurrentPage();
            FormatNum conv = new FormatNum();
            string notaGradoCopertura = string.Empty;

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

            // filtro per l'area
            DataView view = new DataView(datitab.table);
            view.RowFilter = ("CodiceAreaBisogno = '" + area + "'");
            datitab.table = view.ToTable();

            #region Tabella Dati

            // Disegno la tabella
            Tabella tabellaDettaglio_Area = new Tabella(document.getMargineLeft(), document.getLastPos());
            tabellaDettaglio_Area.HeaderFont = 8;
            tabellaDettaglio_Area.Header = true;
            tabellaDettaglio_Area.Datasource = datitab.table;
            tabellaDettaglio_Area.Id = "tabella_S43BIS" + area;
            tabellaDettaglio_Area.AltezzaCella = 30;


            #region Settaggio delle Note di piè di pagina e delle note a piedi della tabella


            //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, tabellaDettaglio_Area.Id, notaGradoCopertura));
                tabellaDettaglio_Area.NotaTabella.Add(document.Note.Find(Nota.TipologiaNota.GradoCopertura, tabellaDettaglio_Area.Id).Testo);
            }


            #endregion

            tabellaDettaglio_Area.Colonne.Add(new Colonna("DataAssociazione", "Data assoc.", 60, TipoAllineamento.SINISTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("Descrizione", "Descrizione", 87, TipoAllineamento.SINISTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("<IMAGE>", " ", 5, TipoAllineamento.SINISTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("Controvalore", "Controvalore (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("VersatoNetto", "Versato netto<BR>da assoc. (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("UtilePerdita", "Utile/perdita<BR>da assoc. (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("CreditRisk", "Rischio<BR>Credito", 65, TipoAllineamento.DESTRA, false, 7, false));
            tabellaDettaglio_Area.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 55, TipoAllineamento.DESTRA, false, 7, false));
           
            // Scrivo la tabella tabella Dati
            tabellaDettaglio_Area.Draw(datitab, document);
            #endregion Tabella Dati

            #region Tabella Totali

            datitab = new DatiTabella();
            datitab.table = set.Tables["ProdottiTotale"];
            datitab.CellClear();

            //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, tabellaDettaglio_Area.Id).ToString();


            Tabella tabellaTotali_Area = new Tabella(document.getMargineLeft(), document.getLastPos() + tabellaDettaglio_Area.AltezzaTabella + 25);
            tabellaTotali_Area.HeaderFont = 8;
            tabellaTotali_Area.Header = false;
            tabellaTotali_Area.SaltoPagina = false;
           

            tabellaTotali_Area.Colonne.Add(new Colonna("Totale", "", 122, TipoAllineamento.SINISTRA, true, 8, true));
            tabellaTotali_Area.Colonne.Add(new Colonna("Controvalore", "", 110, TipoAllineamento.DESTRA, true, 8, true));
            tabellaTotali_Area.Colonne.Add(new Colonna("Var", "", 280, TipoAllineamento.DESTRA, true, 8, true));

            // it should fix problem
            if (document.checkMargin(datitab.GetRowDim() * 4 + 20)) // + 20 because of '(*) Grado di copertura: 80,29%'
                document.addPage();

            // Scrive la tabella Totali
            tabellaTotali_Area.Draw(datitab, document);

            dim = tabellaTotali_Area.AltezzaTabella + tabellaDettaglio_Area.AltezzaTabella;

            document.setLastPos(dim);



            #endregion

        }


        /// <summary>
        /// Disegna la tabella per il dettaglio monitoraggio x Area/Progetto.
        /// Usata per area Investimento. 
        /// </summary>
        /// <param name="datitab"></param>
        /// <param name="document"></param>
        /// <param name="area"></param>
        private void writeAreaProgetto(DatiTabella datitab, DocumentPDF document, string area, string nomeprogetto, DataThread dataThread)
        {
            //bool checkPartiteViaggiantiCrossProgetto = false;
            FormatNum conv = new FormatNum();
            float dim = 0;

            page = document.getCurrentPage();

            //decimal totProgetto = 0;
           // string varProgetto = " ";
//bool stampatatabellaTitoloProgetto = false;
           // float ytabella = 0; // unico incremento per la y di tutte le tabelle.
            string notaGradoCopertura = string.Empty;


            //Tabella tabellaDatiProgetto = null;
            //Tabella tabTotali = null;


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

            // filtro per l'area
            DataView view = new DataView(datitab.table);
            view.RowFilter = ("CodiceAreaBisogno = '" + area + "' and Progetto='" + nomeprogetto.Replace("'", "''") + "'");
            datitab.table = view.ToTable();

            

            if (datitab.table.Rows.Count > 0)
            {
                #region Tabella Dati

                // Disegno la tabella
                //Tabella tabellaDettaglio_Area = new Tabella(document.getMargineLeft(), document.getLastPos() + ytabella);
                Tabella tabellaDettaglio_Area = new Tabella(document.getMargineLeft(), document.getLastPos());
                tabellaDettaglio_Area.HeaderFont = 8;
                tabellaDettaglio_Area.Header = true;
                tabellaDettaglio_Area.Datasource = datitab.table;
                tabellaDettaglio_Area.Id = "tabella_S43BIS" + area + "_" + nomeprogetto;
                tabellaDettaglio_Area.AltezzaCella = 30;


                #region Settaggio delle Note a piedi della tabella

                //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, tabellaDettaglio_Area.Id, notaGradoCopertura));
                    tabellaDettaglio_Area.NotaTabella.Add(document.Note.Find(Nota.TipologiaNota.GradoCopertura, tabellaDettaglio_Area.Id).Testo);
                }


                #endregion

                tabellaDettaglio_Area.Colonne.Add(new Colonna("DataAssociazione", "Data assoc.", 60, TipoAllineamento.SINISTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("Descrizione", "Descrizione", 87, TipoAllineamento.SINISTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("<IMAGE>", " ", 5, TipoAllineamento.SINISTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("Controvalore", "Controvalore (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("VersatoNetto", "Versato netto<BR>da assoc. (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("UtilePerdita", "Utile/perdita<BR>da assoc. (€)", 80, TipoAllineamento.DESTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("CreditRisk", "Rischio<BR>Credito", 65, TipoAllineamento.DESTRA, false, 7, false));
                tabellaDettaglio_Area.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 55, TipoAllineamento.DESTRA, false, 7, false));
                               
                // Scrivo la tabella tabella Dati
                tabellaDettaglio_Area.Draw(datitab, document);
                #endregion Tabella Dati

                #region Tabella Totali

                datitab = new DatiTabella();
                datitab.table = set.Tables["ProdottiTotale"];
                datitab.CellClear();

                //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, tabellaDettaglio_Area.Id).ToString();


                Tabella tabellaTotali_Area = new Tabella(document.getMargineLeft(), document.getLastPos() + tabellaDettaglio_Area.AltezzaTabella + 25);
                tabellaTotali_Area.HeaderFont = 8;
                tabellaTotali_Area.Header = false;
                tabellaTotali_Area.SaltoPagina = false;                   
                 
                tabellaTotali_Area.Colonne.Add(new Colonna("Totale", "", 130, TipoAllineamento.SINISTRA, true, 8, true));
                tabellaTotali_Area.Colonne.Add(new Colonna("Controvalore", "", 112, TipoAllineamento.DESTRA, true, 8, true));
                tabellaTotali_Area.Colonne.Add(new Colonna("Var", "", 270, TipoAllineamento.DESTRA, true, 8, true));
                

                // Scrive la tabella Totali
                tabellaTotali_Area.Draw(datitab, document);

                dim = tabellaTotali_Area.AltezzaTabella + tabellaDettaglio_Area.AltezzaTabella;

                document.setLastPos(dim);

                #endregion
            }

        }



        public void setTitolo(string label)
        {
            Titolo = label;
        }

        public string Area
        {
            get
            {
                return _Area;
            }
            set
            {
                _Area = value;
            }
        }

        public string Progetto
        {
            get
            {
                return _Progetto;
            }
            set
            {
                _Progetto = value;
            }
        }

        public bool VersatoNetto_UtilePerdita
        {
            get { return _versatonetto_utileperdita; }
            set { _versatonetto_utileperdita = value; }
        }

        public bool MostraRendimentoDaInizioAnno
        {
            get { return _mostrarendimentodainizioanno; }
            set { _mostrarendimentodainizioanno = value; }
        }

        public bool MostraRendimentoDaDataSottoscrizione
        {
            get { return _mostrarendimentodadatasottoscrizione; }
            set { _mostrarendimentodadatasottoscrizione = value; }
        }

        public TipoReport TipologiaReport
        {
            get { return _tipologiaReport; }
            set { _tipologiaReport = value; }
        }
    }
}