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 Dundas.Charting.WebControl;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer;
using ContrattoSei.Utilities;

namespace PDFGenerator.Presentation.Section
{
    public class S170 : ISezione
    {
        string Titolo = string.Empty;
        private string _testointroduttivo;
        private string _testotitolo;



        /// <summary>
        /// Testo del titolo della sezione.
        /// </summary>
        public string TestoTitolo
        {
            get
            {
                return _testotitolo;
            }
            set
            {
                _testotitolo = value;
            }
        }

        /// <summary>
        /// Testo introduttivo della sezione
        /// </summary>
        public string TestoIntroduttivo
        {
            get
            {
                return _testointroduttivo;
            }
            set
            {
                _testointroduttivo = value;
            }
        }



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

        /// <summary>
        /// Stampa la sezione S170
        /// </summary>
        /// <param name="paramSez"></param>
        public void writeSezione(DataThread dataThread)
        {
            DocumentPDF document = dataThread.DocumentPDF;
           
           
            float xLine = 50;          
            float widthLine = 320;
            float widthRectangleTable = widthLine - xLine + 3;         
            float fontSize = 5; 
            int altezzaGrafico = 165;
            float altezzaGraficoMenoEtichette = altezzaGrafico - 40;

            DatiTabella datitab = new DatiTabella();
            DataSetS170 set = (DataSetS170)dataThread.Data.DatiSezione;

            bool _mostracolonnacopertura = Convert.ToBoolean(set.ResultSet[0]["MostraColonnaCopertura"]);

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

            #region Titolo e testo introduttivo
                document.setSezTitoloDiagnosi(_testotitolo);
                document.setChapterHeader(_testointroduttivo, 0, 520, 8);
               // document.setLastPos(-50);
            #endregion

            double var1 = 0;
            if (set.RischioPatrimonioFinanziario.Rows.Count != 0)
                var1 = Convert.ToDouble((decimal)set.RischioPatrimonioFinanziario.Rows[0]["Var"]);

            #region Tabella sotto al grafico
                datitab.table = set.Tables["RischioDiversificazioneTable"];
                if (!_mostracolonnacopertura)
                    datitab.table.Columns.Remove(datitab.table.Columns["copertura"]);
                datitab.setIsLinee(1);
                datitab.setHeader();
                datitab.setHeaderDim(30);
                if (_mostracolonnacopertura)
                    datitab.setCell(120, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
                 else
                    datitab.setCell(182, ceTe.DynamicPDF.TextAlign.Left, Globals.OpenTypeFontVerdana, false);
                   
                datitab.setCell(90, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
                datitab.setCell(47, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
                datitab.setCell(93, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
                datitab.setCell(92, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
                if (_mostracolonnacopertura)
                    datitab.setCell(62, ceTe.DynamicPDF.TextAlign.Right, Globals.OpenTypeFontVerdana, false);
                datitab.setY(document.getLastPos() + altezzaGrafico + 20);
                datitab.setX(document.getMargineLeft());
                datitab.setIsRet();
                datitab.setCellSpace(0);
                Tabella tab = new Tabella();
                document.InsertTable(tab.getTabella(datitab));
                datitab.table = set.Tables["RischioDiversificazione"];            
                this.writeMarker(document, datitab);
            #endregion

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

            #region Grafico

            DatiGrafico dati = new DatiGrafico();
            dati.dataTab = datitab.table;
            dati.dataTab.Columns.Remove(dati.dataTab.Columns["Copertura"]);
            dati.setHeight(altezzaGrafico);
            dati.setWidth(295);//T-1466629-Y0Y5 
            GraficoS132BIS pall = new GraficoS132BIS();
            Chart grafico = pall.getGrafico(dati);

            double vaRMassimo = pall.AxisY_Maximum;
            document.InsertGrafico(grafico, document.getMargineLeft() + 75, document.getLastPos());

            #endregion

            dim += (altezzaGrafico + 40);

            #region Marcatore alla sinistra del grafico

            ceTe.DynamicPDF.PageElements.Rectangle rect;
            FormatNum conv = new FormatNum();
            float yImg = document.getLastPos() - 5;

            if (set.RischioPatrimonioFinanziario.Rows.Count != 0)
            {
                float xFreccia = document.getMargineLeft();
                float yFreccia = document.getLastPos();
                float wFreccia = 50;
                float hFreccia = 20;
                string profilo = " ";

                switch (MarkPosition((float)var1))
                {
                    case 0:
                        profilo = "Prudente";
                        yFreccia += 100;
                        break;
                    case 1:
                        profilo = "Moderato";
                        yFreccia += 94;
                        break;
                    case 2:
                        profilo = "Equilibrato";
                        yFreccia += 74;
                        break;
                    case 3:
                        profilo = "Dinamico";
                        yFreccia += 50;
                        break;
                    case 4:
                        profilo = "Aggressivo";
                        yFreccia += -4;
                        break;
                    default:
                        break;
                }

               

                page.Elements.Add(
                    new ceTe.DynamicPDF.PageElements.Rectangle (
                        xFreccia, 
                        yFreccia,
                        wFreccia,
                        hFreccia, 
                        0, 
                        new RgbColor(232, 236, 237)));
                        

                rect = new ceTe.DynamicPDF.PageElements.Rectangle(
                    xFreccia + wFreccia , 
                    yFreccia , 
                    hFreccia / 1.414F, 
                    hFreccia / 1.414F,
                    0, 
                    new RgbColor(232, 236, 237));

                rect.Angle = 45;
                page.Elements.Add(rect);

                xFreccia += 1;
                hFreccia = fontSize + 1;
                float interline = hFreccia + 1;

                page.Elements.Add(
                    new ceTe.DynamicPDF.PageElements.Label("Profilo finanziario:"
                        , xFreccia
                        , yFreccia 
                        , wFreccia
                        , hFreccia
                        , Globals.OpenTypeFontVerdana
                        , fontSize));

                page.Elements.Add(
                    new ceTe.DynamicPDF.PageElements.Label(profilo + " (" + (MarkPosition((float)var1) + 1).ToString() + ")"
                        , xFreccia
                        , yFreccia + interline
                        , wFreccia
                        , hFreccia
                        , Globals.OpenTypeFontVerdana
                        , fontSize));

                 page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("(VaR " + conv.ConvertNum(set.RischioPatrimonioFinanziario.Rows[0]["Var"]) + "%)"
                        , xFreccia
                        , yFreccia + (interline*2)
                        , wFreccia
                        , hFreccia
                        , Globals.OpenTypeFontVerdana
                        , fontSize));
            }

            #endregion

            #region Legenda alla destra del Grafico
                string relativePath = UtilityManager.getAppSetting("LegendaDiversificazioneLarge");
                System.Reflection.Assembly thisExe;
                thisExe = System.Reflection.Assembly.GetExecutingAssembly();
                System.IO.Stream file = thisExe.GetManifestResourceStream(relativePath);
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 400, document.getLastPos() + 103, 0.40F)); // T-1466629-Y0Y5 +80
            #endregion

            // document.setLastPos(-document.getLastPos()+695);  T-1466629-Y0Y5
            if (datitab.table.Rows.Count > 1) //
                document.setLastPos(-document.getLastPos() + 705);
            else
                document.setLastPos(-document.getLastPos() + 695);

            string nota = "";
            if(dataThread.TotalSelfNegCurrentAccountValue!=0){
                nota = "(*) In tabella non vengono considerati eventuali saldi negativi di conto corrente ed investimenti in corso, né il saldo negativo della liquidità sottostante le linee “GP Eligo”.";
            }else{
                nota = "(*) In tabella non vengono considerati eventuali saldi negativi di conto corrente ed investimenti in corso.";
            }

            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(nota, datitab.getX(), document.getLastPos(), 500F, 20F, Globals.OpenTypeFontVerdana, fontSize));
            
            if (dataThread.profiloIsAct == false)
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(Resource.Nota_S96_Scaduto, datitab.getX(), document.getLastPos() + 10, 500F, 20F, Globals.OpenTypeFontVerdana, fontSize, ceTe.DynamicPDF.CmykColor.Red));

            //document.setLastPos(800);  T-1466629-Y0Y5
            if (datitab.table.Rows.Count > 1)
                document.setLastPos(-document.getLastPos() + 822);
            else
                document.setLastPos(-document.getLastPos() + 800);

        }

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

        private int MarkPosition(float var)
        {
            int index = 0;

            if (var < Convert.ToDouble(BusinessLayer.SoglieVar.sVar1))
                index = 0;
            if ((Convert.ToDouble(BusinessLayer.SoglieVar.sVar1) < var) && (var <= Convert.ToDouble(BusinessLayer.SoglieVar.sVar2)))
                index = 1;
            if ((Convert.ToDouble(BusinessLayer.SoglieVar.sVar2) < var) && (var <= Convert.ToDouble(BusinessLayer.SoglieVar.sVar3)))
                index = 2;
            if ((Convert.ToDouble(BusinessLayer.SoglieVar.sVar3) < var) && (var <= Convert.ToDouble(BusinessLayer.SoglieVar.sVar4)))
                index = 3;
            if ((Convert.ToDouble(BusinessLayer.SoglieVar.sVar4) < var))
                index = 4;

            return index;
        }

        /// <summary>
        /// Disegna il simbolo accanto alla descrizione del patrimonio nella colonna Patrimonio.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="datasource"></param>
        private void writeMarker(DocumentPDF document, DatiTabella datasource)
        {
            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
            float y = datasource.GetRowDim();
            float yTabella = datasource.getY();
            ceTe.DynamicPDF.PageElements.Rectangle rect;

            foreach (DataRow myRow in datasource.table.Rows)
            {
                switch (myRow["Patrimonio"].ToString().Trim().ToLower())
                {

                    //FC 26062015 Aggionamento nuova Ragione Sociale
                    //FC 25012016 case "patrimonio banca fideuram":
                    case "patrimonio fideuram":
                    case "patrimonio sanpaolo invest":
                    case "patrimonio san paolo invest":
                    case "patrimonio attuale":
                        //Marker 1
                        rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft(), yTabella - 5 + y, 10 / 1.414F, 10 / 1.414F, 0, new RgbColor(0, 0, 0));
                        rect.Angle = 45;
                        page.Elements.Add(rect);
                        y += datasource.GetRowDim();
                        break;

                    case "patrimonio complessivo":
                        //Marker 2
                        rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() - 4, yTabella - 4 + y, 8, 8, 0, new RgbColor(0, 0, 0));
                        //rect.CornerRadius = 5;
                        page.Elements.Add(rect);
                        y += datasource.GetRowDim();
                        break;

                    case "patrimonio altri istituti":
                    case "patrimonio prospettico":
                        //Marker 3
                        rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() - 5, yTabella - 5 + y, 10, 10, 0, new RgbColor(0, 0, 0));
                        rect.CornerRadius = 5;
                        page.Elements.Add(rect);
                        y += datasource.GetRowDim();
                        break;

                    default:
                        break;
                }
            }
        }

        




    }
}