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

namespace PDFGenerator.Presentation.Section
{
    public class S140 : SIstogramma, ISezione //: ISezione
    {
       
        
        #region ISezione Members



        public new void writeSezione(DataThread dataThread)
        {
            DocumentPDF document = dataThread.DocumentPDF;
            DataSetS140 set = (DataSetS140)dataThread.Data.DatiSezione;

            //if (dataThread.TipoContratto != TipoContratto.Private) 
            //                                        document.addPage(); 

            document.setSezTitolo(dataThread.SezioneReport.Titolo.Replace("/$Banca$/", dataThread.NomeRete));
            if (dataThread.Rete == "F")
                document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo.Replace("/$Banca$/", dataThread.NomeRete).Replace("Fideuram e presso altri Istituti", "Fideuram"), 0, 520, 8);
            if (dataThread.Rete == "S")
                    document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo.Replace("/$Banca$/", dataThread.NomeRete).Replace("Sanpaolo Invest", "Fideuram S.p.A., commercializzati tramite la rete di private banker Sanpaolo Invest").Replace("Fideuram S.p.A., commercializzati tramite la rete di private banker Sanpaolo Invest e presso altri Istituti", "Fideuram S.p.A., commercializzati tramite la rete di private banker Sanpaolo Invest"), 0, 520, 8);
            if (dataThread.Rete == "W")
                document.setChapterHeader(dataThread.SezioneReport.TestoIntroduttivo.Replace("/$Banca$/", dataThread.NomeRete).Replace("IW Private Inv. e presso altri Istituti", "IW Private Inv."), 0, 520, 8);

           float Xtable = document.getMargineLeft();
            float Ytable = document.getLastPos();
            int dFont = 8;
            int hRow = 18;
            int wCol1 = 135;
            int wCol2 = 95;
            int wCol3 = 65;
            int hGrafico = 190; //150;
            float lastPosGrafico = Ytable + hGrafico;

            /************************ Modifiche per Aladdin grafici a barre -- Pino**********************/
            #region  GRAFICO 
            DatiGrafico dati = new DatiGrafico();
            dati.dataTab = set.ChartData;
            //dati.setHeight(hGrafico);
            dati.setHeight(Convert.ToSingle(datitab.getHeaderDim()) + (datitab.GetRowDim() * 2 + 5));
            dati.setWidth(hGrafico);
            Istogramma isto = new Istogramma();
            //document.InsertGrafico(isto.getGrafico(dati), 360, Ytable - 10);
            document.InsertGrafico(isto.getGrafico(dati), 350, Ytable);

            // Scrittura del footer sul grafico
            FormatNum format = new FormatNum();
            ArrayList Labels = format.CreateCustomPerc148(dati.getValMax(),dati.getValMin());
            document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(358, document.getLastPos() + 49, 190, 18, 0, new RgbColor(232, 236, 237)));

            
            float passo = 40.0F;
            for (int ii = 0; ii <= Labels.Count - 1; ++ii)
                if (ii == 0)
                    document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Label(Labels[ii].ToString(), 270F + ii, document.getLastPos() + 49, 100, 10, Globals.OpenTypeFontVerdana, 8, ceTe.DynamicPDF.TextAlign.Right, ceTe.DynamicPDF.CmykColor.Black));
                else
                    document.getCurrentPage().Elements.Add(new ceTe.DynamicPDF.PageElements.Label(Labels[ii].ToString(), 275F + (passo * ii), document.getLastPos() + 49, 100, 10, Globals.OpenTypeFontVerdana, 8, ceTe.DynamicPDF.TextAlign.Right, ceTe.DynamicPDF.CmykColor.Black));

            #endregion
            /*******************************************************************************************/


            datitab.setIsRet();
            datitab.setIsLinee(1);
            datitab.setHeader();
            datitab.setHeaderFont(dFont);
            datitab.setHeaderDim(hRow);
            datitab.SetRowDim(hRow);
            datitab.setCell(wCol1, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setCell(wCol2, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setCell(wCol3, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setY(Ytable);
            datitab.setX(Xtable);
            datitab.setCellSpace(0);
            datitab.table = set.RischioCredito;
            Tabella tab = new Tabella();
            document.InsertTable(tab.getTabella(datitab));

            Ytable += datitab.GetRowDim() * (datitab.getNumRow() + 1);

            datitab = new DatiTabella();
            datitab.table = set.PatrimonioCoperto;
            datitab.setY(Ytable);
            datitab.setX(Xtable);
            datitab.setHeaderDim(hRow);
            datitab.SetRowDim(hRow);
            datitab.setCell(wCol1, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCell(wCol2, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCell(wCol3, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCellSpace(0);
            Tabella Ctab = new Tabella();
            document.InsertTable(Ctab.getTabella(datitab));

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

            datitab = new DatiTabella();
            datitab.table = set.PatrimonioNonCoperto;
            datitab.setIsRet();
            datitab.setIsLinee(1);
            datitab.setY(Ytable);
            datitab.setX(Xtable);
            datitab.setHeaderDim(hRow);
            datitab.SetRowDim(hRow);
            datitab.setCell(wCol1, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setCell(wCol2, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setCell(wCol3, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false, dFont);
            datitab.setCellSpace(0);
            Tabella Ctab2 = new Tabella();
            document.InsertTable(Ctab2.getTabella(datitab));
            Ytable += datitab.GetRowDim() * datitab.getNumRow();

            datitab = new DatiTabella();
            datitab.table = set.RischioCreditoTotal;
            datitab.setY(Ytable);
            datitab.setX(Xtable);
            datitab.setHeaderDim(hRow);
            datitab.SetRowDim(hRow);
            datitab.setCell(wCol1, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCell(wCol2, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCell(wCol3, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdanaB, true, dFont);
            datitab.setCellSpace(0);
            Tabella tabTot = new Tabella();
            document.InsertTable(tabTot.getTabella(datitab));


            // Ytable += datitab.GetRowDim() * (datitab.getNumRow() < 5 ? 5 : datitab.getNumRow()) + 5;
            Ytable = lastPosGrafico - 15;
            //decimal totalePartiteViaggianti = dataThread.PartiteViaggiantiDisinvestimento + dataThread.PartiteViaggiantiInvestimento;
            //if (dataThread.TotalNegativeCurrentAccountValue < 0 && totalePartiteViaggianti == 0)
            //    document.getCurrentPage().Elements.Add(new Label(string.Format("(*) Il controvalore esclude il saldo negativo del\r\nconto corrente ({0} €)", dataThread.TotalNegativeCurrentAccountValue.ToString("N")), Xtable, Ytable, 512, 30F, Globals.OpenTypeFontVerdana, 8));
            //else if (dataThread.TotalNegativeCurrentAccountValue == 0 && totalePartiteViaggianti != 0)
            //    document.getCurrentPage().Elements.Add(new Label(string.Format("(*) Il controvalore esclude gli investimenti in corso", dataThread.TotalNegativeCurrentAccountValue.ToString("N")), Xtable, Ytable, 512, 30F, Globals.OpenTypeFontVerdana, 8));
            //else if (dataThread.TotalNegativeCurrentAccountValue < 0 && totalePartiteViaggianti != 0)
            //    document.getCurrentPage().Elements.Add(new Label(string.Format("(*) Il controvalore esclude il saldo negativo di\r\nconto corrente ({0} €) e gli investimenti in corso", dataThread.TotalNegativeCurrentAccountValue.ToString("N")), Xtable, Ytable, 512, 30F, Globals.OpenTypeFontVerdana, 8));


            // MIOFOGLIO 20181206

            // MIOFOGLIO 20181130

            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;

            List<string> notes = new List<string>();

            string nota = string.Empty;

            if (totalNegativeCurrentAccountValue != 0 || totaleSelfNegFO != 0 || totalSelfNegGP != 0 || totalePartiteViaggianti != 0)
            {

                nota = "(*) Il controvalore esclude ";

                if (totalNegativeCurrentAccountValue < 0)
                {
                    notes.Add(string.Format("il saldo negativo del conto corrente ({0} €)", totalNegativeCurrentAccountValue.ToString("N")));
                }

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

                if (totalSelfNegGP < 0)
                {
                    notes.Add(string.Format("il saldo negativo della liquidità sottostante le linee \"GP Eligo\" ({0} €)", totalSelfNegGP.ToString("N")));
                }

                if (totalePartiteViaggianti != 0)
                {
                    notes.Add(string.Format("gli investimenti in corso."));
                }
            }

            int i = 0;
            if (notes.Count > 1)
            {
                foreach (var s in notes)
                {
                    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-50, 512, 30F, Globals.OpenTypeFontVerdana, 7));

            // commentato:
            //decimal totalNegativeCurrentAccountValue = dataThread.TotalNegativeCurrentAccountValue;
            //decimal totalSelfNegCurrentAccountValue = dataThread.TotalSelfNegCurrentAccountValue;
            //decimal totalePartiteViaggianti = dataThread.PartiteViaggiantiDisinvestimento + dataThread.PartiteViaggiantiInvestimento;
            //string note = "";

            //if (totalNegativeCurrentAccountValue == 0 && totalSelfNegCurrentAccountValue == 0 && totalePartiteViaggianti != 0)
            //{
            //    note += "(*) Il controvalore esclude gli investimenti in corso";
            //}
            //if (totalNegativeCurrentAccountValue < 0 && totalSelfNegCurrentAccountValue == 0 && totalePartiteViaggianti == 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo del conto corrente ({0} €)";
            //    note = string.Format(note, totalNegativeCurrentAccountValue.ToString("N"));
            //}
            //if (totalNegativeCurrentAccountValue == 0 && totalSelfNegCurrentAccountValue < 0 && totalePartiteViaggianti == 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo della liquidità sottostante le linee “GP Eligo” ({0} €)";
            //    note = string.Format(note, totalSelfNegCurrentAccountValue.ToString("N"));
            //}
            //if (totalNegativeCurrentAccountValue < 0 && totalSelfNegCurrentAccountValue == 0 && totalePartiteViaggianti != 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo del conto corrente ({0} €) e gli investimenti in corso";
            //    note = string.Format(note, totalNegativeCurrentAccountValue.ToString("N"));
            //}
            //if (totalNegativeCurrentAccountValue < 0 && totalSelfNegCurrentAccountValue < 0 && totalePartiteViaggianti == 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo del conto corrente ({0} €) e il saldo negativo della liquidità sottostante le linee “GP Eligo” ({1} €)";
            //    note = string.Format(note, totalNegativeCurrentAccountValue.ToString("N"), totalSelfNegCurrentAccountValue.ToString("N"));
            //}
            //if (totalNegativeCurrentAccountValue == 0 && totalSelfNegCurrentAccountValue < 0 && totalePartiteViaggianti != 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo della liquidità sottostante le linee “GP Eligo” ({0} €) e gli investimenti in corso";
            //    note = string.Format(note, totalSelfNegCurrentAccountValue.ToString("N"));
            //}
            //if (totalNegativeCurrentAccountValue < 0 && totalSelfNegCurrentAccountValue < 0 && totalePartiteViaggianti != 0)
            //{
            //    note += "(*) Il controvalore esclude il saldo negativo del conto corrente ({0} €), il saldo negativo della liquidità sottostante le linee “GP Eligo” ({1} €) e gli investimenti in corso";
            //    note = string.Format(note, totalNegativeCurrentAccountValue.ToString("N"), totalSelfNegCurrentAccountValue.ToString("N"));
            //}
            //if (!note.Equals(""))
            //    document.getCurrentPage().Elements.Add(new Label(note, Xtable, Ytable, 512, 30F, Globals.OpenTypeFontVerdana, 8));

            //--MIOFOGLIO 20181206

            document.setLastPos(hGrafico);
        }
         
        #endregion
    }
}