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

namespace PDFGenerator.Presentation.Section
{
    class S134: 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 S134
        /// </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();
            DataSetS134 set = (DataSetS134)dataThread.Data.DatiSezione;
            string tempTesto = "";
           

            document.setSezTitolo(dataThread.SezioneReport.Titolo);
            
            tempTesto = dataThread.SezioneReport.TestoIntroduttivo;
            tempTesto = tempTesto.Replace("/$AREA$/", dataThread.SezioneReport.AreaProgettoDescrizione);


            //if (dataThread.Rete.ToUpper() == "S")
            //    //FC 26062015 Aggionamento nuova Ragione Sociale
            //    tempTesto = tempTesto.Replace("Fideuram", "Sanpaolo Invest");

            //****************************** Cecco modfiche dicitura IWBANK**********************************
            if (dataThread.Rete.ToUpper() == "S")
            {
                //FC 26062015 Aggionamento nuova Ragione Sociale
                tempTesto = tempTesto.Replace("Fideuram", "Fideuram S.p.A., commercializzato tramite la rete di private banker Sanpaolo Invest");
                //tempTesto = tempTesto.Replace("Fideuram", "Sanpaolo Invest");
            }
            else if (dataThread.Rete.ToUpper() == "W")
            {
                tempTesto = tempTesto.Replace("Fideuram", "IW Private Investments");
            }
            //*************************************************************************************************

            document.setChapterHeader(tempTesto, 0, 520, 8);


            if (document.checkMargin(datitab.GetRowDim() * 10 + 60))
                                                    document.addPage();

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

            #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, 335, TipoAllineamento.SINISTRA, true, 8, true));
            tabellaMonitoraggioAttivato.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 + 2);
            datitab.setX(document.getMargineLeft());
            datitab.setIsLinee(2);
            datitab.setCellSpace(0);
            datitab.SetRowDim(25);
            datitab.setCell(200 /*200*/, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true);
            datitab.setCell(135/*85*/, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, false);
            datitab.setCell(0, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);

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

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

           
            //<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*/ 135, 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,
                    70,
                    (datitabRisorseApportate.GetRowDim() * datitabRisorseApportate.getNumRow()) + 3.5F,
                    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
            DatiTabella 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(335, 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*/ 135, 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] + "<BR>" + CumulatoNote;
                datitabTotali.table.Rows.RemoveAt(4);
            }
        
            //End Bido Work
            document.InsertTable(tab.getTabella(datitabTotali));


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

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


            //********* 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*/ 135, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
            document.InsertTable(tab.getTabella(tabRendimentoAnn));
            dim += tabRendimentoAnn.GetRowDim() * tabRendimentoAnn.getNumRow();

            if (hasNota && _hasselectedpatrimnonrap)
            {
                addNota(document);
            }


            //////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);
            document.setHeaderSpace(30);
        }


        /// <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 = "S134";

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


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

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

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

        }

        #endregion
    }
}