package com.loan.system.config;

import com.loan.system.interceptor.JwtTokenUserInterceptor;
import com.loan.system.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration//配置文件类会在程序运行时自动加载
@Slf4j
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

//    @Autowired
//    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
//
    /*
     * 注册自定义拦截器
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
//        registry.addInterceptor(jwtTokenAdminInterceptor)
//                .addPathPatterns("/admin/**")
//                .excludePathPatterns("/admin/employee/login");

        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/wechat/**","/User/**")
                .excludePathPatterns("/wechat/login");
    }

    /*
     * 通过knife4j生成接口文档
     * 1.导入 knife4j 的依赖
     * 2.在配置类中加入 knife4j 相关配置
     * 3.设置静态资源映射，否则接口文档页面无法访问
     * @return
     */
//    @Bean
//    public Docket docket1() {
//        log.info("准备生成接口文件");
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .title("苍穹外卖项目接口文档")
//                .version("2.0")
//                .description("苍穹外卖项目接口文档")
//                .build();
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .groupName("管理端接口")
//                .apiInfo(apiInfo)
//                .select()
//                //最重要：确定要扫描的包
//                .apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin"))
//                .paths(PathSelectors.any())
//                .build();
//        return docket;
//    }

    @Bean
    public Docket docket2() {
        log.info("准备生成接口文件");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("典当项目接口文档")
                .version("2.0")
                .description("典当项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("小程序端端接口")
                .apiInfo(apiInfo)
                .select()
                //最重要：确定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.loan.system.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /*
     * 设置静态资源映射：显示/doc.html接口文档
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("设置静态资源映射");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/files/**").addResourceLocations("classpath:/META-INF/resources/file_store/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /*
    扩展SpringMVC框架的消息转化器
     */
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        //创建一个消息转化器
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
        //需要为消息转化器设置一个对象转换器，对象转换器可以将java对象序列化为json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        //将自己的消息转换器加入容器中--排在容器中的最后,故使用插入使它排在第一个
        converters.add(0,converter);
    }
}
