package cn.pluss.platform.configurer;

import cn.pluss.platform.configurer.extend.CurrentUserMethodArgumentResolver;
import cn.pluss.platform.configurer.extend.FastJsonHttpMessageConverterEx;
import cn.pluss.platform.interceptor.RequestInfoInterceptor;
import cn.pluss.platform.interceptor.TokenRegistryInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

/**
 * Spring MVC 配置
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {


	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(currentUserMethodArgumentResolver());
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//和页面有关的静态目录都放在项目的static目录下
//		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		//上传的图片在D盘下的OTA目录下，访问路径如：http://localhost:8081/OTA/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
		//其中OTA表示访问的前缀。"file:D:/OTA/"是文件真实的存储路径
		registry.addResourceHandler("/Pictures/**").addResourceLocations("file:D:/Pictures/");
	}

	/**
	 * 使用阿里 FastJson 作为JSON MessageConverter
	 * @param converters
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.clear();
		converters.add(fastJsonHttpMessageConverterEx());
	}

	/**
	 * 解决跨域问题
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedMethods("*").allowedOrigins("*").allowedHeaders("*");
	}

	// 添加拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 接口签名认证拦截器，该签名认证比较简单，实际项目中可以使用Json Web Token或其他更好的方式替代。
		List<String> excludes = new ArrayList<>();
		excludes.add("/webjars/**");
		excludes.add("/doc.html");
		excludes.add("/swagger-resources");
		excludes.add("/auditCallback/**");
		excludes.add("/wxCertCallback/**");
		excludes.add("/Pictures/**");
		excludes.add("/druid/**");
		excludes.add("/callback/**");
		registry.addInterceptor(new RequestInfoInterceptor());

		registry.addInterceptor(tokenRegistryInterceptor()).excludePathPatterns(excludes);
	}

	@Bean
	public TokenRegistryInterceptor tokenRegistryInterceptor() {
		return new TokenRegistryInterceptor();
	}

	@Bean
	public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
		return new CurrentUserMethodArgumentResolver();
	}

	@Bean
	public FastJsonHttpMessageConverterEx fastJsonHttpMessageConverterEx() {
		return new FastJsonHttpMessageConverterEx();
	}
}