package cd.casic.plugin.event;

import cd.casic.plugin.PluginCache;
import cd.casic.plugin.PluginClassLoaderCache;
import cd.casic.plugin.PluginInfo;
import cd.casic.plugin.PluginManager;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class PluginLoadedEvent extends ApplicationEvent {

    private final PluginManager pluginManager;

    private final PluginInfo pluginInfo;

    public PluginLoadedEvent(Object source, PluginInfo pluginInfo) {
        super(source);
        this.pluginManager = (PluginManager) source;
        this.pluginInfo = pluginInfo;

        if (!PluginCache.isExist(pluginInfo.getPluginId())) {
            PluginCache.put(pluginInfo.getPluginId(), pluginInfo);
        }
        if (PluginClassLoaderCache.getPlugin(pluginInfo.getPluginId()) == null) {
            PluginClassLoaderCache.put(pluginInfo.getPluginId(), pluginInfo.getPluginWrapper().getPluginClassLoader());
        }
    }

}
