package com.ruoyi.gateway.enhance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.gateway.enhance.entity.BlackList;
import com.ruoyi.gateway.enhance.entity.RateLimitRule;
import com.ruoyi.gateway.enhance.entity.RouteUser;
import com.ruoyi.gateway.enhance.service.RouteEnhanceCacheService;
import com.ruoyi.gateway.enhance.utils.RouteEnhanceCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author MrBird
 */
@Slf4j
@Service
public class RouteEnhanceCacheServiceImpl implements RouteEnhanceCacheService {

    private RedisService redisService;

    @Autowired(required = false)
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    public void saveAllBlackList(Flux<BlackList> blackList) {
        blackList.subscribe(b -> {
            String key = StringUtils.isNotBlank(b.getIp()) ?
                    RouteEnhanceCacheUtil.getBlackListCacheKey(b.getIp()) :
                    RouteEnhanceCacheUtil.getBlackListCacheKey();
            String value = JSONObject.toJSONString(b);
            redisService.setCacheSet(key, value);
        });
        log.info("Cache blacklist into redis >>>");
    }

    @Override
    public void saveBlackList(BlackList blackList) {
        String key = StringUtils.isNotBlank(blackList.getIp()) ?
                RouteEnhanceCacheUtil.getBlackListCacheKey(blackList.getIp()) :
                RouteEnhanceCacheUtil.getBlackListCacheKey();
        redisService.setCacheSet(key, JSONObject.toJSONString(blackList));
    }

    @Override
    public Set<Object> getBlackList(String ip) {
        String key = RouteEnhanceCacheUtil.getBlackListCacheKey(ip);
        return redisService.getCacheSet(key);
    }

    @Override
    public Set<Object> getBlackList() {
        String key = RouteEnhanceCacheUtil.getBlackListCacheKey();
        return redisService.getCacheSet(key);
    }

    @Override
    public void removeBlackList(BlackList blackList) {
        String key = StringUtils.isNotBlank(blackList.getIp()) ?
                RouteEnhanceCacheUtil.getBlackListCacheKey(blackList.getIp()) :
                RouteEnhanceCacheUtil.getBlackListCacheKey();
        redisService.setRemove(key, JSONObject.toJSONString(blackList));
    }

    @Override
    public void saveAllRateLimitRules(Flux<RateLimitRule> rateLimitRules) {
        rateLimitRules.subscribe(r -> {
            String key = RouteEnhanceCacheUtil.getRateLimitCacheKey(r.getRequestUri(), r.getRequestMethod());
            String value = JSONObject.toJSONString(r);
            redisService.setCacheObject(key, value);
        });
        log.info("Cache rate limit rules into redis >>>");
    }

    @Override
    public void saveRateLimitRule(RateLimitRule rateLimitRule) {
        String key = RouteEnhanceCacheUtil.getRateLimitCacheKey(rateLimitRule.getRequestUri(), rateLimitRule.getRequestMethod());
        redisService.setCacheObject(key, JSONObject.toJSONString(rateLimitRule));
    }


    @Override
    public Object getRateLimitRule(String uri, String method) {
        String key = RouteEnhanceCacheUtil.getRateLimitCacheKey(uri, method);
        return redisService.getCacheObject(key);
    }

    @Override
    public int getCurrentRequestCount(String uri, String ip) {
        String key = RouteEnhanceCacheUtil.getRateLimitCountKey(uri, ip);
        return redisService.hasKey(key) ? (int) redisService.getCacheObject(key) : 0;
    }

    @Override
    public void removeRateLimitRule(RateLimitRule rateLimitRule) {
        String key = RouteEnhanceCacheUtil.getRateLimitCacheKey(rateLimitRule.getRequestUri(), rateLimitRule.getRequestMethod());
        redisService.deleteObject(key);
    }

    @Override
    public void setCurrentRequestCount(String uri, String ip, Long time) {
        String key = RouteEnhanceCacheUtil.getRateLimitCountKey(uri, ip);
        redisService.setCacheObject(key, 1, time, TimeUnit.SECONDS);
    }

    @Override
    public void incrCurrentRequestCount(String uri, String ip) {
        String key = RouteEnhanceCacheUtil.getRateLimitCountKey(uri, ip);
        redisService.incr(key, 1L);
    }

}
