package com.qmth.jkserver.core.config;

import com.google.common.base.Predicates;
import com.qmth.jkserver.constant.SystemConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

/**
 * @Description: swagger配置类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2019/9/21
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
//        ParameterBuilder tokenPar = new ParameterBuilder();
//        ParameterBuilder tokenPar1 = new ParameterBuilder();
//        ParameterBuilder tokenPar3 = new ParameterBuilder();
//        ParameterBuilder tokenPar4 = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
//        tokenPar.name("authorization-token").description("鉴权token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        tokenPar1.name(SystemConstant.HEADER_DEVICE_ID).description("设备id").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        tokenPar3.name(SystemConstant.HEADER_TIME).description("时间戳").modelRef(new ModelRef("long")).parameterType("header").required(false).build();
//        tokenPar4.name(SystemConstant.MD5.toLowerCase()).description("md5").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        pars.add(tokenPar.build());
//        pars.add(tokenPar1.build());
//        pars.add(tokenPar3.build());
//        pars.add(tokenPar4.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("jk-server")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qmth.jkserver"))
//                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                // 不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "机考数据中心平台 - api",
                "Some custom description of API.",
                "API TOS",
                "Terms of service",
                "myeaddress@company.com",
                "License of API",
                "API license URL");
        return apiInfo;
    }
}