package org.diagbot.bigdata.common;

import org.diagbot.bigdata.util.BigDataConstants;
import org.diagbot.nlp.util.NlpCache;

import javax.servlet.ServletContext;
import java.util.*;

public class ApplicationCacheUtil {

    public void putStandardInfoContext(ServletContext servletContext) {
        Map<String, Object> map = new HashMap<>(10, 0.8f);
        //同义词缓存
        servletContext.setAttribute(BigDataConstants.standard_info_synonym_cache, NlpCache.getStandard_info_synonym_map());
        //分类关系
        servletContext.setAttribute(BigDataConstants.standard_info_type_cache, NlpCache.getStandard_info_classify_map());
        //分类关系树形结构存储    暂时只支持三级结构
        Map<String, String> standardInfoTypeMap = (Map<String, String>) servletContext.getAttribute(BigDataConstants.standard_info_type_cache);

        Map<String, Node> nodeMap = new HashMap<>();
        for (Map.Entry<String, String> entry : standardInfoTypeMap.entrySet()) {
            Node parent_node = nodeMap.get(entry.getValue());
            if (parent_node == null) {
                parent_node = new Node();
                parent_node.setName(entry.getValue());
            }
            Node child_node = nodeMap.get(entry.getKey());
            if (child_node == null) {
                child_node = new Node();
                child_node.setName(entry.getKey());
            }
            parent_node.add(parent_node, child_node);
            nodeMap.put(entry.getValue(), parent_node);
            nodeMap.put(entry.getKey(), child_node);
        }
        servletContext.setAttribute(BigDataConstants.standard_info_type_tree_cache, nodeMap);

    }

    private void addChilds(Node root, Map<String, String> standardInfoTypeMap, List<String> hasAddList) {
        for (Node node : root.getChilds()) {
            for (Map.Entry<String, String> entry : standardInfoTypeMap.entrySet()) {
                if (node.getName().equals(entry.getValue())) {
                    node.add(node, entry.getKey());
                    hasAddList.add(entry.getKey());
                }
            }
        }
    }

    public class Node {
        private String name;
        private Node parent;
        private Set<Node> childs = new HashSet<>();

        public Node add(Node parent, String name) {
            Node node = new Node();
            node.setName(name);
            node.setParent(parent);
            parent.getChilds().add(node);
            return parent;
        }

        public Node add(Node parent, Node node) {
            node.setParent(parent);
            parent.getChilds().add(node);
            return parent;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Set<Node> getChilds() {
            return childs;
        }

        public void setChilds(Set<Node> childs) {
            this.childs = childs;
        }

        public Node getParent() {
            return parent;
        }

        public void setParent(Node parent) {
            this.parent = parent;
        }
    }
}
