package cd.casic.plugin.core;

import org.pf4j.DevelopmentPluginRepository;
import org.springframework.util.CollectionUtils;

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

/**
 * @description: 设置一个固定的插件路径
 * @author: mianbin
 * @date: 2025/5/13 10:47
 * @version: 1.0
 */
public class DefaultDevelopmentPluginRepository extends DevelopmentPluginRepository {
    private final List<Path> fixedPaths = new ArrayList<>();

    public DefaultDevelopmentPluginRepository(Path... pluginsRoots) {
        super(pluginsRoots);
    }

    public DefaultDevelopmentPluginRepository(List<Path> pluginsRoots) {
        super(pluginsRoots);
    }

    public void setFixedPaths(List<Path> paths) {
        if (CollectionUtils.isEmpty(paths)) {
            return;
        }
        fixedPaths.clear();
        fixedPaths.addAll(paths);
    }

    @Override
    public List<Path> getPluginPaths() {
        List<Path> paths = new ArrayList<>(fixedPaths);
        paths.addAll(super.getPluginPaths());
        return paths;
    }

    @Override
    public boolean deletePluginPath(Path pluginPath) {
        return fixedPaths.remove(pluginPath);
    }
}

