package com.tanpu.community.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;

@Configuration
@EnableCaching
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.max-active}")
    private int maxActive;
    @Value("${spring.redis.max-wait}")
    private long maxWaitMillis;
    @Value("${spring.redis.max-idle}")
    private int maxIdle;

//    @Bean
//    public JedisPoolConfig jedisPoolConfig() {
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        // maximum connection
//        jedisPoolConfig.setMaxTotal(maxActive);
//        // Maximum wait time when no connection is available in the pool
//        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
//        // Maximum number of idle connections
//        jedisPoolConfig.setMinIdle(maxIdle);
//        // Other properties can be added by yourself
//        return jedisPoolConfig;
//    }
//
//    @Bean
//    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
//                .poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
//        return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
//    }
//
//    @Bean
//    public RedisTemplate<String, Object> redisTemplate() {
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
//
//        return template;
//    }
//
//    /**
//     * cache
//     */
//    @Bean
//    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
//        return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
//                .withCacheConfiguration("tempCache",
//                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
//                .withCacheConfiguration("longCache",
//                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
//    }

    @Bean("communityKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new RedisConfig.CommunityKeyGenerator();
    }

    public static class CommunityKeyGenerator implements KeyGenerator {
        public Object generate(Object target, Method method, Object... params) {
            // todo prefix 加到common
            return "new_community_" + target.getClass().getSimpleName() + "_"
                    + method.getName() + "_"
                    + StringUtils.arrayToDelimitedString(params, "_");
        }
    }
}