package com.casic.missiles.config.swagger;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import lombok.Getter;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;

/**
 * @Description: swagger分组
 * @Author: wangpeng
 * @Date: 2023/3/7 10:23
 */

@Getter
public enum SwaggerEntity {
    /** 财务产值模块 */
    FINANCE("财务产值模块",
            basePackage("com.casic.missiles.controller.finance"),
            PathSelectors.any()),

    /** 业务管理模块 */
    BUSINESS("业务管理模块",
            basePackage("com.casic.missiles.controller.business"),
            PathSelectors.any()),
//            PathSelectors.ant("/business/**")),

    /** 客户资源模块 */
    CUSTOMER("客户资源模块",
            basePackage("com.casic.missiles.controller.customer"),
            PathSelectors.any()),

    /** 测量设备模块 */
    EQUIPMENT("测量设备模块",
           basePackage("com.casic.missiles.controller.equipment"),
            PathSelectors.any()),

    /** 审批操作按钮 */
    APPROVAL("审批操作按钮",
            basePackage("com.casic.missiles.controller.flowable"),
            PathSelectors.any()),

    /** 计量管理模块 */
    METER("计量管理模块",
            basePackage("com.casic.missiles.controller.meter"),
            PathSelectors.any()),

    /** 系统管理模块 */
    SYSTEM("系统管理模块",
            basePackage("com.casic.missiles.controller.sys", "com.casic.missiles.controller.system"),
            PathSelectors.any()),

    /** 总工作台 */
    WORKBENCH("总工作台",
            basePackage("com.casic.missiles.controller.workbench"),
            PathSelectors.any()),

    /** minio文件存储 */
    MINIO("minio文件存储",
            basePackage("com.casic.missiles.controller"),
            PathSelectors.ant("/minio/**")),

    /** 计量管理驾驶舱 */
    COCKPIT("计量管理驾驶舱",
            basePackage("com.casic.missiles.controller.cockpit"),
            PathSelectors.any()),

    /** 全部 */
    ALL("全部",
            basePackage("com.casic.missiles.controller"),
            PathSelectors.any()),

    ;

    SwaggerEntity(String groupName, Predicate<RequestHandler> apis, Predicate<String> paths) {
        this.groupName = groupName;
        this.apis = apis;
        this.paths = paths;
    }

    /** 分组名称 */
    private final String groupName;
    /** 扫描包 */
    private final Predicate<RequestHandler> apis;
    /** 扫描包 */
    private final Predicate<String> paths;

    private static Predicate<RequestHandler> basePackage(String... basePackages) {
        return input -> declaringClass(input).transform(handlerPackage(basePackages)).or(true);
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

    private static Function<Class<?>, Boolean> handlerPackage(String[] basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

}
