package com.zhgd.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * @program: Iot
 * @description: Swagger配置类
 * @author: Mr.Wang
 * @create: 2019-04-16 14:46
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    // 定义分隔符
    private static final String splitor = ";";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("A公共性接口文档")
                .apiInfo(apiInfo("公共性接口文档API","公共性接口前后端联调api文档","1.0"))
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.zhgd.xmgl"))
                .apis(basePackage("com.zhgd.xmgl.modules.basicdata.controller.admin"))
                //.apis(basePackage("com.zhgd.xmgl"+splitor+"com.zhgd.xmgl.device"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.regex("/xmgl.*"))
                .build();
    }

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("政务端个性化接口文档")
                .apiInfo(apiInfo("政务端个性化接口文档","政务端个性化前后端联调api文档","1.0"))
                .select()
                .apis(basePackage("com.zhgd.xmgl.modules.basicdata.controller.government"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("企业端个性化接口文档")
                .apiInfo(apiInfo("企业端个性化接口文档","企业端个性化前后端联调api文档","1.0"))
                .select()
                .apis(basePackage("com.zhgd.xmgl.modules.basicdata.controller.enterprise"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("项目端个性化接口文档")
                .apiInfo(apiInfo("项目端个性化接口文档","项目端个性化前后端联调api文档","1.0"))
                .select()
                .apis(basePackage("com.zhgd.xmgl.modules.basicdata.controller.project"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo(String titleName,String description,String version) {
        return new ApiInfoBuilder()
                .title(titleName)
                .description(description)
                .version(version)
                .build();
    }

    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }
    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }
    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}
