using ClosedXML.Excel;
using DevExpress.Export;
using DevExpress.Web;
using DevExpress.XtraPrinting;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace OlympiaIntranet
{
    public partial class test : System.Web.UI.Page
    {
        string queryLoadGrid = "GDN_ElencoFondi6";

        protected void Page_Init(object sender, EventArgs e)
        {
            ASPxDateEdit1.Date = DateTime.Now.Date;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }

            CommonClass.SetColumnFilterContains(gridviewFondi);

            // Assegna la variabile di sessione Switch ad ogni postback
            if (IsPostBack==true)
            {
                Session["Switch"] = rblFiltro.SelectedIndex;
            }

            // Se Overall Score norm distr > 50% allora colora verdino
            GridViewFormatConditionHighlight Rule1 = new GridViewFormatConditionHighlight();
            Rule1.FieldName = "Overall Score (norm distr)";
            Rule1.Expression = "[Overall Score (norm distr)] >= 0.5";
            Rule1.Format = GridConditionHighlightFormat.LightGreenFill;
            gridviewFondi.FormatConditions.Add(Rule1);

            // Se Overall Score norm distr < 50% allora colora rossastro
            GridViewFormatConditionHighlight Rule2 = new GridViewFormatConditionHighlight();
            Rule2.FieldName = "Overall Score (norm distr)";
            Rule2.Expression = "[Overall Score (norm distr)] < 0.5";
            Rule2.Format = GridConditionHighlightFormat.LightRedFill;
            gridviewFondi.FormatConditions.Add(Rule2);

        }


        protected void rblFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Switch"] = rblFiltro.SelectedIndex;
        }

        protected void gridlookupCategorie_ValueChanged(object sender, EventArgs e)
        {
            object objCodice = gridlookupCategorie.GridView.GetRowValues(gridlookupCategorie.GridView.FocusedRowIndex, "Codice");
            if (String.IsNullOrEmpty(objCodice.ToString()) == false)  Session["Codice"] = gridlookupCategorie.GridView.GetRowValues(gridlookupCategorie.GridView.FocusedRowIndex, "Codice");
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {

        }

        protected void gridviewFondi_ContextMenuItemClick(object sender, ASPxGridViewContextMenuItemClickEventArgs e)
        {
            // RefreshData(); // ** da togliere x test!!

            // Nota: Se si aggiunge un menu contestuale , ricordarsi di modificare pure lato client la procedura OnContextMenuItemClick 
            var gridView = (ASPxGridView)sender;

            if (e.Item.Name == "scaricaFondoBBG")
            {
                string tickerBBG = (string)gridviewFondi.GetRowValues(gridView.FocusedRowIndex, "Bloomberg Ticker");
                string result = string.Empty;
                try
                {
                    result = CommonClass.CallWebApi(String.Format("https://api.smart-roots.net:4001/api/bbgFondi?tickerBBG={0}", tickerBBG));

                    if (result.IndexOf("[OK]") != -1)
                    {
                        string msg = string.Format("Dati fondo {0} scaricati da Bloomberg!",tickerBBG);
                        CommonClass.MessageBox(this, msg);
                        //RefreshData();

                    }
                }
                catch (Exception ex)
                {
                    string error = string.Format("[{0}] - " + "[ERRORE] Eccezione rilevata in scarico dati fondi da bbg <br /> <br /> Message: {1} <br /> InnerException: {2}", DateTime.Now, ex.Message, ex.InnerException);
                    CommonClass.MessageBox(this, error);
                }
            }
        }

        protected void gridviewFondi_FillContextMenuItems(object sender, ASPxGridViewContextMenuEventArgs e)
        {
            var gridView = (ASPxGridView)sender;
            if (e.MenuType == GridViewContextMenuType.Rows)
            {
                // Crea Context Menu per scaricare dati fondi da bbg
                var item = e.CreateItem("Scarica dati da BBG", "scaricaFondoBBG");
                item.BeginGroup = true;
                item.Image.IconID = "miscellaneous_publish_16x16";
                e.Items.Add(item);
            }
        }

        //protected void gridviewFondi_CustomErrorText(object sender, ASPxGridViewCustomErrorTextEventArgs e)
        //{
        //    // https://supportcenter.devexpress.com/ticket/details/e59/how-to-show-an-error-message-when-a-record-cannot-be-deleted
        //    if (e.Exception is MyException) e.ErrorText = e.Exception.Message;
        //}

        private void RefreshData()
        {
            SqlDSFondi.SelectCommand = queryLoadGrid;
            SqlDSFondi.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            Parameter p1 = SqlDSFondi.SelectParameters["Switch"];
            SqlDSFondi.SelectParameters.Remove(p1);
            SqlDSFondi.SelectParameters.Add("Switch", Session["Switch"].ToString());
            Parameter p2 = SqlDSFondi.SelectParameters["Codice"];
            SqlDSFondi.SelectParameters.Remove(p2);
            SqlDSFondi.SelectParameters.Add("Codice", Session["Codice"].ToString());
            gridviewFondi.DataBind();

        }
    }
}