package cd.casic.plugin;

import cn.hutool.core.io.FileUtil;
import lombok.Data;
import org.pf4j.RuntimeMode;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

@Data
@ConfigurationProperties(prefix = "cd.casic.ops.plugin")
public class PluginProperties implements InitializingBean {
    /**
     * 是否启用插件系统
     */
    private Boolean enable;
    /**
     * 插件根目录
     */
    private String pluginsRoot;
    /**
     * 运行模式，可以是 development 或者 deployment
     */
    private RuntimeMode runtimeMode;
    /**
     * 插件接口RESTful API的路径前缀
     */
    private String restPathPrefix;
    /**
     * 是否在RESTful API前缀中包含插件ID，默认为true
     */
    private boolean enablePluginIdAsRestPrefix = true;
    /**
     * 已启用的插件集合
     */
    private Set<String> enabledPlugins = new HashSet<>();
    /**
     * 已禁用的插件集合
     */
    private Set<String> disabledPlugins = new HashSet<>();

    @Override
    public void afterPropertiesSet() {
        Path pluginsRootPath = Paths.get(this.pluginsRoot);
        if (!pluginsRootPath.toFile().exists()) {
            FileUtil.mkdir(pluginsRootPath.toFile());
        }
    }
}
