package com.brframework.commonweb.core;

import com.brframework.commonweb.annotation.SwaggerIgnore;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @Author xu
 * @Date 2018/1/3 0003 下午 3:30
 * Swagger api生成
 */
public class SwaggerContext {

    public static Docket createDocket(String groupName, String packageName, ApiInfo apiInfo, List<Parameter> pars, boolean enable){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.groupName(groupName)
//                .genericModelSubstitutes(DeferredResult.class)
//                .useDefaultResponseMessages(false)
//                .forCodeGeneration(true)
                .globalOperationParameters(pars);
//                .pathMapping("/");// base，最终调用接口后会和paths拼接在一起
        docket.enable(enable);
        ApiSelectorBuilder select = docket.apiInfo(apiInfo).select();

        select.apis(input -> {
            if (RequestHandlerSelectors.basePackage(packageName).apply(input)) {

                if (RequestHandlerSelectors.withClassAnnotation(SwaggerIgnore.class).apply(input) ||
                        RequestHandlerSelectors.withMethodAnnotation(SwaggerIgnore.class).apply(input)) {
                    return false;
                }
                return true;
            }
            return false;
        });


        select.paths(PathSelectors.any());

        return select.build();
    }

}
