using PDFGenerator.BusinessLayer;
using PDFGenerator.BusinessLayer.DataSection;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace PDFGenerator.Presentation.Section
{
    public class FD10 : SIstogramma, ISezione
    {
        DataSetS10 set = new DataSetS10();

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

        #region ISezione Members

        public new void writeSezione(DataThread datathread)
        {

            document = datathread.DocumentPDF;

            set = (DataSetS10)datathread.Data.DatiSezione;


            /***************** "Modifiche per Aladdin grafici a barre -- Pino" **************************************/
            DataTable _tmp = set.Tables["MacroAssetClass"];
            _tmp.Columns.Remove("Controvalore");

            tabTortaTabella = _tmp; // 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;
            //decimal PatrBancaSez = datathread.PatrimonioBancaFideuramCtvAlNettoContoCorrente + datathread.PartiteViaggiantiInvestimento + datathread.PartiteViaggiantiDisinvestimento + datathread.PatrimonioNonRappresentabileFI;
            tabTortaTotali.Rows[0][1] = datathread.PatrimonioBancaFideuramCtvAlNettoContoCorrente + datathread.PartiteViaggiantiInvestimento + datathread.PartiteViaggiantiDisinvestimento; //+ datathread.PatrimonioNonRappresentabileFI;
            // ******************************************************************************************//

            base.DirectBanker = "S";

            base.writeSezione10(datathread);



            if (dim > ChartHeigth)
            {
                //this.addNota(document, (int)dim);
                this.addNota1(document, (int)dim, datathread);
            }
            else
            {
                //this.addNota(document, ChartHeigth);
                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 = (DataSetS10)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 totalSelfNegGP = dataThread.Periodico ? dataThread.GetSelfNegativeValue("XY") : 0;
            decimal totaleSelfNegFO = dataThread.Periodico ? dataThread.GetSelfNegativeValue("FO") : 0;
            decimal totalNegativeCurrentAccountValue = dataThread.Periodico ? dataThread.ContoCorrente : 0;
            decimal totalePartiteViaggianti = dataThread.PartiteViaggiantiDisinvestimento + dataThread.PartiteViaggiantiInvestimento;
            decimal GPEligoFondi = dataThread.GPEligoFondi;
            decimal GPEligoTitoli = dataThread.GPEligoTitoli;
            decimal ProdottiNonRapp = dataThread.PatrimonioNonRappresentabileFI;
            decimal PartiteViaggianti = dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento;
            decimal PatrBancaSez = dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente - dataThread.PatrimonioNonRappresentabileFI;

            //  decimal GradoCopertura = dataThread.CoperturaTotaleRisorseFinanziarie;
            decimal GradoCopertura = PatrBancaSez / (dataThread.PatrimonioBancaFideuramCtvAlNettoContoCorrente + PartiteViaggianti) * 100;
            //  decimal gc = dataThread.GradoCoperturaMacroAsset;
            List<string> notes = new List<string>();



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



            //totalNegativeCurrentAccountValue = -1000.00M;
            //totaleSelfNegFO = -1000.00M;
            //totalePartiteViaggianti = 1000.00M;
            //GPEligoFondi = -500.00M;
            //GPEligoTitoli = -500.00M;
            //PatrimonioTerzi = -400.00M;
            //GradoCopertura = 70.00M;
            //PatrimonioTerziNonRapp = 100.00M;
            //ProdottiNonRapp = 20.00M;


            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);
        }
    }
}