package com.yfd.platform.config;

import io.swagger.models.auth.In;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

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

/**
 * swagger配置
 */
@Configuration
public class SwaggerConfig {

    Boolean swaggerEnabled = true;

    @Bean
    public Docket createRestGBApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
//                .globalRequestParameters(generateRequestParameters())
                .groupName("1. 系统管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yfd.platform.system.controller"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .enable(swaggerEnabled);
    }

    @Bean
    public Docket createRestStorageApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
//                .globalRequestParameters(generateRequestParameters())
                .groupName("2. 文件存储管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yfd.platform.modules.storage.controller"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .enable(swaggerEnabled);
    }

    @Bean
    public Docket createSpecialDocumentApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
//                .globalRequestParameters(generateRequestParameters())
                .groupName("3. 专项文档管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yfd.platform.modules.specialDocument.controller"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .enable(swaggerEnabled);
    }


    @Bean
    public Docket createExperimentalDataApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
//                .globalRequestParameters(generateRequestParameters())
                .groupName("4. 试验数据管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yfd.platform.modules.experimentalData.controller"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .enable(swaggerEnabled);
    }

    /**
     * 获取通用的全局参数
     *
     * @return  全局参数列表
     */
    private List<RequestParameter> generateRequestParameters(){
        RequestParameterBuilder token = new RequestParameterBuilder();
        List<RequestParameter> parameters = new ArrayList<>();
        token.name("token").description("token").in(In.HEADER.toValue()).required(true).build();
        parameters.add(token.build());
        return parameters;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目API 接口文档")
                .description("")
                .contact(new Contact("郑顺利", "郑顺利", "13910913995@163.com"))
                .version("3.0")
                .build();
    }
}
