package xiaoqu.home.open.config;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

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.EnableSwagger2;

@Configuration
@EnableSwagger2 // 启用 Swagger
@EnableWebMvc
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
	@Value("${spring.profiles.active}")
	private String springProfilesActive;

	@SuppressWarnings("deprecation")
	@Bean
	public Docket createRestApi() {
		// 添加head参数start
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("uuid").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
		pars.add(tokenPar.build());
		
		Docket docket = null;
		// 可以根据配置决定不做任何API生成
		if (!"prd".equals(springProfilesActive)) {
			docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
					// 标示只有被 @Api 标注的才能生成API.
					.apis(RequestHandlerSelectors.basePackage("xiaoqu.home.open.controller")).paths(PathSelectors.any()).build().globalOperationParameters(pars);
		} else {
			docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfo("", "", "", "", "", "", "")).select().apis(RequestHandlerSelectors.none()).build();

		}
		return docket;
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("智慧化保洁后台接口文档与测试")// 大标题
				.description("这是一个给前端人员调用server端接口的测试文档与平台").version("1.0.0")// 版本
				.build();
	}
}