package cn.com.qmth.examcloud.core.questions.starter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.EnableSwagger2WebMvc;

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

@Configuration
@EnableSwagger2WebMvc
public class Swagger2 {

    @Bean
    public Docket buildDocket() {
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder().name("key").modelRef(new ModelRef("String")).parameterType("header").required(false).build());
        parameters.add(new ParameterBuilder().name("token").modelRef(new ModelRef("String")).parameterType("header").required(false).build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("default")
                .apiInfo(buildApiInfo())
                .globalOperationParameters(parameters)
                .useDefaultResponseMessages(false)
                .select()
                // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("cn.com.qmth.examcloud.core.questions.api.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                .title("考试云平台接口文档")
                .version("v4.x.x")
                .build();
    }

}
