package cd.casic.plugin;

import cd.casic.plugin.core.PluginContext;
import cd.casic.plugin.dataobject.pojo.SpringPlugin;
import cd.casic.plugin.function.PluginGetter;
import lombok.RequiredArgsConstructor;
import org.pf4j.Plugin;
import org.pf4j.PluginFactory;
import org.pf4j.PluginWrapper;

/**
 * @description: TODO
 * @author: mianbin
 * @date: 2025/5/12 15:16
 * @version: 1.0
 */
@RequiredArgsConstructor
public class SpringPluginFactory implements PluginFactory {

    private final PluginApplicationContextFactory contextFactory;
    private final PluginGetter pluginGetter;

    @Override
    public Plugin create(PluginWrapper pluginWrapper) {
        var plugin = pluginGetter.getPlugin(pluginWrapper.getPluginId());
        var pluginContext = PluginContext.builder()
                .name(pluginWrapper.getPluginId())
                .configMapName(plugin.getClass().getName())
                .version(pluginWrapper.getDescriptor().getVersion())
                .runtimeMode(pluginWrapper.getRuntimeMode())
                .build();
        return new SpringPlugin(contextFactory, pluginContext);
    }
}
