
package com.jeelowcode.core.framework.config.virtualization;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 虚拟化字段 插件管理器
 */
@Slf4j
public class VirtualizationFieldPluginManager {

    //只保留最近10个常用的
    static Cache<String, VirtualizationFieldPlugin> cache = CacheUtil.newLRUCache(10);

    //初始化实例
    public static VirtualizationFieldPlugin initClazz(String clazzStr) {
        try {
            if(clazzStr.contains(".")){//全路径
                Class<?> clazz = Class.forName(clazzStr);
                VirtualizationFieldPlugin plugin = (VirtualizationFieldPlugin)clazz.newInstance();//实例化
                return plugin;
            }else{
                VirtualizationFieldPlugin plugin = (VirtualizationFieldPlugin)SpringUtils.getBean(clazzStr);
                return plugin;
            }

        } catch (Exception e) {
            throw new JeeLowCodeException(e.getMessage());
        }
    }


    //添加插件
    private static void addPlugin(String key, VirtualizationFieldPlugin plugin) {
        cache.put(key, plugin);
    }


    private static VirtualizationFieldPlugin getPlugin(String key) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        }

        //没有在队列里面，则重新实例化
        addPlugin(key, initClazz(key));
        return cache.get(key);
    }

    //运行插件
    public static String executePlugin(String key,Map<String,Object> dataMap) {
        VirtualizationFieldPlugin plugin = getPlugin(key);
        if(Func.isEmpty(plugin)){
            return null;
        }
        return plugin.execute(dataMap);
    }


}
