package com.yonge.cooleshow.teacher.config;

import java.util.ArrayList;
import java.util.List;

import com.yonge.cooleshow.biz.dal.config.LocalFastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.yonge.cooleshow.common.config.EnumConverterFactory;
import com.yonge.cooleshow.teacher.interceptor.MDCInterceptor;
import com.yonge.cooleshow.teacher.interceptor.OperationLogInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Autowired
	private MDCInterceptor mdcInterceptor;

	@Autowired
	private OperationLogInterceptor operationLogInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		List<String> includePathPatterns = new ArrayList<String>();
		includePathPatterns.add("/**");
		List<String> excludePathPatterns = new ArrayList<String>();
		excludePathPatterns.add("/login");
		registry.addInterceptor(mdcInterceptor).addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);

//		registry.addInterceptor(operationLogInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
	}

	/**
	 * 枚举类的转换器 addConverterFactory
	 */
	@Override
	public void addFormatters(FormatterRegistry registry) {
		registry.addConverterFactory(new EnumConverterFactory());
	}
	
	@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
		LocalFastJsonHttpMessageConverter converter = new LocalFastJsonHttpMessageConverter();
        List<MediaType> fastMediaTypes =  new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        return new HttpMessageConverters(converter);
    }

}
