package net.northking.cctp.element.core.support;

import net.northking.cctp.element.log.Level;
import net.northking.cctp.element.log.Logger;
import net.northking.cctp.element.log.Slf4jLogger;
import org.slf4j.LoggerFactory;

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

/**
 * 引擎执行时用例时每个线程都会按照需求创建自己独占的库实例
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class ThreadLevelCache {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(ThreadLevelCache.class);
    private final static ThreadLocal<Map<Class, Object>> cache = new InheritableThreadLocal<>();

    public static Logger getLogger() {
        return new Logger() {
            @Override
            public void info(String message) {
                threadLogger().info(message);
            }

            @Override
            public void info(String format, Object... arguments) {
                threadLogger().info(format, arguments);
            }

            @Override
            public void error(String message) {
                threadLogger().error(message);
            }

            @Override
            public void error(String format, Object... arguments) {
                threadLogger().error(format, arguments);
            }

            @Override
            public void debug(String message) {
                threadLogger().debug(message);
            }

            @Override
            public void debug(String format, Object... arguments) {
                threadLogger().debug(format, arguments);
            }

            @Override
            public void warn(String message) {
                threadLogger().warn(message);
            }

            @Override
            public void warn(String format, Object... arguments) {
                threadLogger().warn(format, arguments);
            }

            @Override
            public void log(Level level, String format, Object... arguments) {
                threadLogger().log(level, format, arguments);
            }

        };
    }

    /**
     * 移除线程缓存
     */
    public static void remove() {
        logger.debug("移除线程缓存的功能对象");
        cache.remove();
    }

    /**
     * 缓存对象
     *
     * @param clazz
     * @param object
     */
    public static void cache(Class clazz, Object object) {
        Map<Class, Object> variableMap = cache.get();
        if (variableMap == null) {
            variableMap = new HashMap<>();
            cache.set(variableMap);
        }
        if (!variableMap.containsKey(clazz)) {
            variableMap.put(clazz, object);
        }
    }

    /**
     * 获取缓存对象
     *
     * @param clazz
     * @return
     */
    public static Object get(Class clazz) {
        Map<Class, Object> variableMap = cache.get();
        return variableMap == null ? null : variableMap.get(clazz);
    }

    public static void setLogger(Logger logger) {
        cache(Logger.class, logger);
    }

    /**
     * 动态获取每个线程日志对象
     *
     * @return
     */
    private static Logger threadLogger() {
        Object result = get(Logger.class);
        if (result == null) {
            return new Slf4jLogger();
        }
        return (Logger) result;
    }

}