package com.nokia.config.web;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    @Value("${redis.timeoutSeconds:600}")
    private Integer timeoutSeconds;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加请求日志拦截
        registry.addInterceptor(new RequestLogHandlerInterceptor()).addPathPatterns("/**");
        // 添加web登录拦截
        registry.addInterceptor(new WebLoginInterceptor(timeoutSeconds)).addPathPatterns("/api/web/**");
    }

    /**
     * 使用自定义DispatcherServlet
     */
    @Bean
    @Qualifier(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet() {
        return new MyDispatcherServlet();
    }

//    /**
//     * 配置消息转换器
//     *
//     * @param converters 转换器
//     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(mappingJackson2HttpMessageConverter());
//    }
//
//    /**
//     * 配置映射jackson2 http消息转换器
//     *
//     * @return {@link MappingJackson2HttpMessageConverter}
//     */
//    @Bean
//    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//        converter.setObjectMapper(mapper);
//        return converter;
//    }
}