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 PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer;
using ContrattoSei.Utilities;

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

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

        #region ISezione Members

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

            //setta la datatable
            datitab.table = set.Tables["PatrimonioBFAreeBisogno"];
            DataView view = new DataView(datitab.table);
            view.RowFilter = "";
            datitab.table = view.ToTable(false, setTableForTable());

            //calcola se entra nella pagina altrimenti aggiunge una nuova pagina. In questa sezione il # di righe � fissato
            //devo considerare lo spazio dell'header- ora lo aggiungo manualmente
            if (document.checkMargin(datitab.GetRowDim() * 20))
                document.addPage();


            //aggiorna l'indice, titolo e header sezione
            document.setSezTitolo(dataThread.SezioneReport.Titolo,true);
            document.setSezHeader(dataThread.SezioneReport.TestoIntroduttivo);
            // document.setHeaderSpace(20);

            //disegno la parte della piramide
            Piramide modello = new Piramide();
            modello.writeSezione(document);

            //settaggi dell'oggetto tabella
            datitab.setX(200);
            datitab.setCellSpace(0);
            datitab.setY(document.getLastPos());
            datitab.setHeader();
            datitab.SetRowDim(30);
            datitab.setCell(125, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);

            Tabella tab = new Tabella();
            //setta posizione tabella
            document.InsertTable(tab.getTabella(datitab));

            int nota = 0;

            datitab.table = view.ToTable(false, "Nota");
            nota = this.setDangerIcon(datitab, document);
            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();

            //aggiungo una riga in pi� nel conteggio per l'header
            float dim = datitab.GetRowDim() * (datitab.getNumRow() + 1);

            //==========GRAFICO===========================================================================
            DatiGrafico dati = new DatiGrafico();
            dati.dataTab = view.ToTable(false, setTableForChart());
            dati.setHeight(185);
            dati.setWidth(123);
            dati.ActLabelSeries();
            IstoDoppio isto = new IstoDoppio();
            document.InsertGrafico(isto.getGrafico(dati), document.getMargineLeft() + 395, document.getLastPos() + 14);
            //============================================================================================


            float yTabelleRisorsenonAllocate = 0;
            //Controllo se sono presenti risorse non allocate.
            if ((bool)set.Tables["TotalePiramide"].Rows[0]["HasRisorseNonAllocate"])
            {
                yTabelleRisorsenonAllocate = document.getLastPos() + dim + 30;

                //TOTALE RISORSE ALLOCATE
                DataTable dtTotaleRisorseAllocate;
                DataView dwTotaleRisorseAllocate = new DataView(set.Tables["TotalePiramide"]);
                dwTotaleRisorseAllocate.RowFilter = " DESCRIZIONE= 'TOTALE RISORSE ALLOCATE'";
                dtTotaleRisorseAllocate = dwTotaleRisorseAllocate.ToTable();
                datitab = new DatiTabella();
                datitab.table = dtTotaleRisorseAllocate;

                Tabella tabTotaleRisorseAllocate = new Tabella(document.getMargineLeft(), yTabelleRisorsenonAllocate);
                tabTotaleRisorseAllocate.Datasource = datitab.table;
                tabTotaleRisorseAllocate.Header = false;
                tabTotaleRisorseAllocate.Colonne.Add(new Colonna("Descrizione", "Descrizione", 195, TipoAllineamento.SINISTRA, false, 8, true));
                tabTotaleRisorseAllocate.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 90, TipoAllineamento.DESTRA, false, 8, true));
                tabTotaleRisorseAllocate.Colonne.Add(new Colonna("CoperturaPercentuale", "CoperturaPercentuale", 100, TipoAllineamento.DESTRA, false, 8, true));
                tabTotaleRisorseAllocate.Draw(datitab, document);


                //Risorse non allocate
                yTabelleRisorsenonAllocate += 23;
                DataTable dtRisorsenonallocate;
                DataView dwRisorsenonallocate = new DataView(set.Tables["TotalePiramide"]);
                dwRisorsenonallocate.RowFilter = " DESCRIZIONE= 'Risorse non allocate'";
                dtRisorsenonallocate = dwRisorsenonallocate.ToTable();
                datitab = new DatiTabella();
                datitab.table = dtRisorsenonallocate;

                Tabella tabRisorsenonallocate = new Tabella(document.getMargineLeft(), yTabelleRisorsenonAllocate);
                tabRisorsenonallocate.Datasource = datitab.table;
                tabRisorsenonallocate.Header = false;
                tabRisorsenonallocate.Colonne.Add(new Colonna("Descrizione", "Descrizione", 195, TipoAllineamento.SINISTRA, false, 8, false));
                tabRisorsenonallocate.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 90, TipoAllineamento.DESTRA, false, 8, false));
                tabRisorsenonallocate.Colonne.Add(new Colonna("CoperturaPercentuale", "CoperturaPercentuale", 100, TipoAllineamento.DESTRA, false, 8, false));
                tabRisorsenonallocate.Draw(datitab, document);


                //TOTALE RISORSE FINANZIARIE
                //Risorse non allocate
                yTabelleRisorsenonAllocate += 23;

                DataTable dtTOTALERISORSEFINANZIARIE;
                DataView dwTOTALERISORSEFINANZIARIE = new DataView(set.Tables["TotalePiramide"]);
                dwTOTALERISORSEFINANZIARIE.RowFilter = " DESCRIZIONE= 'TOTALE RISORSE FINANZIARIE'";
                dtTOTALERISORSEFINANZIARIE = dwTOTALERISORSEFINANZIARIE.ToTable();
                datitab = new DatiTabella();
                datitab.table = dtTOTALERISORSEFINANZIARIE;

                Tabella tabTOTALERISORSEFINANZIARIE = new Tabella(document.getMargineLeft(), yTabelleRisorsenonAllocate);
                tabTOTALERISORSEFINANZIARIE.Datasource = datitab.table;
                tabTOTALERISORSEFINANZIARIE.Header = false;
                tabTOTALERISORSEFINANZIARIE.Colonne.Add(new Colonna("Descrizione", "Descrizione", 195, TipoAllineamento.SINISTRA, true, 8, true));
                tabTOTALERISORSEFINANZIARIE.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 90, TipoAllineamento.DESTRA, true, 8, true));
                tabTOTALERISORSEFINANZIARIE.Colonne.Add(new Colonna("CoperturaPercentuale", "CoperturaPercentuale", 100, TipoAllineamento.DESTRA, true, 8, true));
                tabTOTALERISORSEFINANZIARIE.Draw(datitab, document);

                dim += (datitab.GetRowDim() * 3) + 5;
            }
            else
            {
                // modifica 23/02/2009
                yTabelleRisorsenonAllocate = document.getLastPos() + dim + 30;

                Tabella tabTotalePiramide = new Tabella(document.getMargineLeft(), yTabelleRisorsenonAllocate);
                tabTotalePiramide.Datasource = set.Tables["TotalePiramide"];
                datitab = new DatiTabella();
                datitab.table = set.Tables["TotalePiramide"];
                tabTotalePiramide.Header = false;
                tabTotalePiramide.Colonne.Add(new Colonna("Descrizione", "Descrizione", 195, TipoAllineamento.SINISTRA, true, 8, true));
                tabTotalePiramide.Colonne.Add(new Colonna("Var", "Rischio<BR>(VaR %)", 90, TipoAllineamento.DESTRA, true, 8, true));
                tabTotalePiramide.Colonne.Add(new Colonna("CoperturaPercentuale", "CoperturaPercentuale", 100, TipoAllineamento.DESTRA, true, 8, true));
                tabTotalePiramide.Draw(datitab, document);
                dim += datitab.GetRowDim() * (datitab.getNumRow()) + 5;
                // * modifica 23/02/2009
            }


            //datitab = new DatiTabella();
            //datitab.table = set.Tables["TotaleAreeBisogno"];
            //view = new DataView(datitab.table);
            //view.RowFilter = "";
            //datitab.table = view.ToTable(false, setTableForTotal());

            //datitab.setY(document.getLastPos() + dim);
            //datitab.setX(document.getMargineLeft());
            //datitab.setCell(195, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
            //datitab.setCell(90, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            //datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            //document.InsertTable(tab.getTabella(datitab));

            //dim += datitab.GetRowDim() * (datitab.getNumRow()) + 5;

            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() - 5 + 115, document.getLastPos() + dim - 10, 260, 23, 0, new RgbColor(232, 236, 237)));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Piramide attuale              Piramide modello", document.getMargineLeft() - 5 + 135, document.getLastPos() + dim - 5, 320, 23, Globals.OpenTypeFontVerdanaB, 9, ceTe.DynamicPDF.CmykColor.Red));

            dim += datitab.GetRowDim();
            datitab = new DatiTabella();
            datitab.table = set.Tables["PatrimonioBFAreeBisogno"];
            view = new DataView(datitab.table);
            view.RowFilter = "";
            datitab.table = view.ToTable(false, setTableForTable2());

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setHeader();
            datitab.setIsLinee(1);
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(45, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(45, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(80, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(72, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);

            document.InsertTable(tab.getTabella(datitab));
            dim += datitab.GetRowDim() * (datitab.getNumRow() + 1);

            datitab = new DatiTabella();
          
            datitab.table = set.Tables["TotaleAreeBisogno"];
            view = new DataView(datitab.table);
            view.RowFilter = "";
            datitab.table = view.ToTable(false, setTableForTotal2());

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(45, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(45, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() - 5, document.getLastPos() + dim - 10, 520, 23, 0, new RgbColor(232, 236, 237)));
            document.InsertTable(tab.getTabella(datitab));

            dim += datitab.GetRowDim() * (datitab.getNumRow());

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

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            document.InsertTable(tab.getTabella(datitab));

            dim += datitab.GetRowDim() * (datitab.getNumRow());


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

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            document.InsertTable(tab.getTabella(datitab));

            dim += datitab.GetRowDim() * (datitab.getNumRow());

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

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            document.InsertTable(tab.getTabella(datitab));

            dim += datitab.GetRowDim() * (datitab.getNumRow());

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

            datitab.setY(document.getLastPos() + dim);
            datitab.setX(document.getMargineLeft());
            datitab.setCell(100, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(85, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);
            //page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() - 5, document.getLastPos() + dim - 10, 405, 23, 0, new RgbColor(232, 236, 237)));
            document.InsertTable(tab.getTabella(datitab));

            dim += datitab.GetRowDim() * (datitab.getNumRow());

            //disegno la grafica decorativa del grafico
            this.headerChart(page, document.getMargineLeft() + 396, document.getLastPos());

            if (nota > 0) {
                string relativePath = UtilityManager.getAppSetting("Danger");
                System.Reflection.Assembly thisExe;
                thisExe = System.Reflection.Assembly.GetExecutingAssembly();
                System.IO.Stream file = thisExe.GetManifestResourceStream(relativePath);


                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() - 5, document.getLastPos() + dim, 0.2F));
                if (nota == 1)
                    document.setSezFooter(Resource.S56_S57_Nota, dim, 18);
                else
                    document.setSezFooter(Resource.S56_S57_NotaVar, dim, 18);
            }

            //dim += datitab.GetRowDim();
            //devo considerare lo spazio dell'header- ora lo aggiungo manualmente
            document.setLastPos(dim);
        }
        #endregion

        private void headerChart(ceTe.DynamicPDF.Page page, float x, float y) {
            //Header grafico a farfalla
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(x + 100, y + 2, 10, 5, 0, new RgbColor(121, 142, 185)));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(x + 100, y - 9, 10, 5, 0, new RgbColor(239, 65, 53)));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Piramide Modello", x - 25, y, 120, 10, Globals.OpenTypeFontVerdana, 9, ceTe.DynamicPDF.TextAlign.Right));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Piramide Attuale", x - 25, y - 11, 120, 10, Globals.OpenTypeFontVerdana, 9, ceTe.DynamicPDF.TextAlign.Right));
            //////
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(x, y + 170, 122, 23, 0, new RgbColor(232, 236, 237)));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("0%     25%     50%    75%  100%", x, y + 176, 320, 10, Globals.OpenTypeFontVerdana, 7, ceTe.DynamicPDF.TextAlign.Left));
        }

        private int setDangerIcon(DatiTabella datitab, DocumentPDF document) {
            int nota = 0;
            float y = document.getLastPos() + 23;
            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();

            string relativePath = UtilityManager.getAppSetting("Danger");

            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file = thisExe.GetManifestResourceStream(relativePath);

            foreach (DataRow myRow in datitab.table.Rows)
                if (!myRow.IsNull("Nota") && ((int)myRow["Nota"] == 1 || (int)myRow["Nota"] == 2)) {
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, 370, y + 2, 0.2F));
                    y += datitab.GetRowDim();
                    nota = (int)myRow["Nota"];
                }
                else
                    y += datitab.GetRowDim();
            return nota;
        }

        private string[] setTableForTable() {
            //eventuali operazioni sul dataset
            string[] Columns = new String[2];
            Columns[0] = "Var";
            Columns[1] = "GradoCopertura";
            return Columns;
        }

        private string[] setTableForTotal() {
            //eventuali operazioni sul dataset
            string[] Columns = new String[3];
            Columns[0] = "DescrTotale";
            Columns[1] = "Var";
            Columns[2] = "GradoCopertura";
            return Columns;
        }

        private string[] setTableForTable2() {
            //eventuali operazioni sul dataset
            string[] Columns = new String[7];
            Columns[0] = "DescrAreaBisogno";
            Columns[1] = "ControvaloreAttuale";
            Columns[2] = "PercentualeAttuale";
            Columns[3] = "ControvaloreModello";
            Columns[4] = "PercentualeModello";
            Columns[5] = "DifferenzaEuro";
            Columns[6] = "DifferenzaPercentuale";

            return Columns;
        }

        private string[] setTableForTotal2() {
            //eventuali operazioni sul dataset
            string[] Columns = new String[5];
            Columns[0] = "DescrTotale";     
            Columns[1] = "ControvaloreAttuale";
            Columns[2] = "PercentualeAttuale";
            Columns[3] = "ControvaloreModello";
            Columns[4] = "PercentualeModello";

            return Columns;
        }

        private string[] setTableForChart() {
            string[] Columns = new String[2];
            Columns[0] = "PercentualeModello";
            Columns[1] = "PercentualeAttuale";
            return Columns;
        }


    }
}