
package com.jeelowcode.core.framework.config.btncommand.definable;

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

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

@Slf4j
public class DefinablePluginManager<T> {

    //从数据库读取，放入到map
     public static Map<String,String> allDefinableMap=new HashMap<>();


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

    //初始化实例
    public static DefinableButtonPlugin initClazz(String clazzStr) {
        try {
            Class<?> clazz = Class.forName(clazzStr);
            DefinableButtonPlugin definableButtonPlugin = (DefinableButtonPlugin)clazz.newInstance();//实例化
            return definableButtonPlugin;
        } catch (Exception e) {
            throw new JeeLowCodeException(e.getMessage());
        }
    }


    //添加插件
    public void addPlugin(String key, DefinableButtonPlugin plugin) {
        cache.put(key, plugin);
    }

    //移除
    public void remove(String key) {
        if (cache.containsKey(key)) {
            cache.remove(key);
        }
    }

    public DefinableButtonPlugin getPlugin(String key) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        }
        if(!allDefinableMap.containsKey(key)){
            log.error("自定义按钮控件不存在==》"+key);
            return null;
        }

        //没有在队列里面，则重新实例化
        String clazzUrl = allDefinableMap.get(key);

        addPlugin(key, initClazz(clazzUrl));
        return cache.get(key);
    }

    //运行插件
    public Object executePlugin(String key,T param) {
        DefinableButtonPlugin plugin = this.getPlugin(key);
        if(Func.isEmpty(plugin)){
            return null;
        }
        return plugin.execute(param);
    }


}
