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.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.BusinessLayer;
using System.Resources;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace PDFGenerator.Presentation.Section
{
    /********************* "Modifiche per Aladdin grafici a barre -- Pino" ****************************/
    /************** Modificata l'ereditarieta da STorta a SIstogramma *********************************/
    class S148 : SIstogramma, ISezione
    {


        DataSetS148 set = new DataSetS148();
        public S148()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region ISezione Members

        public new void writeSezione(DataThread datathread)
        {
            
            document = datathread.DocumentPDF;
           
                            //document.addPage();         

            set = (DataSetS148)datathread.Data.DatiSezione;
           
            tabTortaTabella = set.Tables["MacroAssetClass"];
            tabTortaTotali = set.Tables["MacroAssetTotale"];
            tabTortaPatrimonioNonRappresentabile = set.Tables["PatrimonioNonRappresentabile"];
            tabTortaContoCorrenteBancaFideuram = set.Tables["ContoCorrenteBancaFideuram"];
            tabTortaPartiteViaggianti = set.Tables["PartiteViaggianti"];
            tabTotale = set.Tables["Totale"];
            // ************************* Modifica Andrea per correzione ***********************************//
            //  tabTortaTotali.Rows[0][1] = tabTotale.Rows[0][1];
            //tabTortaTotali.Rows[0][1] = datathread.PatrimonioBancaFideuramCtvAlNettoContoCorrente;
            tabTortaTotali.Rows[0][1] = datathread.PatrimonioBancaFideuramCtvAlNettoContoCorrente + datathread.PartiteViaggiantiInvestimento + datathread.PartiteViaggiantiDisinvestimento; //datathread.PatrimonioNonRappresentabileFI;
            // ******************************************************************************************//
            //base.writeSezione(datathread);
            // base.writeSezione148(datathread);
            base.writeSezione148(datathread);

            //if (set.Tables["PatrimonioNonRappresentabile"].Rows.Count > 0)
            //{
            if (dim > ChartHeigth)
            {
                this.addNota1(document, (int)dim, datathread);
            }
            else
            {
                this.addNota1(document, ChartHeigth, datathread);
            }
        //}


                if (dim > ChartHeigth)
            {
                document.setLastPos(dim);
            }
            else
            {
                document.setLastPos(ChartHeigth);
            }
        }
        #endregion

        public void writeSezioneCombo(DataThread datathread)
        {

            document = datathread.DocumentPDF;

            set = (DataSetS148)datathread.Data.DatiSezione;

            tabTortaTabella = set.Tables["MacroAssetClass"];
            tabTortaTotali = set.Tables["MacroAssetTotale"];
            tabTortaPatrimonioNonRappresentabile = set.Tables["PatrimonioNonRappresentabile"];
            tabTortaContoCorrenteBancaFideuram = set.Tables["ContoCorrenteBancaFideuram"];
            tabTortaPartiteViaggianti = set.Tables["PartiteViaggianti"];
            tabTotale = set.Tables["Totale"];
            base.writeSezioneComboSX(datathread);

            if (salto)
                document.returnLastPage();

            document.setPotenzialeLastPos(datitab.GetRowDim() * datitab.getNumRow() + dim, salto);

        }

        private void addNota(DocumentPDF doc, int dim)
        {
            doc.setSezFooter("(*) Il dettaglio del Patrimonio non rappresentabile per Asset class viene riportato in fondo al documento.", dim);
        }
        private void addNota1(DocumentPDF doc, int dim, DataThread dataThread)
        {
            decimal totaleSelfNegFO = dataThread.Periodico ? dataThread.GetSelfNegativeValue("FO") : 0;
            decimal totalNegativeCurrentAccountValue = dataThread.Periodico ? dataThread.ContoCorrente : 0;
            decimal GPEligoFondi = dataThread.GPEligoFondi;
            decimal GPEligoTitoli = dataThread.GPEligoTitoli;
            decimal ProdottiNonRapp = dataThread.PatrimonioNonRappresentabileFI;
            decimal PartiteViaggianti = dataThread.PartiteViaggiantiInvestimento;
            //  decimal GradoCopertura = dataThread.GradoCoperturaMacroAssetInterno;
            //decimal PatrBancaSez = dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente - dataThread.PartiteViaggiantiInvestimento - dataThread.PartiteViaggiantiDisinvestimento - dataThread.PatrimonioNonRappresentabileFI;
            decimal PatrBancaSez = dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente  - dataThread.PatrimonioNonRappresentabileFI;
            decimal GradoCopertura = PatrBancaSez / (dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente + dataThread.PartiteViaggiantiDisinvestimento + dataThread.PartiteViaggiantiInvestimento) * 100;
            List<string> notes = new List<string>();



            string nota = string.Empty;
            string nota1 = "";
            string token = "";


            if (totalNegativeCurrentAccountValue != 0 || totaleSelfNegFO != 0 || GPEligoFondi < 0 || GPEligoTitoli < 0 || ((GradoCopertura >= 0 && GradoCopertura < 100) && (ProdottiNonRapp != 0 || PartiteViaggianti != 0)))
            {
                nota = "(*) ";
            }

            if (totalNegativeCurrentAccountValue != 0 || totaleSelfNegFO != 0 || GPEligoFondi < 0 || GPEligoTitoli < 0)
            {
                notes.Add("Il controvalore esclude il saldo negativo");
                token = " ";

                if (totalNegativeCurrentAccountValue < 0)
                {
                    notes.Add(string.Format(token + "dei conti correnti ({0} €)", totalNegativeCurrentAccountValue.ToString("N")));
                    token = ", ";
                }

                if (totaleSelfNegFO < 0)
                {
                    notes.Add(string.Format(token + "della liquidità sottostante \"IL MF - Liquidita\" ({0} €)", totaleSelfNegFO.ToString("N")));
                    token = ", ";
                }

                if (GPEligoFondi < 0)
                {
                    notes.Add(string.Format(token + "della liquidità sottostante GP Eligo Fondi ({0} €)", GPEligoFondi.ToString("N")));
                    token = ", ";
                }

                if (GPEligoTitoli < 0)
                {
                    notes.Add(string.Format(token + "della liquidità sottostante GP Eligo Titoli ({0} €)", GPEligoTitoli.ToString("N")));
                    token = ", ";
                }
                token = ". ";

            }
            if (ProdottiNonRapp != 0 || PartiteViaggianti != 0)
            {
            if (GradoCopertura >= 0 && GradoCopertura < 100)
            {
                notes.Add(string.Format(token + "Grado di copertura (%) della rappresentazione grafica per macro asset class pari a {0}", GradoCopertura.ToString("N")));
                token = ": ";

                if (ProdottiNonRapp != 0 || PartiteViaggianti != 0)
                {
                    notes.Add(string.Format(token + "non sono considerati"));
                    token = " ";
                    if (ProdottiNonRapp != 0 || PartiteViaggianti != 0)
                    {
                        if (ProdottiNonRapp != 0)
                        {
                            notes.Add(string.Format(token + "i prodotti non rappresentabili ({0} €)", ProdottiNonRapp.ToString("N")));
                            token = ", ";
                        }
                        if (PartiteViaggianti != 0)
                        {
                            notes.Add(string.Format(token + "gli importi relativi alle operazioni in corso ({0} €)", PartiteViaggianti.ToString("N")));
                            token = ", ";
                        }
                    }
                }
                notes.Add(string.Format("."));
            }
            }
            nota1 += ".";
            token = " ";

            //int i = 0;
            nota += notes.FirstOrDefault<string>();
            if (notes.Count > 1)
            {
                foreach (var s in notes.Skip(1))
                {
                    nota += s;

                    //if (i == 0)
                    // nota += s;
                    //if (i > 0 && i < notes.Count - 1)
                    // nota += ", " + s;
                    //if (i == notes.Count - 1)
                    // nota += " e " + s;
                    //i++;
                }
            }
            //else
            //    nota += notes.FirstOrDefault<string>();

            if (!nota.Equals(""))
                //document.getCurrentPage().Elements.Add(new Label(nota, Xtable, Ytable, 512, 30F, Globals.OpenTypeFontVerdana, 7));
                doc.setSezFooter(nota, dim - 15);
        }
        public void writeSezione_old(DataThread datathread) {
            document = datathread.DocumentPDF;
            DataSetS148 set = (DataSetS148)datathread.Data.DatiSezione;
            if (document.checkMargin(250))
                            document.addPage();           
            tabTortaTabella = set.MacroAssetClass;
            tabTortaTotali = set.MacroAssetTotale;    
            base.writeSezione(datathread);            
        }

        //public void writeSezione_old(DataThread dataThread)
        //{
        //    DocumentPDF document = dataThread.DocumentPDF;           
        //    DataSetS148 set = (DataSetS148)dataThread.Data.DatiSezione;
        //    DatiTabella datitab = new DatiTabella(set.AssetClass);

        //    float Xtable = document.getMargineLeft();
        //    float Ytable = document.getLastPos();
        //    int dFont = 9;
        //    int wCol1 = 160;
        //    int wCol2 = 90;
        //    int wCol3 = 60;
        //    int hGrafico = 150;

        //    if (document.checkMargin(250))
        //        document.addPage();

        //   document.setSezTitolo(dataThread.SezioneReport.Titolo);
        //   document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo.Replace("/$Banca$/", dataThread.NomeRete), 0, 520, 8);


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

        //   #region  GRAFICO
        //   DatiGrafico dati = new DatiGrafico();
        //   dati.dataTab = set.AssetClass;
        //   dati.setHeight(hGrafico);
        //   dati.setWidth(hGrafico);
        //   Torta isto = new Torta();
        //   document.InsertGrafico(isto.getGrafico(dati), Xtable + wCol1 + wCol2 + wCol3 + 50, Ytable - 11);
        //   #endregion

        //    #region Tabella Rating
           
        //    Tabella tabellaDati = new Tabella(Xtable, Ytable);
        //    tabellaDati.Header = true;
        //    tabellaDati.Colonne.Add(new Colonna("AssetClassName", "Asset Class", wCol1, TipoAllineamento.SINISTRA, false, dFont, false, TipoColonna.IMMAGINE_E_TESTO));
        //    tabellaDati.Colonne.Add(new Colonna("Controvalore", "Controvalore<BR>€", wCol2, TipoAllineamento.DESTRA, false, dFont, false));
        //    tabellaDati.Colonne.Add(new Colonna("Percentuale", "Peso<BR>%", wCol3, TipoAllineamento.DESTRA, false, dFont, false));
        //    tabellaDati.Draw(datitab, document);
            


        //    //SIMBOLI
        //    int nPallino = 1;
        //    float yPallino = Ytable + tabellaDati.AltezzaCella;
        //    float xPallino = Xtable;

        //    float ycambio = tabellaDati.YCambioPagina;
        //    bool saltoPagina = tabellaDati.SaltoPaginaEffettuato;

        //    ceTe.DynamicPDF.Merger.ImportedPage pageTable = document.getCurrentPage();
        //    foreach (DataRow drSimbolo in tabellaDati.Datasource.Rows)
        //    {

        //        RgbColor color =
        //            new RgbColor(byte.Parse(drSimbolo["Red"].ToString()), byte.Parse(drSimbolo["Green"].ToString()), byte.Parse(drSimbolo["Blue"].ToString()));
        //        ceTe.DynamicPDF.PageElements.Circle simbolo =
        //            new ceTe.DynamicPDF.PageElements.Circle(xPallino, yPallino, 10 / 1.414F, color, color);
        //        page.Elements.Add(simbolo);
        //        if (saltoPagina && nPallino > 20 && page != pageTable)
        //        {
        //            page = pageTable;
        //            yPallino = ycambio + tabellaDati.AltezzaCella;
        //            //nPallino++;
        //        }
        //        else
        //        {
        //            yPallino += tabellaDati.AltezzaCella;
        //            nPallino++;
        //        }

        //    }


        //    document.setLastPos(datitab.GetRowDim() * (datitab.getNumRow() + 1));
        //    Ytable = document.getLastPos();
        //    //TOTALE
        //    tabellaDati = new Tabella(Xtable, Ytable);
        //    tabellaDati.Header = false;
        //    tabellaDati.Colonne.Add(new Colonna("Totale", "", wCol1, TipoAllineamento.SINISTRA, true, dFont, true));
        //    tabellaDati.Colonne.Add(new Colonna("Controvalore", "", wCol2, TipoAllineamento.DESTRA, true, dFont, true));
        //    tabellaDati.Colonne.Add(new Colonna("Percentuale", "", wCol3, TipoAllineamento.DESTRA, true, dFont, true));
        //    tabellaDati.Draw(new DatiTabella(set.AssetTotale), document);


        //    #endregion

        //    document.setLastPos(datitab.GetRowDim());
        //}
    }
}