package cn.axzo.msg.center.message.service.group;

import lombok.Getter;
import lombok.Setter;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author yanglin
 */
public class TemplateCache {

    private static final ThreadLocal<TemplateCache> LOCAL = new ThreadLocal<>();

    @Setter @Getter
    private RootNodeWrapper root;
    private final Map<Set<String>, GroupTemplates> nodeCodes2Templates = new HashMap<>();

    public static TemplateCache get() {
        return LOCAL.get();
    }

    public static void enableCache() {
        LOCAL.set(new TemplateCache());
    }

    public static void disableCache() {
        LOCAL.remove();
    }

    public GroupTemplates getGroupTemplates(Collection<String> nodeCodes) {
        return nodeCodes2Templates.get(new HashSet<>(nodeCodes));
    }

    public void putGroupTemplates(Collection<String> nodeCodes, GroupTemplates templates) {
        nodeCodes2Templates.put(new HashSet<>(nodeCodes), templates);
    }
}