package com.xxx.base.springframework.core.web.config;

import com.xxx.base.springframework.config.PathLocaleResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
 * 将MappingJackson2HttpMessageConverter消息转换器排在靠前位置
 *
 * @author : weierming
 * @date : 2020/10/12
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private ResponseHandlerConfigure responseHandlerConfigure;

    @Autowired
    private PathLocaleResolver pathLocaleResolver;

//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        //设置日期格式
////        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(responseHandlerConfigure.getDateFormat());
////        objectMapper.setDateFormat(simpleDateFormat);
//
//        // null值是否返回,默认为不返回

    /// /        if (!responseHandlerConfigure.getIsWriteMapNullValue()) {
    /// /            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    /// /        }
//
//        SimpleModule module = new SimpleModule();
//        module.addDeserializer(String.class, new StringWithoutSpaceDeserializer(String.class));
//        objectMapper.registerModule(module);
//        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//
//        // 在反序列化时忽略在 json 中存在但 Java 对象不存在的属性
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        mappingJackson2HttpMessageConverter.setDefaultCharset(Charsets.UTF_8);
//        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON));
//        converters.add(0, mappingJackson2HttpMessageConverter);
//    }
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").
                        allowedOriginPatterns("*"). //允许跨域的域名，可以用*表示允许任何域名使用
                        allowedMethods("GET", "POST"). //允许任何方法（post、get等）
                        allowedHeaders("*"). //允许任何请求头
                        allowCredentials(true). //带上cookie信息
                        exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内，不需要再发送预检验请求，可以缓存该结果
            }
        };
    }

    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        return pathLocaleResolver;
    }
}