using System;
using StackExchange.Redis;

namespace VCommon.Caching
{
    public struct CacheValue
    {
        private RedisValue _redisValue;

        public static implicit operator CacheValue(RedisValue value) => new CacheValue { _redisValue = value };

        public static implicit operator RedisValue(CacheValue self) => self._redisValue;

        public static explicit operator CacheValue(long value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(long? value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(double value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(double? value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(bool value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(bool? value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(string value) => new CacheValue { _redisValue = value };

        public static explicit operator CacheValue(Guid value) => new CacheValue { _redisValue = value.ToString() };

        public static explicit operator CacheValue(Guid? value) => new CacheValue { _redisValue = value?.ToString() };

        public static explicit operator CacheValue(DateTime value) => new CacheValue { _redisValue = value.Ticks };

        public static explicit operator CacheValue(DateTime? value) => new CacheValue { _redisValue = value?.Ticks };

        public static explicit operator double(CacheValue self) => (double)self._redisValue;

        public static explicit operator double? (CacheValue self) => (double?)self._redisValue;

        public static explicit operator long(CacheValue self) => (long)self._redisValue;

        public static explicit operator long? (CacheValue self) => (long?)self._redisValue;

        public static explicit operator bool(CacheValue self) => (bool)self._redisValue;

        public static explicit operator bool? (CacheValue self) => (bool?)self._redisValue;

        public static explicit operator string(CacheValue self) => self._redisValue;

        public static explicit operator Guid(CacheValue self) => new Guid((string)self._redisValue);

        public static explicit operator Guid? (CacheValue self) => self._redisValue.IsNull ? (Guid?)null : new Guid((string)self._redisValue);

        public static explicit operator DateTime? (CacheValue self) => self._redisValue.IsNull ? (DateTime?)null : new DateTime((long)self._redisValue);

        public static explicit operator DateTime(CacheValue self) => new DateTime((long)self._redisValue);

        public bool IsNull => _redisValue.IsNull;
    }
}