package cache

import (
	"fmt"

	"github.com/go-redis/redis"
)

func (p *RedisCache) ZRevRange(key string, start, stop int64) ([]string, error) {
	if p.isCluster {
		return p.cluster.ZRevRange(key, start, stop).Result()
	}

	return p.client.ZRevRange(key, start, stop).Result()
}

func (p *RedisCache) ZRevRangeByScore(key string, max, min string, offset, count int64) ([]string, error) {
	if p.isCluster {
		return p.cluster.ZRevRangeByScore(key,
			redis.ZRangeBy{
				Min:    min,
				Max:    max,
				Offset: offset,
				Count:  count,
			}).Result()
	}

	return p.client.ZRevRangeByScore(key,
		redis.ZRangeBy{
			Min:    min,
			Max:    max,
			Offset: offset,
			Count:  count,
		}).Result()
}

func (p *RedisCache) ZAdd(key string, score float64, member interface{}) (int64, error) {
	if p.isCluster {
		return p.cluster.ZAdd(key,
			redis.Z{
				Score:  score,
				Member: member,
			}).Result()
	}

	return p.client.ZAdd(key,
		redis.Z{
			Score:  score,
			Member: member,
		}).Result()
}

func (p *RedisCache) ZCount(key, min, max string) (int64, error) {
	if p.isCluster {
		return p.cluster.ZCount(key, min, max).Result()
	}

	return p.client.ZCount(key, min, max).Result()
}

func (p *RedisCache) ZScore(key, member string) (float64, error) {
	if p.isCluster {
		return p.cluster.ZScore(key, member).Result()
	}

	return p.client.ZScore(key, member).Result()
}

func (p *RedisCache) ZRange(key string, start, stop int64) ([]string, error) {
	if p.isCluster {
		return p.cluster.ZRange(key, start, stop).Result()
	}

	return p.client.ZRange(key, start, stop).Result()
}

func (p *RedisCache) ZIncrBy(key string, incr float64, member string) (float64, error) {
	if p.isCluster {
		return p.cluster.ZIncrBy(key, incr, member).Result()
	}

	return p.client.ZIncrBy(key, incr, member).Result()
}

func (p *RedisCache) ZRem(key string, members ...interface{}) (int64, error) {
	if p.isCluster {
		return p.cluster.ZRem(key, members).Result()
	}

	return p.client.ZRem(key, members).Result()
}

func (p *RedisCache) ZUnionStore(dest string, weights []float64, aggregate string, keys ...string) (int64, error) {
	if p.isCluster {
		return 0, fmt.Errorf("cluster unsupport <zunionstore>.")
	}

	return p.client.ZUnionStore(dest,
		redis.ZStore{
			Weights:   weights,
			Aggregate: aggregate,
		},
		keys...).Result()
}

func (p *RedisCache) ZCard(key string) (int64, error) {
	if p.isCluster {
		return p.cluster.ZCard(key).Result()
	}

	return p.client.ZCard(key).Result()
}

func (p *RedisCache) ZRemRangeByScore(key, min, max string) (int64, error) {
	if p.isCluster {
		return p.cluster.ZRemRangeByScore(key, min, max).Result()
	}

	return p.client.ZRemRangeByScore(key, min, max).Result()
}
