using System;
using System.Collections.Generic;

namespace VCommon.Caching
{
    public class HashCacheManager
    {
        private readonly string _key;
        private readonly FixedCacheManager _underlying;

        public HashCacheManager(string server, int db, string key)
        {
            _key = key;
            _underlying = new FixedCacheManager(server, db);
        }

        public void Clean() => _underlying.KeyDelete(_key);

        public bool Exist(string field) => _underlying.HashFieldExist(_key, field);

        public bool Delete(string field) => _underlying.HashFieldDelete(_key, field);

        public long Delete(params string[] fields) => _underlying.HashFieldDelete(_key, fields);

        public CacheValue Fetch(string field, Func<CacheValue> missingCacheValueProvider, CacheExpire expire = null) => _underlying.HashFieldFetch(_key, field, missingCacheValueProvider, expire);

        public T FetchJson<T>(string field, Func<T> missingCacheValueProvider, CacheExpire expire = null) => _underlying.HashFieldFetchJson(_key, field, missingCacheValueProvider, expire);

        public void SetJson<T>(string field, T value, CacheExpire expire = null) => _underlying.HashFieldSetJson(_key, field, value, expire);

        public T GetJson<T>(string field) => _underlying.HashFieldGetJson<T>(_key, field);

        public void PurgeAndSet(IReadOnlyDictionary<string, CacheValue> fields) => _underlying.HashFieldPurgeAndSet(_key, fields);

        public void DeleteByPattern(string pattern) => _underlying.HashFieldDeleteByPattern(_key, pattern);

        public IReadOnlyDictionary<string, CacheValue> GetByPattern(string pattern) => _underlying.HashFieldGetByPattern(_key, pattern);

        public IReadOnlyDictionary<string, T> GetByPatternJson<T>(string pattern) => _underlying.HashFieldGetByPatternJson<T>(_key, pattern);

        public void Set(string field, CacheValue value, CacheExpire expire = null) => _underlying.HashFieldSet(_key, field, value, expire);

        public CacheValue Get(string field) => _underlying.HashFieldGet(_key, field);

        public void Set(IReadOnlyDictionary<string, CacheValue> field, CacheExpire expire = null) => _underlying.HashFieldSet(_key, field, expire);

        public long Increment(string field) => _underlying.HashFieldIncrement(_key, field);

        public long Decrement(string field) => _underlying.HashFieldDecrement(_key, field);

        public IReadOnlyDictionary<string, CacheValue> Get(params string[] fields) => _underlying.HashFieldGet(_key, fields);

        public IReadOnlyDictionary<TK, TV> FetchAsDic<TK, TV>(
            IEnumerable<TK> fields,
            Func<TK, string> convertField, Func<IEnumerable<TK>,
            IReadOnlyDictionary<TK, TV>> missingCacheValueProvider,
            Func<TV, CacheValue> convertValueToCache,
            Func<CacheValue, TV> convertCacheToValue,
            CacheExpire expire = null)
        {
            return _underlying.HashFieldsFetchAsDic(_key, fields, convertField, missingCacheValueProvider, convertValueToCache, convertCacheToValue, expire);
        }
    }
}
