using Microsoft.AspNetCore.Mvc.Formatters;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace MS.WebCore.Core
{
    public class RedisExample
    {
        private static ConnectionMultiplexer redisConnection;
        private static IDatabase redisDb;

        public RedisExample()
        {
            redisConnection = ConnectionMultiplexer.Connect("localhost");
            redisDb = redisConnection.GetDatabase();
        }

        public void SetData(string key, string value)
        {
            redisDb.StringSet(key, value);
        }

        public string GetData(string key)
        {
            return redisDb.StringGet(key);
        }

        public void DeleteData(string key, string field)
        {
            redisDb.HashDelete(key, field);
            //redisDb.KeyDelete(key);
        }
        public void DeleteData(string key)
        {
            redisDb.KeyDelete(key);
            //redisDb.KeyDelete(key);
        }
        public  bool HashKeyExists(string hashKey, string fieldKey)
        {
            return redisDb.HashExists(hashKey, fieldKey);
        }
        public void SetDataWithTime(string key, string value, DateTime time)
        {
            redisDb.SortedSetAdd("mySortedSet", value, time.Ticks);
        }
        public string GetValueByTime(DateTime time)
        {
            var result = redisDb.SortedSetRangeByScore("mySortedSet", time.Ticks, time.Ticks);
            return result.FirstOrDefault();
        }

        public void AddToHash(string key, string field, string value)
        {
            RedisType keyType = redisDb.KeyType(key);
            if (keyType != RedisType.None && keyType != RedisType.Hash)
            {
                // throw new Exception("Key is not of type Hash");
            }

            redisDb.HashSet(key, field, value);
        }


        //public List<string> GetListValues()
        //{
        //    var values = redisDb.ListRange("myList");

        //    List<string> result = new List<string>();
        //    foreach (var value in values)
        //    {
        //        result.Add(value.ToString());
        //    }

        //    return result;
        //}

        public List<string> GetValuesByUUID(string uuid)
        {
            var values = redisDb.ListRange("myList");
            List<string> result = new List<string>();

            foreach (var value in values)
            {
                var parts = value.ToString().Split(new[] { ' ' }, 2);
                var storedUUID = parts[0];

                if (storedUUID == uuid)
                {
                    result.Add(value.ToString());
                }
            }

            return result;
        }


        public List<KeyValuePair<string, string>> GetListValues()
        {
            var values = redisDb.HashGetAll("myList");

            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            foreach (var value in values)
            {
                result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
            }

            return result;
        }

        public List<KeyValuePair<string, string>> GetValuesByKey(string key)
        {
            var values = redisDb.HashGetAll("myList");
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

            foreach (var value in values)
            {
                if (value.Name == key)
                {
                    result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
                }
            }

            return result;
        }
        public void AddToList(string key, string value)
        {
            RedisType keyType = redisDb.KeyType(key);
            if (keyType != RedisType.None && keyType != RedisType.Hash)
            {
                //
                //throw new Exception("Key is not of type Hash");
            }

            redisDb.HashSet(key, value, value);
        }

        public List<KeyValuePair<string, string>> GetListValues(string key)
        {
            RedisType keyType = redisDb.KeyType(key);
            if (keyType != RedisType.Hash)
            {
                // throw new Exception("Key is not of type Hash");
            }

            var values = redisDb.HashGetAll(key);

            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            foreach (var value in values)
            {
                result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
            }

            return result;
        }
        public List<string> GetHashKeysByPattern(string pattern)
        {
            List<string> keys = new List<string>();

            // 使用服务器的 EndPoint 获取服务器的键
            EndPoint[] endPoints = redisDb.Multiplexer.GetEndPoints();
            foreach (EndPoint endPoint in endPoints)
            {
                IServer server = redisDb.Multiplexer.GetServer(endPoint);
                keys.AddRange(server.Keys(pattern: pattern, database: redisDb.Database).Select(key => (string)key));
            }

            return keys;
        }
    }
}
