package com.zksy.web.core.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
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;

/**
 * Swagger2的接口配置
 *
 * @author ruoyi
 */
@EnableSwagger2
@Configuration
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    /**
     * 系统基础配置
     */
    @Autowired
    private com.zksy.common.config.zksyConfig zksyConfig;

    /**
     * 是否开启swagger
     */
    @Value("${swagger.enabled}")
    private boolean enabled;

    /**
     * 设置请求的统一前缀
     */
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    Predicate<RequestHandler> selector1 = RequestHandlerSelectors.basePackage("com.zksy.web.controller.common");
    Predicate<RequestHandler> selector2 = RequestHandlerSelectors.basePackage("com.zksy.web.controller.system");
    Predicate<RequestHandler> selector3 = RequestHandlerSelectors.basePackage("com.zksy.web.controller.monitor");
    Predicate<RequestHandler> selector4 = RequestHandlerSelectors.basePackage("com.zksy.web.controller.tool");
    Predicate<RequestHandler> selector5 = RequestHandlerSelectors.basePackage("com.zksy.web.controller.basicData");
    Predicate<RequestHandler> selector6 = RequestHandlerSelectors.basePackage("com.zksy.commonFile.controller");
    Predicate<RequestHandler> selector7 = RequestHandlerSelectors.basePackage("com.zksy.generator.controller");

    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder commPar1 = new ParameterBuilder();
        commPar1.name("Authorization").description("auth token").defaultValue("test")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(true).build();
        pars.add(commPar1.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(selector1, selector2, selector3, selector4, selector5, selector6, selector7))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("swagger文档")
                .description("swagger文档desc")
                .termsOfServiceUrl("http://localhost:10086")
                .version("1.0")
                .build();
    }
}
