package com.dayou.configuration;

import com.dayou.constants.JwtConstants;
import com.dayou.entity.GisUser;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.dayou.bo.ChatCacheSessionBO;
import com.dayou.bo.LoginCacheUserBO;
import com.dayou.bo.UserBaseInfoBo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class CacheConfig {
    /**
     * 用户菜单API接口鉴权缓存KEY
     */
    public static final String USER_AUTH_MENU_API = "USER_AUTH_MENU_API";

    @Value("${baidu.ai.tokenexpire:20}")
    private Integer tokenExpire;
    @Value("${sms.expire:15}")
    private Integer smsExpire;
    @Value("${system.login.error-limit.lock-time:30}")
    private Integer accountLockingExpire;



    @Bean
    @Qualifier("loginCache")
    public Cache<Long, LoginCacheUserBO> loginCache() {
        Cache<Long, LoginCacheUserBO> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(JwtConstants.EXPIRATION, TimeUnit.SECONDS).build();
        return cache;
    }

    @Bean
    @Qualifier("oauthCache")
    public Cache<Long, UserBaseInfoBo> oauthCache() {
        Cache<Long, UserBaseInfoBo> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(JwtConstants.EXPIRATION, TimeUnit.SECONDS).build();
        return cache;
    }


    @Bean
    @Qualifier("baiduaiCache")
    public Cache<String, String> baiduaiCache() {
        Cache<String, String> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(tokenExpire, TimeUnit.DAYS).build();
        return cache;
    }

    @Bean
    @Qualifier("smsCache")
    public Cache<String, List<String>> smsCache() {
        Cache<String, List<String>> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(smsExpire, TimeUnit.MINUTES).build();
        return cache;
    }

    @Bean
    @Qualifier("lockCache")
    public Cache<String, Object> lockCache() {
        Cache<String, Object> cache = CacheBuilder.newBuilder().build();
        return cache;
    }


    @Bean
    @Qualifier("commonCache")
    public Cache<String, Object> commonCache() {
        Cache<String, Object> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("loginErrorCountCache")
    public Cache<String, AtomicInteger> loginErrorCountCache() {
        Cache<String, AtomicInteger> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(24, TimeUnit.HOURS)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("accountLockingCache")
    public Cache<String, Integer> accountLockingCache() {
        Cache<String, Integer> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(accountLockingExpire, TimeUnit.MINUTES)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("noExpireCache")
    public Cache<String, Object> noExpireCache() {
        Cache<String, Object> cache = CacheBuilder.newBuilder().build();
        return cache;
    }

    @Bean
    @Qualifier("sdkAccessLimitCache")
    public Cache<String, AtomicInteger> sdkAccessLimitCache() {
        Cache<String, AtomicInteger> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(24, TimeUnit.HOURS)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("formTokenCache")
    public Cache<String, String> formTokenCache() {
        Cache<String, String> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(30, TimeUnit.MINUTES)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("chatSessionCache")
    public Cache<String, ChatCacheSessionBO> chatSessionCache() {
        Cache<String, ChatCacheSessionBO> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(10, TimeUnit.MINUTES)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("verificationCodeCache")
    public Cache<String,String> verificationCodeCache(){
        Cache<String, String> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(1, TimeUnit.MINUTES)
                .build();
        return cache;
    }

    @Bean
    @Qualifier("gisLoginCache")
    public Cache<String, GisUser> gisLoginCache() {
        Cache<String, GisUser> cache = CacheBuilder.newBuilder()
                .expireAfterAccess(JwtConstants.GIS_EXPIRATION, TimeUnit.SECONDS).build();
        return cache;
    }

}

