using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using ceTe.DynamicPDF.Text;
using System.Collections;
using System.Collections.Generic;
using ceTe.DynamicPDF;
using PDFGenerator.Presentation.Section;
using PDFGenerator;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer;
using PDFGenerator.BusinessLayer.DataSection;

/// <summary>
/// Summary description for S157
/// </summary>
/// 
public class S157 : ISezione
{
    string Titolo = string.Empty;
    string _testotitolo;
    string _testointroduttivo;

    /// <summary>
    /// Testo introduttivo della sezione.
    /// </summary>
    public string TestoIntroduttivo
    {
        get
        {
            return _testointroduttivo;
        }
        set
        {
            _testointroduttivo = value;
        }
    }

    /// <summary>
    /// Testo del titolo della sezione.
    /// </summary>
    public string TestoTitolo
    {
        get
        {
            return _testotitolo;
        }
        set
        {
            _testotitolo = value;
        }
    }


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

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

    public void writeSezione(DataThread dataThread)
    {

        DocumentPDF document = dataThread.DocumentPDF;
        DatiTabella datitab = new DatiTabella();
        DataSetS157 set = (DataSetS157)dataThread.Data.DatiSezione;
        bool _mostracolonnacopertura = false;

        #region Resulset- Verifica disegno colonna "copertura"
        datitab.table = set.Tables["ResultSet"];

        #endregion


        if (document.checkMargin(300))
            document.addPage();


        ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
        document.setSezTitolo(_testotitolo);
        document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo, 0, 520, 8);

        // Modifica per Testo Verticale

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

        datitab.table = set.Tables["GraficoPiramide"];
        DataView view = new DataView(datitab.table);
        view.RowFilter = "";
        datitab.table = view.ToTable(false, "Controvalore", "Rischio", "Copertura");


        Tabella tabella = new Tabella();
        tabella.HeaderFont = 8;
        tabella.LineeTabella = false;
        tabella.Y = document.getLastPos();
        tabella.X = 210;
        tabella.Header = true;
        tabella.SaltoPagina = false;
        tabella.Datasource = datitab.table;
        tabella.AltezzaCella = 30;

        if (_mostracolonnacopertura)
        {

            tabella.Colonne.Add(new Colonna("Controvalore", "Controvalore (€)", 70, TipoAllineamento.DESTRA, false, 9, false));
            tabella.Colonne.Add(new Colonna("Rischio", "Rischio <BR> (VaR %)", 50, TipoAllineamento.DESTRA, false, 9, false));
            tabella.Colonne.Add(new Colonna("Copertura", "Copertura <BR> (%)", 60, TipoAllineamento.DESTRA, false, 9, false));
        }
        else
        {
            tabella.Colonne.Add(new Colonna("Controvalore", "Controvalore (€)", 100, TipoAllineamento.DESTRA, false, 9, false));
            tabella.Colonne.Add(new Colonna("Rischio", "Rischio <BR> (VaR %)", 80, TipoAllineamento.DESTRA, false, 9, false));

        }
        tabella.Draw(datitab, document);
        #endregion

        #region Grafico a Farfalla

        DatiGrafico dati = new DatiGrafico();

        dati.dataTab = set.Tables["GraficoFarfalla"];
        dati.setHeight(188);
        dati.setWidth(163);
        dati.ActLabelSeries();
        IstoFarfalla farfalla = new IstoFarfalla();
        document.InsertGrafico(farfalla.getGrafico(dati), 400, document.getLastPos() + 14);

        // header del grafico
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Piramide Modello", tabella.X + 190, tabella.Y - 5, 120, 10, Globals.OpenTypeFontVerdanaB, 8, ceTe.DynamicPDF.TextAlign.Left));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Piramide Attuale", tabella.X + 275, tabella.Y - 5, 120, 10, Globals.OpenTypeFontVerdanaB, 8, ceTe.DynamicPDF.TextAlign.Left));

        //footer del grafico
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(tabella.X + 190, tabella.Y + 173, 163, 23, 0, new RgbColor(232, 236, 237)));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(" 100%      50%          0%          50%    100%", tabella.X + 190, tabella.Y + 179, 320, 10, Globals.OpenTypeFontVerdana, 7, ceTe.DynamicPDF.TextAlign.Left));
        #endregion



        #region Tabella per il Totale delle Risorse Allocate
        //setta la datatable
        datitab = new DatiTabella();
        datitab.table = set.Tables["RisorseAllocate"];

        Tabella tabellaRisorseAllocate = new Tabella();
        tabellaRisorseAllocate.HeaderFont = 9;
        tabellaRisorseAllocate.X = document.getMargineLeft();
        tabellaRisorseAllocate.Y = document.getLastPos() + tabella.AltezzaTabella + 40;
        tabellaRisorseAllocate.SaltoPagina = false;
        tabellaRisorseAllocate.Datasource = datitab.table;
        tabellaRisorseAllocate.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, true));

        if (_mostracolonnacopertura)
        {
            tabellaRisorseAllocate.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseAllocate.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseAllocate.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, false, 9, true));
        }
        else
        {
            tabellaRisorseAllocate.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseAllocate.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, false, 9, true));
        }
        if (set.Tables["RisorseNonAllocate"].Rows.Count > 0)
            tabellaRisorseAllocate.Draw(datitab, document);
        #endregion

        #region Tabella per il Totale delle Risorse non Allocate  

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

        Tabella tabellaRisorseNonAllocate = new Tabella();
        tabellaRisorseNonAllocate.HeaderFont = 9;
        tabellaRisorseNonAllocate.X = document.getMargineLeft();
        tabellaRisorseNonAllocate.Y = document.getLastPos() + tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + 40;
        tabellaRisorseNonAllocate.SaltoPagina = false;
        tabellaRisorseNonAllocate.Datasource = datitab.table;
        tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, false));

        if (_mostracolonnacopertura)
        {
            tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, false));
            tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, false, 9, false));
            tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, false, 9, false));
        }
        else
        {
            tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, false));
            tabellaRisorseNonAllocate.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, false, 9, false));

        }

        if (set.Tables["RisorseNonAllocate"].Rows.Count > 0)
            tabellaRisorseNonAllocate.Draw(datitab, document);

        #endregion

        #region Tabella per il Totale delle Risorse Finanaziarie

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

        Tabella tabellaRisorseFinanaziarie = new Tabella();
        tabellaRisorseFinanaziarie.HeaderFont = 9;
        tabellaRisorseFinanaziarie.X = document.getMargineLeft();
        tabellaRisorseFinanaziarie.Y = document.getLastPos() + tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + tabellaRisorseNonAllocate.AltezzaTabella + 40;
        tabellaRisorseFinanaziarie.SaltoPagina = false;
        tabellaRisorseFinanaziarie.Datasource = datitab.table;
        tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, true));

        if (_mostracolonnacopertura)
        {
            tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, false, 9, true));
        }
        else
        {
            tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, true));
            tabellaRisorseFinanaziarie.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, false, 9, true));
        }

        //if (
        //    ((set.Tables["RisorseAllocate"].Rows.Count == 0 || set.Tables["RisorseNonAllocate"].Rows.Count == 0) && set.Tables["PartiteViaggianti"].Rows.Count > 0) || ((set.Tables["RisorseAllocate"].Rows.Count == 0 || set.Tables["RisorseNonAllocate"].Rows.Count == 0) && dataThread.TotalNegativeCurrentAccountValue < 0)
        //    ||
        //    ((set.Tables["RisorseAllocate"].Rows.Count > 0 && set.Tables["RisorseNonAllocate"].Rows.Count > 0 && set.Tables["PartiteViaggianti"].Rows.Count > 0) || (set.Tables["RisorseAllocate"].Rows.Count > 0 && set.Tables["RisorseNonAllocate"].Rows.Count > 0 && dataThread.TotalNegativeCurrentAccountValue < 0))
        //   )
        if (set.Tables["RisorseNonAllocate"].Rows.Count > 0
            || set.Tables["RisorseNonAllocate"].Rows.Count > 0
                || dataThread.TotalNegativeCurrentAccountValue < 0
                    || set.Tables["PartiteViaggianti"].Rows.Count > 0
                        || dataThread.TotalSelfNegCurrentAccountValue < 0)
            tabellaRisorseFinanaziarie.Draw(datitab, document);

        #endregion

        #region Tabella per il Conto Corrente
        datitab = new DatiTabella();
        datitab.table = set.Tables["ContoCorrente"];

        Tabella tabellaContoCorrente = new Tabella();
        tabellaContoCorrente.HeaderFont = 9;


        tabellaContoCorrente.X = document.getMargineLeft();
        tabellaContoCorrente.Y = document.getLastPos() + tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + tabellaRisorseNonAllocate.AltezzaTabella + tabellaRisorseFinanaziarie.AltezzaTabella + 40;
        tabellaContoCorrente.SaltoPagina = false;
        tabellaContoCorrente.Datasource = datitab.table;
        tabellaContoCorrente.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, false));
        if (_mostracolonnacopertura)
        {
            tabellaContoCorrente.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, false));
            tabellaContoCorrente.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, false, 9, false));
            tabellaContoCorrente.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, false, 9, false));
        }
        else
        {
            tabellaContoCorrente.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, false));
            tabellaContoCorrente.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, false, 9, false));
        }

        if (dataThread.TotalNegativeCurrentAccountValue < 0)
            tabellaContoCorrente.Draw(datitab, document);


        #endregion

        #region Tabella per il Linea Self Negativa
        datitab = new DatiTabella();
        datitab.table = set.Tables["LineaSelf"];

        Tabella tabellaLineaSelfNegativa = new Tabella();
        tabellaLineaSelfNegativa.HeaderFont = 9;
        tabellaLineaSelfNegativa.X = document.getMargineLeft();
        tabellaLineaSelfNegativa.Y = document.getLastPos() + tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + tabellaRisorseNonAllocate.AltezzaTabella + tabellaRisorseFinanaziarie.AltezzaTabella + tabellaContoCorrente.AltezzaTabella + 40;
        tabellaLineaSelfNegativa.SaltoPagina = false;
        tabellaLineaSelfNegativa.Datasource = datitab.table;
        tabellaLineaSelfNegativa.LineeTabella = false;
        tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, false));
        if (_mostracolonnacopertura)
        {
            tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, false));
            tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, false, 9, false));
            tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, false, 9, false));
        }
        else
        {
            tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, false));
            tabellaLineaSelfNegativa.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, false, 9, false));
        }

        if (dataThread.TotalSelfNegCurrentAccountValue < 0)
            tabellaLineaSelfNegativa.Draw(datitab, document);

        #endregion


        #region Tabella per gli Investimenti in corso o Partite Viaggianti
        datitab = new DatiTabella();
        datitab.table = set.Tables["PartiteViaggianti"];

        Tabella tabellaPartiteViaggianti = new Tabella();
        tabellaPartiteViaggianti.HeaderFont = 9;

        tabellaPartiteViaggianti.X = document.getMargineLeft();
        tabellaPartiteViaggianti.Y = document.getLastPos() + tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + tabellaRisorseNonAllocate.AltezzaTabella + tabellaRisorseFinanaziarie.AltezzaTabella + tabellaContoCorrente.AltezzaTabella + tabellaLineaSelfNegativa.AltezzaTabella + 40;
        tabellaPartiteViaggianti.SaltoPagina = false;
        tabellaPartiteViaggianti.Datasource = datitab.table;
        tabellaPartiteViaggianti.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, false, 9, false));
        if (_mostracolonnacopertura)
            tabellaPartiteViaggianti.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, false, 9, false));
        else
            tabellaPartiteViaggianti.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, false, 9, false));

        if (set.Tables["PartiteViaggianti"].Rows.Count > 0)
            tabellaPartiteViaggianti.Draw(datitab, document);

        #endregion



        #region Tabella per il Totale Patrimonio
        datitab = new DatiTabella();
        datitab.table = set.Tables["TotalePatrimonio"];

        float YtabellaTotalePatrimonio = tabella.AltezzaTabella + tabellaRisorseAllocate.AltezzaTabella + tabellaRisorseNonAllocate.AltezzaTabella + tabellaRisorseFinanaziarie.AltezzaTabella + tabellaContoCorrente.AltezzaTabella + tabellaPartiteViaggianti.AltezzaTabella + tabellaLineaSelfNegativa.AltezzaTabella + 40;
        float XtabellaTotalePatrimonio = document.getMargineLeft();

        Tabella tabellaTotalePatrimonio = new Tabella();
        tabellaTotalePatrimonio.HeaderFont = 9;
        tabellaTotalePatrimonio.X = XtabellaTotalePatrimonio;
        tabellaTotalePatrimonio.Y = document.getLastPos() + YtabellaTotalePatrimonio;
        tabellaTotalePatrimonio.SaltoPagina = false;
        tabellaTotalePatrimonio.Datasource = datitab.table;
        tabellaTotalePatrimonio.Colonne.Add(new Colonna("Descrizione", "", 170, TipoAllineamento.SINISTRA, true, 9, true));
        if (_mostracolonnacopertura)
        {
            tabellaTotalePatrimonio.Colonne.Add(new Colonna("Controvalore", "", 74, TipoAllineamento.DESTRA, true, 9, true));
            tabellaTotalePatrimonio.Colonne.Add(new Colonna("Rischio", "", 48, TipoAllineamento.DESTRA, true, 9, true));
            tabellaTotalePatrimonio.Colonne.Add(new Colonna("Copertura", "", 58, TipoAllineamento.DESTRA, true, 9, true));
        }
        else
        {
            tabellaTotalePatrimonio.Colonne.Add(new Colonna("Controvalore", "", 100, TipoAllineamento.DESTRA, true, 9, true));
            tabellaTotalePatrimonio.Colonne.Add(new Colonna("Rischio", "", 80, TipoAllineamento.DESTRA, true, 9, true));
        }
        tabellaTotalePatrimonio.Draw(datitab, document);

        #endregion

        document.setLastPos(YtabellaTotalePatrimonio - 30);
        FormatNum conv = new FormatNum();

        if (decimal.Parse(set.TotalePatrimonio[0]["copertura"].ToString()) < 100)
        {
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("(*) Grado di copertura: " + conv.ConvertNum(decimal.Parse(set.TotalePatrimonio[0]["copertura"].ToString())) + "%", document.getMargineLeft(), document.getLastPos(), 180, 10, Globals.OpenTypeFontVerdana, 7));
            document.setLastPos(40);
        }
        else
        {
            document.setLastPos(30);
        }

    }


}