package com.elab.spring.config;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * Swagger配置
 *
 * @author Liukx
 * @create 2018-04-09 11:07
 * @email liukx@elab-plus.com
 **/
@Order(Integer.MAX_VALUE)
@EnableSwagger2
@Configuration
public class SwaggerConfigBean {

    @Autowired
    private Environment environment;

    private static String defaultDelimiter = ",";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 具体需要扫描的包 RequestHandlerSelectors
                .apis(baseMultiPackage(environment.getProperty("swagger2.basePackage")))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(environment.getProperty("swagger2.title"))
                .description(environment.getProperty("swagger2.description"))
                .termsOfServiceUrl(environment.getProperty("swagger2.termsOfServiceUrl"))
                .version(environment.getProperty("swagger2.licenseUrl"))
                .licenseUrl(environment.getProperty("swagger2.version"))
                .build();
    }

    /**
     * 处理多个包的扫描情况
     *
     * @param basePackage
     * @return
     */
    public static Predicate<RequestHandler> baseMultiPackage(final String basePackage) {
        return new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                return Optional.fromNullable(input.declaringClass()).transform(handlerMultiPackage(basePackage)).or(true);
            }
        };
    }

    /**
     * 匹配多个
     *
     * @param basePackage
     * @return
     */
    private static Function<Class<?>, Boolean> handlerMultiPackage(final String basePackage) {
        return new Function<Class<?>, Boolean>() {
            @Override
            public Boolean apply(Class<?> input) {

                if (basePackage.indexOf(defaultDelimiter) > -1) {
                    String packageName = ClassUtils.getPackageName(input);
                    List<String> packageList = Splitter.on(defaultDelimiter).splitToList(basePackage);
                    for (int i = 0; i < packageList.size(); i++) {
                        String packages = packageList.get(i);
                        if (packageName.startsWith(packages)) {
                            return true;
                        }
                    }
                    return false;
                } else {
                    return ClassUtils.getPackageName(input).startsWith(basePackage);
                }
            }
        };
    }
}
