package com.muchinfo.mtp3century.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.core.env.Environment;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(Environment environment) {
        // 配置 default/dev/test 环境可用，其他环境不可用
        Profiles profile = Profiles.of("default", "dev");
        boolean flag = environment.acceptsProfiles(profile);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)                             // 是否启用
                .groupName("muchinfo")                       // 组别，区分接口提供者，默认default
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.muchinfo.mtp3century.controller"))   // 根据包名扫描
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Century Swagger2")                      // 设置文档的标题
                .description("Century API 接口文档")             // 设置文档的描述
                .version("V1.0")                           // 设置文档的版本信息
                .termsOfServiceUrl("http://www.baidu.com") // 设置文档的License信息->1.3 License information
                .build();
    }
}
