using AppData;
using Newtonsoft.Json;
using SteamWare;
using System;
using System.Collections.Generic;

namespace NKC_WF
{
    public class BaseUserControl : System.Web.UI.UserControl
    {
        /// <summary>
        /// Generico evento di richiesta refresh a aprent
        /// </summary>
        public event EventHandler eh_doRefresh;
        /// <summary>
        /// Generico evento di richiesta refresh a aprent
        /// </summary>
        public event EventHandler eh_doReset;
        /// <summary>
        /// Chiamata evento
        /// </summary>
        public void raiseEvent()
        {
            // se qualcuno ascolta sollevo evento nuovo valore...
            if (eh_doRefresh != null)
            {
                eh_doRefresh(this, new EventArgs());
            }
        }
        /// <summary>
        /// Chiamata evento
        /// </summary>
        public void raiseReset()
        {
            // se qualcuno ascolta sollevo evento nuovo valore...
            if (eh_doReset != null)
            {
                eh_doReset(this, new EventArgs());
            }
        }
        /// <summary>
        /// Wrapper traduzione termini
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(string lemma)
        {
            return SteamWare.user_std.UtSn.Traduci(lemma);
        }

        protected Dictionary<int, string> anagMateriali
        {
            set
            {
                string jsonData = JsonConvert.SerializeObject(value);
                memLayer.ML.setRSV("anagMateriali", jsonData, 60);
            }
            get
            {
                Dictionary<int, string> answ = new Dictionary<int, string>();
                //cerco in redis...
                string rawData = memLayer.ML.getRSV("anagMateriali");
                if (!string.IsNullOrEmpty(rawData))
                {
                    answ = JsonConvert.DeserializeObject<Dictionary<int, string>>(rawData);
                }
                // sennò nel DB e salvo in redis...
                else
                {
                    var tabMat = DataLayer.man.taMat.GetData();
                    foreach (var item in tabMat)
                    {
                        answ.Add(item.MatID, item.MatDesc);
                    }
                    // salvo in redis 
                    anagMateriali = answ;
                }
                // restituisco
                return answ;
            }
        }

        public string matByKey(object _matId)
        {
            string answ = "";
            int matId = 0;
            int.TryParse(_matId.ToString(), out matId);
            try
            {
                answ = anagMateriali[matId];
            }
            catch
            { }
            return answ;
        }
        /// <summary>
        /// Recupera IP utente
        /// </summary>
        /// <returns></returns>
        public string currIpAddress()
        {
            string userip = Request.UserHostAddress;
            //if (Request.UserHostAddress != null)
            //{
            //    Int64 macinfo = new Int64();
            //    string macSrc = macinfo.ToString("X");
            //    if (macSrc == "0")
            //    {
            //        if (userip == "127.0.0.1")
            //        {
            //            Response.Write("visited Localhost!");
            //        }
            //        else
            //        {
            //            lblIPAdd.Text = userip;
            //        }
            //    }
            //}
            return userip;
        }
        /// <summary>
        /// Restituisce URL immagine QRCode
        /// </summary>
        /// <param name="QrValue">Parametro da renderizzare con QRCode</param>
        /// <returns></returns>
        public string getImgUrl(object QrValue)
        {
            string baseUrl = $"{memLayer.ML.CRS("matrixUrl")}/HOME/QR_site/JSON?val=";
            string payload = "{'baseUrl':'{0}','parameters':['" + $"{QrValue}" + "']}";
            string answ = $"{baseUrl}{payload}";
            return answ;
        }
    }
}