
package com.jeelowcode.core.framework.config.aspect.enhance.plugin;

import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.jeelowcode.core.framework.entity.EnhanceJavaEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JX
 * @create 2024-08-12 9:38
 * @dedescription:
 */
public class PluginManager{

    public static Map<String, BaseAdvicePlugin> plugins = new HashMap<>();


    public static BaseAdvicePlugin getPlugin(String key){
        return plugins.get(key);
    }

    public static BaseAdvicePlugin getPlugin(EnhanceJavaEntity entity){
        return plugins.get(entity.getJavaClassUrl());
    }


    public static void addPlugin(String key, BaseAdvicePlugin plugin){
        plugins.put(key,plugin);
    }

    public static void addPlugin(BaseAdvicePlugin plugin){
        plugins.put(plugin.getClass().getName(),  plugin);
    }

    public static boolean removePlugin(String name){
        plugins.remove(name);
        return true;
    }

    public static void executePlugin(String name, EnhanceContext enhanceContext){
        BaseAdvicePlugin plugin = plugins.get(name);
        plugin.execute(enhanceContext);
    }

    public static void executePlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        plugin.execute(enhanceContext);
    }

    public static void executeAroundBeforePlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        AroundAdvicePlugin aroundPlugin = (AroundAdvicePlugin) plugin;
        aroundPlugin.beforeExecute(enhanceContext);
    }
    public static void executeAroundAfterPlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        AroundAdvicePlugin aroundPlugin = (AroundAdvicePlugin) plugin;
        aroundPlugin.afterExecute(enhanceContext);
    }
}