package com.chinaweal.aiccs.config;

import org.springframework.beans.factory.annotation.Value;
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.Contact;
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;

/**
 * @author itluck
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerKnife4j {
    @Value("${applicationName}")
    private String applicationName;
    @Value("${version}")
    private String version;
    @Value("${description}")
    private String description;
    @Value("${license}")
    private String license;

    @Bean("fraudApi")
    public Docket fraudApi() {
        //添加head参数start
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .groupName("3.涉嫌冒用登记")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chinaweal.aiccs.aiccs.fraud"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("marketCoordination")
    public Docket standardizingApi() {
        //添加head参数start
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .groupName("2.市场所协同")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chinaweal.aiccs.aiccs.market"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("basisApi")
    public Docket basisApi() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> headers = new ArrayList<>();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        headers.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .groupName("1.basisApi")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chinaweal.aiccs"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(headers);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description(description)
                .termsOfServiceUrl("https://www.chinaweal.com.cn")
                .version(version)
                .contact(new Contact("chinaweal", "https://www.chinaweal.com.cn", ""))
                .license(license)
                .licenseUrl("https://www.chinaweal.com.cn")
                .build();
    }
}
