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


/// <summary>
/// Summary description for S83
/// </summary>
public class S83 : ISezione
{

    FormatNum conv = new FormatNum();
    string Titolo = " ";
    private string _testointroduttivo = "";

    public string TestoIntroduttivo
    {
        get
        {
            return _testointroduttivo;
        }
        set
        {
            _testointroduttivo = value;
        }
    }


    #region ISezione Members

    /// <summary>
    /// Disegna la sezione standard S83
    /// </summary>
    /// <param name="dataThread"></param>
    public void writeSezione(DataThread dataThread)
    {
        Tabella tab = new Tabella();
        float dim = 0;
        DocumentPDF document = dataThread.DocumentPDF;
        DatiTabella datitab = new DatiTabella();
        DataSetS83 set = (DataSetS83)dataThread.Data.DatiSezione;


        System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream fileImmagineA = thisExe.GetManifestResourceStream(UtilityManager.getAppSetting("ImmagineA_S83"));
        System.IO.Stream fileImmagineB = thisExe.GetManifestResourceStream(UtilityManager.getAppSetting("ImmagineB_S83"));
        System.IO.Stream fileImmagineC = thisExe.GetManifestResourceStream(UtilityManager.getAppSetting("ImmagineC_S83"));

        //calcola se entra nella pagina altrimenti aggiunge una nuova pagina. In questa sezione il # di righe è fissato
        if (dataThread.UltimaSezioneStampata == "S82")
            document.addPage();
        else
            if (document.checkMargin(datitab.GetRowDim() * 10 + 60))
                document.addPage();
        ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
        //document.setSezTitolo(Titolo);
        document.setTitolo(Titolo);
        document.setChapterHeader(_testointroduttivo);

        #region Tabella "MONITORAGGIO ATTIVATO IL"

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

        Tabella tabellaMonitoraggioAttivato = new Tabella();
        tabellaMonitoraggioAttivato.X = document.getMargineLeft();
        tabellaMonitoraggioAttivato.Y = document.getLastPos() + 30;
        tabellaMonitoraggioAttivato.Datasource = datitab.table;

        tabellaMonitoraggioAttivato.Colonne.Add(new Colonna("Descrizione", string.Empty, 325, TipoAllineamento.SINISTRA, true, 8, true));
        tabellaMonitoraggioAttivato.Draw(datitab, document);

        #endregion

        #region Tabella "AGGIORNATO IL"
        datitab = new DatiTabella();
        datitab.table = set.Tables["AggiornamentoMonitoraggio"];

        Tabella tabellaMonitoraggioAggiornato = new Tabella();
        tabellaMonitoraggioAggiornato.X = document.getMargineLeft() + 340/*300*/;
        tabellaMonitoraggioAggiornato.Y = document.getLastPos() + 58;
        tabellaMonitoraggioAggiornato.Datasource = datitab.table;

        tabellaMonitoraggioAggiornato.Colonne.Add(new Colonna("Descrizione", string.Empty, 170/*210*/, TipoAllineamento.SINISTRA, true, 8, true));
        tabellaMonitoraggioAggiornato.Draw(datitab, document);

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



        //**********
        datitab = new DatiTabella();
        datitab.table = set.Tables["Monitoraggio"];

        #region Determinazione presenza assenza nota
        bool hasNota = false;
        foreach (DataRow rw in datitab.table.Rows)
        {
            if (rw["Nota"] != DBNull.Value)
                if (Convert.ToBoolean(rw["Nota"]))
                    hasNota = true;
        }
        #endregion

        datitab.setY(document.getLastPos() + dim);
        datitab.setX(document.getMargineLeft());
        datitab.setIsLinee(2);
        datitab.setCellSpace(0);
        datitab.setCell(200 /*200*/, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        datitab.setCell(125/*85*/, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, false);
        datitab.setCell(0, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);

        document.InsertTable(tab.getTabella(datitab));

        #region Immagine A e Immagine B
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineA, document.getMargineLeft() + 165, document.getLastPos() + dim - 5, 0.15F));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineB, document.getMargineLeft() + 165, document.getLastPos() + dim + 19, 0.15F));
        #endregion

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

        datitab = new DatiTabella();
        datitab.table = set.Tables["DettaglioRisorseApportate"];
        datitab.setY(document.getLastPos() + dim);
        datitab.setX(document.getMargineLeft());
        datitab.setIsLinee(2);
        datitab.setCellSpace(0);
        datitab.setCell(200 /*175*/, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true);

        document.InsertTable(tab.getTabella(datitab));

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

        //********* Riga fittizia per ottenere stesso effetto grafico
        // Riga bianca
        DatiTabella datitabLinea = new DatiTabella();
        datitabLinea.table = set.Tables["RigaFittizia"];
        datitabLinea.setY(document.getLastPos() + dim);
        datitabLinea.setX(document.getMargineLeft() + /*175*/ 200);
        datitabLinea.setCellSpace(0);
        datitabLinea.setIsLinee(2);
        datitabLinea.setCell(/*85*/ 125, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        datitabLinea.SetRowDim(3);
        document.InsertTable(tab.getTabella(datitabLinea));

        // Riga Grigia
        float yRigaGrigia = document.getLastPos() + dim;
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Line(247/*222*/, yRigaGrigia - 12, 371, yRigaGrigia - 12, 3F, new RgbColor(232, 236, 237)));

        //<Bido> Commented
        //**********
        DatiTabella datitabRisorseApportate = new DatiTabella();
        datitabRisorseApportate.table = set.Tables["RisorseApportate"];

        datitabRisorseApportate.setY(document.getLastPos() + dim);
        datitabRisorseApportate.setX(document.getMargineLeft() + 50);
        datitabRisorseApportate.setCellSpace(0);
        datitabRisorseApportate.setIsLinee(2);
        datitabRisorseApportate.setCell(/*125*/ 150, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, true);
        datitabRisorseApportate.setCell(/*85*/ 125, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);

        //Bido Work
        DataRow Temp = datitabRisorseApportate.table.NewRow();
        DataRow CedoleRow = datitabRisorseApportate.table.Rows[2];
        Temp[0] = CedoleRow[0];
        Temp[1] = CedoleRow[1];
        datitabRisorseApportate.table.Rows.Remove(CedoleRow);
        //End Bido Work


        document.InsertTable(tab.getTabella(datitabRisorseApportate));

        page.Elements.Add(
            new ceTe.DynamicPDF.PageElements.Rectangle(
                document.getMargineLeft() - 4.5F,
                document.getLastPos() + dim - 14,
                78,
                (datitabRisorseApportate.GetRowDim() * datitabRisorseApportate.getNumRow()) + 3,
                new RgbColor(232, 236, 237),
                new RgbColor(232, 236, 237))
        );



        datitab.CellClear();
        dim += (datitabRisorseApportate.GetRowDim() * datitabRisorseApportate.getNumRow()) + 10;


        //********* Riga fittizia per ottenere stesso effetto grafico
        datitabLinea = new DatiTabella();
        datitabLinea.table = set.Tables["RigaFittizia"];
        datitabLinea.setY(document.getLastPos() + dim - 25);
        datitabLinea.setX(document.getMargineLeft());
        datitabLinea.setCellSpace(0);
        datitabLinea.setIsLinee(2);
        datitabLinea.setCell(325, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        datitabLinea.SetRowDim(3);

        document.InsertTable(tab.getTabella(datitabLinea));
        //**********
        DatiTabella datitabTotali = new DatiTabella();
        datitabTotali.table = set.Tables["Totali"];
        // Bido Work
        //datitabTotali.table.Columns.Add("Notes");
        // End of Bido Work

        datitabTotali.setY(document.getLastPos() + dim);
        datitabTotali.setX(document.getMargineLeft());
        datitabTotali.setCellSpace(0);
        datitabTotali.setIsLinee(2);
        //datitabTotali.setCell(15, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        datitabTotali.setCell(/*175*/ 200, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        datitabTotali.setCell(/*110*/ 125, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);

        //Bido Work
        //Cedole e dividendi
        DataRow CedoledividendiDR = datitabTotali.table.NewRow();
        //Copying Row Items field by field, Whole row assigmnet generate error
        CedoledividendiDR[0] = Temp[0];
        CedoledividendiDR[1] = Temp[1];
        datitabTotali.table.Rows.InsertAt(CedoledividendiDR, 1);

        //Guadagno/Perdita Totale
        DataRow GuadagnoPerditaDR = datitabTotali.table.NewRow();
        //Copying Row Items field by field, Whole row assigmnet generate error
        GuadagnoPerditaDR[0] = "Guadagno/Perdita Totale";

        string AB = datitabTotali.table.Rows[0][1].ToString().Replace("€", "");
        string C = datitabTotali.table.Rows[1][1].ToString().Replace("€", "");
        double DB_GuadagnoPerdita = double.Parse(AB) + double.Parse(C);

        string STR_GuadagnoPerdita = conv.ConvertNum(DB_GuadagnoPerdita).ToString() + " €";
        //string STR_GuadagnoPerdita = DB_GuadagnoPerdita.ToString().Replace(".", ",") + " €";

        if (DB_GuadagnoPerdita > 0)
            STR_GuadagnoPerdita = "+" + STR_GuadagnoPerdita;

        GuadagnoPerditaDR[1] = STR_GuadagnoPerdita;
        datitabTotali.table.Rows.InsertAt(GuadagnoPerditaDR, 2);


        string CumulatoNote = "";
        if (datitabTotali.table.Rows.Count >= 5)
        {
            CumulatoNote = "  (" + datitabTotali.table.Rows[4][1].ToString() + " su base annua)";
            datitabTotali.table.Rows[3][1] = datitabTotali.table.Rows[3][1];
            datitabTotali.table.Rows.RemoveAt(4);
        }

        //End Bido Work
        document.InsertTable(tab.getTabella(datitabTotali));


        #region Immagine A - B
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineA, document.getMargineLeft() + 165, document.getLastPos() + dim - 5, 0.15F));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + 181, document.getLastPos() + dim, 5, 1, ceTe.DynamicPDF.CmykColor.Black, ceTe.DynamicPDF.CmykColor.Black));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineB, document.getMargineLeft() + 190, document.getLastPos() + dim - 5, 0.15F));
        #endregion
        dim += datitabTotali.GetRowDim() * datitabTotali.getNumRow();
        //Bido Work
        #region Immagine C
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineC, document.getMargineLeft() + 190, document.getLastPos() + dim - 75, 0.15F));
        #endregion

        #region Immagine A - B + C
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineA, document.getMargineLeft() + 140, document.getLastPos() + dim - 52, 0.15F));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + 156, document.getLastPos() + dim - 47, 5, 1, ceTe.DynamicPDF.CmykColor.Black, ceTe.DynamicPDF.CmykColor.Black));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineB, document.getMargineLeft() + 165, document.getLastPos() + dim - 52, 0.15F));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + 181, document.getLastPos() + dim - 47, 5, 1, ceTe.DynamicPDF.CmykColor.Black, ceTe.DynamicPDF.CmykColor.Black));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + 183, document.getLastPos() + dim - 49, 1, 5, ceTe.DynamicPDF.CmykColor.Black, ceTe.DynamicPDF.CmykColor.Black));
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(fileImmagineC, document.getMargineLeft() + 190, document.getLastPos() + dim - 52, 0.15F));
        #endregion
        //End Bido Work

        //Bido Work
        dim += (datitabTotali.GetRowDim() * datitabTotali.getNumRow());
        float font = float.Parse("8,0");

        if (CumulatoNote != "")
            document.writeText(380, document.getLastPos() + 222, " " + CumulatoNote, font);
        //End Bido Work

        //********* tabella che contiene il dettaglio temporale su data di avvio etc
        //page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + 390, 211, 130, datitab.GetRowDim() * datitab.getNumRow() - 5, new RgbColor(232, 236, 237), new RgbColor(232, 236, 237)));
        datitab.CellClear();
        //scrive la data di attivazione del monitoraggio

        //Bido <For the hidden note under section 84>
        dim = dim - 95;
        //End Bido 
        /* Bido Commented */

        DatiTabella tabRendimentoAnn = new DatiTabella();
        tabRendimentoAnn.table = set.Tables["RendimentoAnnualizzato"];
        tabRendimentoAnn.setY(document.getLastPos() + dim);
        tabRendimentoAnn.setX(document.getMargineLeft());
        tabRendimentoAnn.setCellSpace(0);
        tabRendimentoAnn.setIsLinee(2);
        //datitabTotali.setCell(15, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        tabRendimentoAnn.setCell(/*175*/ 200, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
        tabRendimentoAnn.setCell(/*95*/ 125, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
        document.InsertTable(tab.getTabella(tabRendimentoAnn));
        dim += tabRendimentoAnn.GetRowDim() * tabRendimentoAnn.getNumRow();

        if (hasNota && _hasselectedpatrimnonrap)
        {
            addNota(document, Convert.ToInt32(dim));
        }


        //////aggiorno il puntatore alla posizione da cui si può scrivere
        ////V Problema sforamento in caso di presenza di rendimento annualizzato
        ////document.setLastPos(dim - 25);
        document.setLastPos(dim - 32);
    }


    /// <summary>
    /// Disegna la S83 in formato solo testuale.
    /// Se un'area/progetto è oggetto di monitoraggio ma il suo controvalore monitorato è pari a 0, si stampa solo un testo introduttivo.
    /// </summary>
    /// <param name="dataThread"></param>
    public void writeSezioneFormatoSoloTesto(DataThread dataThread)
    {
        DocumentPDF document = dataThread.DocumentPDF;

        //calcola se entra nella pagina altrimenti aggiunge una nuova pagina. In questa sezione il # di righe è fissato
        if (document.checkMargin(30))
            document.addPage();

        ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
        document.setChapterHeader(_testointroduttivo);

        //This flag will be used in S43Bis to check if S83 was not printed as normal section and it's just printed as note,
        //in this case S43BIS will be printed just after S83 note as S84 & S85 will not be printed.
        dataThread.UltimaSezioneStampata = "S83";

        ////aggiorno il puntatore alla posizione da cui si può scrivere
        document.setLastPos(30);
    }


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

    private void addNota(DocumentPDF doc, int dim)
    {
        FormatNum con = new FormatNum();
        doc.setSezFooter("(*) Alcuni prodotti non rientrano nel calcolo del rendimento. Il dettaglio del patrimonio non considerato viene fornito nel capitolo ''Patrimonio non rappresentabile''", dim);
    }

    bool _hasselectedpatrimnonrap;
    public bool HasSelectedPatrimNonRap
    {
        set
        {
            _hasselectedpatrimnonrap = value;
        }
        get
        {
            return _hasselectedpatrimnonrap;
        }

    }





    #endregion
}