package com.elab.redis.config;

import com.elab.redis.CacheTemplate;
import com.elab.redis.annotation.CacheLoopSubmit;
import com.elab.redis.annotation.CacheReadLock;
import com.elab.redis.annotation.CacheWriteLock;
import com.elab.redis.interceptor.LockCacheAttributeSourceAdvisor;
import com.elab.redis.interceptor.LockCacheAttributeSourcePointcut;
import com.elab.redis.interceptor.LockCacheInterceptor;
import com.elab.redis.redisson.DefaultRedissonSpringCacheManager;
import com.elab.redis.serializers.CompatibilityJsonRedisSerializer;
import com.elab.redis.spring.data.RedisTemplateDecorator;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 缓存自动配置
 *
 * @author ： liukx
 * @time ： 2020/7/8 - 19:12
 */
@Configuration
@EnableConfigurationProperties({RedisProperties.class, ElabRedisProperties.class})
@ComponentScan(value = {"com.elab.redis.interceptor.impl"})
public class CacheAutoConfiguration {

    @Bean
    public CacheTemplate cacheTemplate(@Autowired RedissonClient redissonClient, @Autowired RedisTemplate<String,
            Object> redisTemplate) {
        CacheTemplate cacheTemplate = new CacheTemplate();
        cacheTemplate.setRedissonClient(redissonClient);
        cacheTemplate.setRedisTemplate(redisTemplate);
        return cacheTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(value = {RedisTemplate.class})
    public RedisTemplate<String, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws Exception {
        RedisTemplate<String, Object> template = new RedisTemplateDecorator<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new CompatibilityJsonRedisSerializer());
//        template.setHashKeySerializer(new StringRedisSerializer());
//        template.setHashValueSerializer(new StringRedisSerializer());
        return template;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public LockCacheAttributeSourceAdvisor transactionAdvisor() {
        LockCacheAttributeSourceAdvisor advisor = new LockCacheAttributeSourceAdvisor();
        // 具体的拦截器
        advisor.setAdvice(redisCacheInterceptor());
        // 需要被拦截方法的规则判断器，一旦符合，才会被代理给拦截器处理
        advisor.setPointcut(cacheAttributeSourcePointcut());
        return advisor;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public LockCacheAttributeSourcePointcut cacheAttributeSourcePointcut() {
        // 该类是用作比对方法的注解是否符合代理的条件
        LockCacheAttributeSourcePointcut cacheAttributeSourcePointcut = new LockCacheAttributeSourcePointcut();
        cacheAttributeSourcePointcut.addAnnotations(CacheLoopSubmit.class);
        cacheAttributeSourcePointcut.addAnnotations(CacheReadLock.class);
        cacheAttributeSourcePointcut.addAnnotations(CacheWriteLock.class);
        return cacheAttributeSourcePointcut;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public LockCacheInterceptor redisCacheInterceptor() {
        LockCacheInterceptor interceptor = new LockCacheInterceptor();
        return interceptor;
    }

    @Bean
    public CacheManager cacheManager(RedissonClient redissonClient, ElabRedisProperties elabRedisProperties) {
        // 统一的注解缓存失效配置
        DefaultRedissonSpringCacheManager defaultRedissonSpringCacheManager = new DefaultRedissonSpringCacheManager(redissonClient);

        return defaultRedissonSpringCacheManager;
    }

//    @Bean
//    public CacheManager cacheManager(RedissonConnectionFactory connectionFactory, ElabRedisProperties redisProperties) {
//        // 统一的注解缓存失效配置
//        RedisSerializationContext.SerializationPair<String> keySerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
//        RedisSerializationContext.SerializationPair<Object> valueSerialization = RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer());
//        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().serializeKeysWith(keySerializationPair)
//                .serializeValuesWith(valueSerialization).computePrefixWith(cacheName -> redisProperties.getPrefixWith() + cacheName)
//                .entryTtl(Duration.ofMinutes(redisProperties.getTtl()));
//        return RedisCacheManager.builder(connectionFactory).cacheDefaults(configuration).build();
//    }

//    @Bean
//    public CacheManager cacheManager(@Autowired RedissonClient redissonClient){
//        RedissonSpringCacheManager redissonSpringCacheManager = new DefaultRedissonSpringCacheManager(redissonClient);
//
//    }
}
