using DevExpress.Export;
using DevExpress.Web;
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OlympiaIntranet
{
    public partial class LimitiUcits : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }
        }

        protected void btnCaricaDati_Click(object sender, EventArgs e)
        {
            Session["cod_rap"] = txtPortafoglio.Text;

            // Formattazioni condizionali - colonna 10% (OLY)
            GridViewFormatConditionHighlight Rule1 = new GridViewFormatConditionHighlight();
            Rule1.FieldName = "10% (OLY)";
            Rule1.Expression = "[10% (OLY)] <= 0.1 and [10% (OLY)] > 0";
            Rule1.Format = GridConditionHighlightFormat.YellowFillWithDarkYellowText;
            gvLimitiUcits.FormatConditions.Add(Rule1);

            GridViewFormatConditionHighlight Rule11 = new GridViewFormatConditionHighlight();
            Rule11.FieldName = "10% (OLY)";
            Rule11.Expression = "[10% (OLY)] > 0.1";
            Rule11.Format = GridConditionHighlightFormat.LightRedFillWithDarkRedText;
            gvLimitiUcits.FormatConditions.Add(Rule11);

            // Formattazioni condizionali - colonna 10% (C4F)
            GridViewFormatConditionHighlight Rule2 = new GridViewFormatConditionHighlight();
            Rule2.FieldName = "10% (C4F)";
            Rule2.Expression = "[10% (C4F)] <= 0.1 and [10% (C4F)] > 0";
            Rule2.Format = GridConditionHighlightFormat.YellowFillWithDarkYellowText;
            gvLimitiUcits.FormatConditions.Add(Rule2);

            GridViewFormatConditionHighlight Rule21 = new GridViewFormatConditionHighlight();
            Rule21.FieldName = "10% (C4F)";
            Rule21.Expression = "[10% (C4F)] > 0.1";
            Rule21.Format = GridConditionHighlightFormat.LightRedFillWithDarkRedText;
            gvLimitiUcits.FormatConditions.Add(Rule21);

            // Formattazioni condizionali - colonna 5%-40% (OLY)
            GridViewFormatConditionHighlight Rule3 = new GridViewFormatConditionHighlight();
            Rule3.FieldName = "5%-40% (OLY)";
            Rule3.Expression = "[5%-40% (OLY)] >= 0.05 and [5%-40% (OLY)] <= 0.1";
            Rule3.Format = GridConditionHighlightFormat.YellowFillWithDarkYellowText;
            gvLimitiUcits.FormatConditions.Add(Rule3);

            GridViewFormatConditionHighlight Rule31 = new GridViewFormatConditionHighlight();
            Rule31.FieldName = "5%-40% (OLY)";
            Rule31.Expression = "[5%-40% (OLY)] > 0.1 and [5%-40% (OLY)] <> 0";
            Rule31.Format = GridConditionHighlightFormat.LightRedFillWithDarkRedText;
            gvLimitiUcits.FormatConditions.Add(Rule31);

            // Formattazioni condizionali - colonna 10% (C4F)
            GridViewFormatConditionHighlight Rule4 = new GridViewFormatConditionHighlight();
            Rule4.FieldName = "5%-40% (C4F)";
            Rule4.Expression = "[5%-40% (C4F)] >= 0.05 and [5%-40% (C4F)] <= 0.1";
            Rule4.Format = GridConditionHighlightFormat.YellowFillWithDarkYellowText;
            gvLimitiUcits.FormatConditions.Add(Rule4);

            GridViewFormatConditionHighlight Rule41 = new GridViewFormatConditionHighlight();
            Rule41.FieldName = "5%-40% (C4F)";
            Rule41.Expression = "[5%-40% (C4F)] > 0.1 and [5%-40% (C4F)] <> 0";
            Rule41.Format = GridConditionHighlightFormat.LightRedFillWithDarkRedText;
            gvLimitiUcits.FormatConditions.Add(Rule41);

            gvLimitiUcits.DataBind();

            if (gvLimitiUcits.VisibleRowCount == 0)
            {
                CommonClass.MessageBox(this,"Consultare la posizione in Guardian e ripetere la richiesta!");
              //  throw new MyException("Consultare la posizione in Guardian e ripetere la richiesta!");
            }
        }

        protected void gvLimitiUcits_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != GridViewRowType.Data) return;

            // Colora sottostanti che non hanno emittente
            string Emittente = e.GetValue("Emittente").ToString();
            string Descrizione = e.GetValue("Descrizione").ToString();
            
            if (String.IsNullOrEmpty(Emittente) && Descrizione== "SOTTOSTANTE NON TROVATO!")
            {
                e.Row.BackColor = Color.OrangeRed;              
            }
            
        }

        protected void btnEsportaExcel_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.ExportSelectedRowsOnly = false;
            ASPxGridViewExporter1.FileName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - Esposizione Titoli";
            XlsxExportOptionsEx op = new XlsxExportOptionsEx() { ExportType = ExportType.WYSIWYG }; 
            ASPxGridViewExporter1.WriteXlsxToResponse(op);
        }

        protected void gvLimitiUcits_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;
        }

        protected void gvLimitiUcits_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.DataColumn.FieldName == "Descrizione")
            {
                if (e.CellValue != null)
                {
                    // Se viene rilevato un sintetico , allora viene caricato l'elenco dei possibili certificati all'interno del portafoglio guardian eventualmente da controllare 
                    if (e.CellValue.ToString() == "POSSIBILE SINTETICO") 
                    {
                        e.Cell.BackColor = ColorTranslator.FromHtml("#e44e2f"); // evidenzia cella con possibile errore da sistemare 

                        if (Session["cod_rap"] != null)
                        {
                            using (var con = new SqlConnection(Properties.Settings.Default.SqlConnection))
                            {
                                var cmd = new SqlCommand
                                {
                                    CommandText = "GDN_EsposizioneTitoli_CTF",
                                    CommandType = CommandType.StoredProcedure,
                                    Connection = con
                                };
                                cmd.Parameters.Add("@cod_rap", SqlDbType.VarChar).Value = Session["cod_rap"].ToString();
                                con.Open(); // Open the SqlConnection.
                                object objResult = cmd.ExecuteScalar();
                                if (objResult != null)
                                {
                                    e.Cell.ToolTip = (string)objResult;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}