package com.yanzhu.framework.ratelimiter.config;

import com.yanzhu.framework.ratelimiter.core.aop.RateLimiterAspect;
import com.yanzhu.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.keyresolver.impl.ClientIpRateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.keyresolver.impl.DefaultRateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.keyresolver.impl.ExpressionRateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.keyresolver.impl.ServerNodeRateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.keyresolver.impl.UserRateLimiterKeyResolver;
import com.yanzhu.framework.ratelimiter.core.redis.RateLimiterRedisDAO;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;

import java.util.List;

@AutoConfiguration
public class YanzhuRateLimiterConfiguration {

    @Bean
    @ConditionalOnBean(RedissonClient.class)
    public RateLimiterAspect rateLimiterAspect(List<RateLimiterKeyResolver> keyResolvers, RedissonClient redissonClient) {
        return new RateLimiterAspect(keyResolvers, new RateLimiterRedisDAO(redissonClient));
    }

    @Bean
    public DefaultRateLimiterKeyResolver defaultRateLimiterKeyResolver() {
        return new DefaultRateLimiterKeyResolver();
    }

    @Bean
    public UserRateLimiterKeyResolver userRateLimiterKeyResolver() {
        return new UserRateLimiterKeyResolver();
    }

    @Bean
    public ClientIpRateLimiterKeyResolver clientIpRateLimiterKeyResolver() {
        return new ClientIpRateLimiterKeyResolver();
    }

    @Bean
    public ServerNodeRateLimiterKeyResolver serverNodeRateLimiterKeyResolver() {
        return new ServerNodeRateLimiterKeyResolver();
    }

    @Bean
    public ExpressionRateLimiterKeyResolver expressionRateLimiterKeyResolver() {
        return new ExpressionRateLimiterKeyResolver();
    }

}
