package com.copy.trade.config;

import com.copy.trade.interceptor.AuthenticationInterceptor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Locale;

@Configuration
public class WebMvcConfigration  implements WebMvcConfigurer{

    @Resource
    private AuthenticationInterceptor authenticationInterceptor;

    @Autowired
    private LocaleChangeInterceptor localeChangeInterceptor;


    String[] excludePathPatterns = {
            //swagger
            "/v2/api-docs",
            "/swagger-resources",
            "/swagger-resources/**",
            "/configuration/ui",
            "/configuration/security",
            "/swagger-ui.html",
            "/webjars/**",
            "/api-docs**",
            //不需要登陆
            "/error"
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(authenticationInterceptor)
                .excludePathPatterns(excludePathPatterns)
                .addPathPatterns("/**");
        registry.addInterceptor(localeChangeInterceptor);
    }

    /**
     * lang变更拦截器
     *
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setIgnoreInvalidLocale(true);
        lci.setParamName("lang");
        return lci;
    }

    /**
     * 国际化语言解析器
     * 从请求头 lang 或 URL 参数 lang 中读取语言，无需 Session，兼容移动端
     *
     * @return HeaderLocaleResolver
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new HeaderLocaleResolver();
    }


    /**
     * 响应添加etag，用于做get请求的304
     *
     * @return
     */
    @Bean
    public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
        return new ShallowEtagHeaderFilter();
    }

    @Bean
    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = converter.getObjectMapper();
        //反序列化失败
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        SimpleModule simpleModule = new SimpleModule();
        //统一bigdecimal转为字符串
        simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        converter.setObjectMapper(objectMapper);
        return converter;
    }
}
