package com.persagy.adm.diagram.config;

import com.google.common.base.Predicate;
import com.spring4all.swagger.EnableSwagger2Doc;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Nullable;

@Configuration
@EnableSwagger2Doc
public class SwaggerConfig {

//	private static final List<String> excludedPathPrefix = Arrays.asList(
//			"/",
//			"/legend",
//			"/diagram"
//	);

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(new Swaggerfilter())
//				.paths((s) -> {
//					for(String pathPrefix : excludedPathPrefix) {
//						if(StringUtils.startsWith(s, pathPrefix)) {
//							return false;
//						}
//					}
//					return true;
//				})
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("系统图接口")
				.version("0.1")
				.build();
	}


	/**
	 * 过滤页面路由
	 */
	class Swaggerfilter implements Predicate<RequestHandler> {
		@Override
		public boolean apply(@Nullable RequestHandler requestHandler) {
			if(requestHandler.findAnnotation(ApiOperation.class).isPresent()){
				return true;
			}
//       		if(requestHandler.findAnnotation(ResponseBody.class).isPresent()|| requestHandler.findControllerAnnotation(ResponseBody.class).isPresent()){
//                return true;
//            }
			return false;
		}
	}

}
