package cd.casic.plugin.config;

import cd.casic.plugin.utils.PathUtils;
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 org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 插件Properties
 * @author: mianbin
 * @date: 2025/5/12 16:06
 * @version: 1.0
 */
@Data
@EnableConfigurationProperties(PluginProperties.class)
@ConfigurationProperties(prefix = "ops.plugin")
public class PluginProperties implements InitializingBean {

    public static final String GRADLE_LIBS_DIR = "build/libs";

    /**
     * Auto start plugin when main app is ready.
     */
    private boolean enable = true;

    /**
     * 默认插件路径是通过文件扫描获取的。在开发模式下，你可以指定插件路径作为项目目录。
     */
    private String pluginRoot;

    /**
     * 默认情况下禁用的插件.
     */
    private String[] disabledPlugins = new String[0];

    /**
     * 默认情况需要启动的插件，要在`disabledPlugins`之前.
     */
    private String[] enabledPlugins = new String[0];

    /**
     * 插件的类目录
     */
    private List<String> classesDirectories = new ArrayList<>();
    /**
     * 插件接口统一前缀
     */
    private String restPathPrefix;
    /**
     * 插件restful接口前缀中是否需要包含插件id，默认包含
     */
    private boolean enablePluginIdAsRestPrefix = true;
    /**
     * 插件jar目录.
     */
    private List<String> libDirectories = new ArrayList<>(List.of(GRADLE_LIBS_DIR));

    /**
     * 运行状态
     */
    private RuntimeMode runtimeMode = RuntimeMode.DEPLOYMENT;

    @Override
    public void afterPropertiesSet() throws Exception {
        Path pluginsRootPath = Paths.get(pluginRoot);
        if (!PathUtils.exists(pluginsRootPath, false)) {
            FileUtil.mkdir(pluginsRootPath.toFile());
        }
    }
}
