package com.example.demo.config;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StopWatch;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@EnableSwagger2
@SpringBootConfiguration
public class SwaggerConfig implements WebMvcConfigurer {

    @Value("${server.servlet.context-path}")
    private String serverServletContextPath;

    @Value("${server.port}")
    private String serverPort;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/web_frontend/**").addResourceLocations("classpath:/web_frontend/");
    }

    /**
     * 功能描述：微信小程序API
     */
    @Bean
    public Docket weixinFrontEndAPI() {
        StopWatch watch = new StopWatch();
        watch.start();

        String groupName = "微信小程序";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName) // 定义组
                .apiInfo(apiInfo()) // 配置说明
                .ignoredParameterTypes(HttpServletRequest.class) // 方法参数忽略，例如HttpServletRequest不想展示在Swagger上
                .select() // 选中哪些路径和api会生成Document
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 拦截的包路径
                .paths(PathSelectors.regex("(/wx/.*)")) // 拦截的接口路径
                .build(); // 创建

        watch.stop();
        log.info("Started Swagger [{}] in [{}] ms, url [http://127.0.0.1:{}{}/swagger-ui.html?urls.primaryName={}]", groupName, watch.getTotalTimeMillis(), serverPort, serverServletContextPath, groupName);

        return docket;
    }

    /**
     * 功能描述：管理后台界面接口API
     */
    @Bean
    public Docket adminBackEndAPI() {
        StopWatch watch = new StopWatch();
        watch.start();

        String groupName = "管理后台";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName) // 定义组
                .apiInfo(apiInfo()) // 配置说明
                .ignoredParameterTypes(HttpServletRequest.class) // 方法参数忽略，例如HttpServletRequest不想展示在Swagger上
                .select() // 选中哪些路径和api会生成Document
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 拦截的包路径
                .paths(PathSelectors.regex("(/api/.*)")) // 拦截的接口路径
                .build(); // 创建

        watch.stop();
        log.info("Started Swagger [{}] in [{}] ms, url [http://127.0.0.1:{}{}/swagger-ui.html?urls.primaryName={}]", groupName, watch.getTotalTimeMillis(), serverPort, serverServletContextPath, groupName);

        return docket;
    }

    /**
     * 隐藏UI上的Models模块
     */
    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
//                .deepLinking(true)
//                .displayOperationId(false)
//                .defaultModelsExpandDepth(-1)
//                .defaultModelExpandDepth(0)
//                .defaultModelRendering(ModelRendering.EXAMPLE)
//                .displayRequestDuration(false)
//                .docExpansion(DocExpansion.NONE)
//                .filter(false)
//                .maxDisplayedTags(null)
//                .operationsSorter(OperationsSorter.ALPHA)
//                .showExtensions(false)
//                .tagsSorter(TagsSorter.ALPHA)
//                .validatorUrl(null)
                .build();
    }

    /**
     * 功能描述：SwaggerUI文档的基本信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("")
                .description("")
                .termsOfServiceUrl("")
                .contact(new Contact("", "", ""))
                .version("v0.0.1")
                .build();
    }

}
