package com.yn.bftl.thirdparty.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

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

/**
 * Swagger配置
 *
 * @author huabiao
 * @create 2020/06/18  15:17
 **/
@Configuration
@Profile({"dev","local","test","lg"})
public class SwaggerConfig {

    @Value("${spring.profiles.active}")
    private String active;

    @Bean
    public Docket customDocket() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2);

        List<Parameter> pars = new ArrayList<>();

        ParameterBuilder customertokenPar = new ParameterBuilder();
        ParameterBuilder usertokenPar = new ParameterBuilder();
        customertokenPar.name("Customer-Token").description("customer token")
                .modelRef(new ModelRef("string"))
                .parameterType("header").defaultValue("")
                .required(false).build();
        usertokenPar.name("User-Token").description("user token")
                .modelRef(new ModelRef("string"))
                .parameterType("header").defaultValue("")
                .required(false).build();
        pars.add(customertokenPar.build());
        pars.add(usertokenPar.build());

        docket=docket.apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yn.bftl"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        if (!active.equals("dev") && !active.equals("local")) {
            docket.pathMapping("/thirdparty");
        }
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("yn-bftl-thirdparty API中心")
                .description("API接口文档")
                .contact(new Contact("yn", "http://yn-ce.com", "xxxx@qq.com"))
                .version("1.0")
                .build();
    }
}
