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.BeanFactoryCacheAttributeSourceAdvisor;
import com.elab.redis.interceptor.CacheAttributeSourcePointcut;
import com.elab.redis.interceptor.CacheInterceptor;
import com.elab.redis.redisson.DefaultRedissonSpringCacheManager;
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.cache.CacheManager;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 缓存自动配置
 *
 * @author ： liukx
 * @time ： 2020/7/8 - 19:12
 */
@Configuration
@Import({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
    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 GenericJackson2JsonRedisSerializer());
        return template;
    }

//    private Jackson2JsonRedisSerializer jackson2JsonRedisSerialize(){
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        return jackson2JsonRedisSerializer;
//    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public BeanFactoryCacheAttributeSourceAdvisor transactionAdvisor() {
        BeanFactoryCacheAttributeSourceAdvisor advisor = new BeanFactoryCacheAttributeSourceAdvisor();
        advisor.setAdvice(cacheInterceptor());
        advisor.setPointcut(cacheAttributeSourcePointcut());
        return advisor;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public CacheAttributeSourcePointcut cacheAttributeSourcePointcut() {
        CacheAttributeSourcePointcut cacheAttributeSourcePointcut = new CacheAttributeSourcePointcut();
        cacheAttributeSourcePointcut.addAnnotations(CacheLoopSubmit.class);
        cacheAttributeSourcePointcut.addAnnotations(CacheReadLock.class);
        cacheAttributeSourcePointcut.addAnnotations(CacheWriteLock.class);
        return cacheAttributeSourcePointcut;
    }

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

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

        return defaultRedissonSpringCacheManager;
    }
//    @Bean
//    public CacheManager cacheManager(RedisConnectionFactory 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();
//    }
}
