package com.qmth.themis.admin.config;

import com.qmth.themis.admin.interceptor.AuthInterceptor;
import com.qmth.themis.admin.interceptor.AuthThirdInterceptor;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.SysConfig;
import com.qmth.themis.business.service.ThemisCacheService;
import com.qmth.themis.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 路径拦截器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/27
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    ThemisCacheService themisCacheService;

    @Bean
    public AuthInterceptor AuthInterceptor() {
        return new AuthInterceptor();
    }

    @Bean
    public AuthThirdInterceptor AuthThirdInterceptor() {
        return new AuthThirdInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        SysConfig sysConfig = themisCacheService.addSysConfigCache(SystemConstant.ADMIN_NO_AUTH_URLS);
        Optional.ofNullable(sysConfig).orElseThrow(() -> new BusinessException("未配置admin无需鉴权的url"));

        List<String> list = Arrays.asList(StringUtils.split(sysConfig.getConfigValue(), ","));
        registry.addInterceptor(AuthInterceptor()).addPathPatterns(SystemConstant.ADMIN_ALL_PATH)
                .excludePathPatterns(list);
        registry.addInterceptor(AuthThirdInterceptor()).addPathPatterns(SystemConstant.SCORE_PUSH_PATH)
                .excludePathPatterns(list)
                .excludePathPatterns(SystemConstant.THIRD_ALL_PATH_LIST);
    }

    //    @Override
    //    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //        ObjectMapper objectMapper = new ObjectMapper();
    //        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    //        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    //
    //        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    //        //objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    //
    //        // 处理数字位数超过JS默认最大数字时精度差异问题
    //        SimpleModule simpleModule = new SimpleModule();
    //        simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
    //        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    //        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    //
    //        objectMapper.registerModule(simpleModule);
    //
    //        for (HttpMessageConverter<?> c : converters) {
    //            if (c instanceof MappingJackson2HttpMessageConverter) {
    //                ((MappingJackson2HttpMessageConverter) c).setObjectMapper(objectMapper);
    //            }
    //        }
    //    }

    //    @Override
    //    public void addCorsMappings(CorsRegistry registry) {
    //        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true)
    //                .allowedMethods("GET", "POST", "DELETE", "PUT").maxAge(3600);
    //    }
}