package com.handong.smartservice.component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RcTree_old {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static List<Map<String, Object>> convertDb2RcTree(List<Map<String, Object>> listTreeDb) {
        RcTree_old rcTree = new RcTree_old();
        return rcTree.convert(listTreeDb);
    }

    public Map<String, Object> searchTreeNode(List<Map<String, Object>> listRcTree, String key) {
        Map<String, Object> node = null;
        
        for(int i = 0; i < listRcTree.size(); i++) {
            node = listRcTree.get(i);
            if (key.compareTo((String)node.get("key")) == 0) {
                return node;
            }

            Object children = node.get("children");
            if (children != null) {
                Map<String, Object> parentNode = searchTreeNode((List<Map<String, Object>>)children, key);
                if (parentNode != null)
                    return parentNode;
            }
        }
        
        return null;
    }
    
    public List<Map<String, Object>> convert(List<Map<String, Object>> listTreeDb) {
        List<Map<String, Object>> listRcTree = new ArrayList<Map<String, Object>>();
        listTreeDb.forEach(item -> {
            Map<String, Object> newNode = new LinkedHashMap<String, Object>();

            String biz_group_id = item.get("biz_group_id").toString();
            newNode.put("key", biz_group_id);
            logger.debug("key(biz_group_id): " + biz_group_id);

            String name = (String)item.get("name");
            newNode.put("name", name);

            String biz_reg_num = (String)item.get("biz_reg_num");
            newNode.put("biz_reg_num", biz_reg_num);            
            
            newNode.put("title", name + "(" + biz_reg_num + ")");
            logger.debug("title(name): " + name);

            Timestamp reg_time = (Timestamp)item.get("reg_time");
            if (reg_time != null)
                newNode.put("reg_time", reg_time);

            String email = (String)item.get("email");
            if (email != null)
                newNode.put("email", email);
            
            String phone = (String)item.get("phone");
            if (phone != null)
                newNode.put("phone", phone);
            
            String ceo = (String)item.get("ceo");
            if (ceo != null)
                newNode.put("ceo", ceo);

            String address = (String)item.get("address");
            if (address != null)
                newNode.put("address", address);

            newNode.put("isLeaf", false);
            
            if (item.get("pid") == null || listRcTree.size() == 0) {
                listRcTree.add(newNode);
            }
            else {
                // search parent node
                final String pid = item.get("pid") != null ? item.get("pid").toString() : "";
                logger.debug("pid = " + pid);

                Map<String, Object> parentNode = searchTreeNode(listRcTree, pid);
                logger.debug("convert, parentNode = " + parentNode);

                if (parentNode != null) {
                    List<Map<String, Object>> children = null;
                    Object childrenNode = parentNode.get("children");
                    if (childrenNode == null) {
                        children = new ArrayList<Map<String, Object>>();
                    }
                    else {
                        children = (List<Map<String, Object>>)childrenNode;
                    }
                    children.add(newNode);
                    parentNode.put("children", children);
                }
            }
        });
        
        return listRcTree;
    }
}
