package com.qmth.exam.reserve.config;

import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

@EnableSwagger2WebMvc
@Configuration
public class SwaggerConfig {

    private static final Logger log = LoggerFactory.getLogger(SwaggerConfig.class);

    @Bean
    public Docket buildDocket() {
        log.info("swagger init...");

        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder().name("authorization").modelRef(new ModelRef("String")).parameterType("header").required(false).build());
        parameters.add(new ParameterBuilder().name("time").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))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .version("v1.0.0")
                .build();
    }

}
