using System;
using Server.DB.Redis;
using StackExchange.Redis;

namespace GameDAL.Game
{
    public static class SessionUUID
    {
        private const string key = "SessionUUID";
        
        /// <summary>
        /// 一天时间再过期
        /// </summary>
        private const int ExpireTime = 24 * 60;
        
        public static string GetSessionUUIDKey(string sessionUUID)
        {
            return $"{key}:{sessionUUID}";
        }

        public static void UpdateExpiredTime(string sessionUUID)
        {
            redisManager.db.KeyExpire(GetSessionUUIDKey(sessionUUID), TimeSpan.FromMinutes(ExpireTime));
        }

        public static string CreateSessionUUID(int userid)
        {
            string sessionUUID = Guid.NewGuid().ToString().Replace("-", "");
            redisManager.db.StringSet(GetSessionUUIDKey(sessionUUID), userid, TimeSpan.FromMinutes(ExpireTime));
            return sessionUUID;
        }

        public static int GetUserIdBySessionUUID(string sessionUUID)
        {
            RedisValue value = redisManager.db.StringGet(GetSessionUUIDKey(sessionUUID));
            if (value.HasValue && int.TryParse(value, out int userid) && userid > 0)
            {
                return userid;
            }

            return -1;
        }
    }
}