using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EgwCApp.Core
{
    public class RedisMan
    {
        #region Public Constructors

        public RedisMan(string server, int port, int db)
        {
            servAddr = server;
            servPort = port;
            redisDb = db;
            connString = $"{servAddr}:{servPort},abortConnect=false,ssl=false";
            connRedis = ConnectionMultiplexer.Connect(connString);
        }

        #endregion Public Constructors

        #region Public Properties

        /// <summary>
        /// Oggetto DB REDIS corrente
        /// </summary>
        public IDatabase cache
        {
            get
            {
                IDatabase answ;
                // se già valorizzato uso oggetto private...
                if (_currDB != null)
                {
                    answ = _currDB;
                }
                else
                {
                    // init DB
                    answ = connRedis.GetDatabase(redisDb);
                    _currDB = answ;
                }
                // restituisco oggetto DB
                return answ;
            }
        }

        /// <summary>
        /// Oggetto statico connessione redis
        /// </summary>
        public ConnectionMultiplexer connRedis { get; set; }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Restituisce una chiave salvata in RedisCache
        /// </summary>
        /// <param name="chiave"></param>
        /// <returns></returns>
        public string getRSV(string chiave)
        {
            string answ = "";
            try
            {
                answ = cache.StringGet(chiave);
            }
            catch (Exception exc)
            {
#if false
                Logging.Instance.Info($"getRSV {exc}");
#endif
            }
            return answ;
        }

        /// <summary>
        /// Salva una chiave in RedisCache
        /// </summary>
        /// <param name="chiave"></param>
        /// <param name="valore"></param>
        /// <returns></returns>
        public bool setRSV(string chiave, string valore)
        {
            bool answ = false;
            try
            {
                cache.StringSet(chiave, valore);
                answ = true;
            }
            catch (Exception exc)
            {
#if false
                Logging.Instance.Error($"setRSV {exc}");
#endif
            }
            return answ;
        }

        /// <summary>
        /// Salva una chiave in RedisCache
        /// </summary>
        /// <param name="chiave"></param>
        /// <param name="valore"></param>
        /// <param name="TTL_sec">in secondi</param>
        /// <returns></returns>
        public bool setRSV(string chiave, string valore, int TTL_sec)
        {
            bool answ = false;
            try
            {
                TimeSpan expT = new TimeSpan(0, 0, TTL_sec);
                // salvo con expyry...
                cache.StringSet(chiave, valore, expT);
                answ = true;
            }
            catch (Exception exc)
            {
#if false
                Logging.Instance.Error($"setRSV {exc}");
#endif
            }
            return answ;
        }

        #endregion Public Methods

        #region Protected Fields

        protected int redisDb = 0;
        protected string servAddr = "";
        protected int servPort = 0;

        #endregion Protected Fields

        #region Private Fields

        private string connString = "";

        #endregion Private Fields

        #region Private Properties

        /// <summary>
        /// Oggetto currentDb REDIS locale
        /// </summary>
        private IDatabase _currDB { get; set; }

        #endregion Private Properties
    }
}