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

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.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());
    }

    /**
     * 添加插件
     * @param key
     * @param plugin
     */
    public static void addPlugin(String key, BaseAdvicePlugin plugin){
        plugins.put(key,plugin);
    }

    /**
     * 添加插件
     * @param plugin
     */
    public static void addPlugin(BaseAdvicePlugin plugin){
        plugins.put(plugin.getClass().getName(),  plugin);
    }

    //移除插件
    public static boolean removePlugin(String name){
        plugins.remove(name);
        return true;
    }

    /**
     * 运行插件
     * @param name
     * @param enhanceContext
     */
    public static void executePlugin(String name, EnhanceContext enhanceContext){
        BaseAdvicePlugin plugin = plugins.get(name);
        plugin.execute(enhanceContext);
    }

    /**
     * 运行插件
     * @param plugin
     * @param enhanceContext
     */
    public static void executePlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        plugin.execute(enhanceContext);
    }

    /**
     * 运行前置环绕插件
     * @param plugin
     * @param enhanceContext
     */
    public static void executeAroundBeforePlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        AroundAdvicePlugin aroundPlugin = (AroundAdvicePlugin) plugin;
        aroundPlugin.beforeExecute(enhanceContext);
    }

    /**
     * 运行环绕后置增强
     * @param plugin
     * @param enhanceContext
     */
    public static void executeAroundAfterPlugin(BaseAdvicePlugin plugin, EnhanceContext enhanceContext){
        AroundAdvicePlugin aroundPlugin = (AroundAdvicePlugin) plugin;
        aroundPlugin.afterExecute(enhanceContext);
    }
}