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

namespace OASystem.RedisRepository.RedisAsyncHelper
{
    /// <summary>
    /// Redis异步操作类  List部分类
    /// </summary>
    internal partial class RedisHelper //: IRedisHelper
    {
        //    _client.ListRightPopLeftPushAsync();
        //    _client.ListTrimAsync();

        #region List 写操作

        public async Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value)
        {
            return await _client.ListInsertAfterAsync(key, pivot, SerializeHelper.Serialize(value));
        }

        public async Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value)
        {
            return await _client.ListInsertBeforeAsync(key, pivot, SerializeHelper.Serialize(value));
        }

        public async Task<T> ListLeftPopAsync<T>(string key)
        {
            return SerializeHelper.Deserialize<T>(await _client.ListLeftPopAsync(key));
        }

        public async Task<long> ListLeftPushAsync<T>(string key, T value)
        {
            return await _client.ListLeftPushAsync(key, SerializeHelper.Serialize(value));
        }

        public async Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values)
        {
            return await _client.ListLeftPushAsync(key, SerializeHelper.Serialize(RedisInnerTypeHelper.GenericListToRedisValues(values)));
        }

        public async Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L)
        {
            return await _client.ListRemoveAsync(key, SerializeHelper.Serialize(value), count);
        }

        public async Task<T> ListRightPopAsync<T>(string key)
        {
            return SerializeHelper.Deserialize<T>(await _client.ListRightPopAsync(key));
        }

        public async Task<long> ListRightPushAsync<T>(string key, T value)
        {
            return await _client.ListRightPushAsync(key, SerializeHelper.Serialize(value));
        }

        public async Task<long> ListRightMultiPushAsync<T>(string key, List<T> values)
        {
            return
                await
                    _client.ListRightPushAsync(key,
                        SerializeHelper.Serialize(RedisInnerTypeHelper.GenericListToRedisValues(values)));
        }

        public async Task ListSetByIndexAsync<T>(string key, int index, T value)
        {
            await _client.ListSetByIndexAsync(key, index, SerializeHelper.Serialize(value));
        }

        public async Task ListTrimAsync(string key, long start, long stop)
        {
            await _client.ListTrimAsync(key, start, stop);
        }
        #endregion

        #region List 读操作

        public async Task<T> ListGetByIndexAsync<T>(string key, long index)
        {
            return SerializeHelper.Deserialize<T>(await _client.ListGetByIndexAsync(key, index, CommandFlags.PreferSlave));
        }

        public async Task<long> ListLengthAsync<T>(string key)
        {
            return await _client.ListLengthAsync(key, CommandFlags.PreferSlave);
        }

        public async Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L)
        {
            return RedisInnerTypeHelper.RedisValuesToGenericList<T>(await _client.ListRangeAsync(key, start, stop, CommandFlags.PreferSlave));
        }

        #endregion
    }
}
