/**
 * Created by Tony on 2017/1/23.
 */
const NODE_ID = "ID", P_ID = "ParentID", NEXT_ID = "NextSiblingID", ADHOC_PRE_ID="Previous_ID", CHILDREN_NODE = "items", SUB_ID = "sub_ids", EMPTY_ID_VAL = -1;

var tree_Data_Helper = {
    buildTreeNodeDirectly: function(data) {
        var me = this, topArr = [], rst = [], tmpNodes = {}, prefix = "id_";
        private_getTopNode = function (idArr) {
            var rst = null;
            for (var i = 0; i < idArr.length; i++) {
                if (tmpNodes[prefix + idArr[i]][ADHOC_PRE_ID] == EMPTY_ID_VAL) {
                    rst = tmpNodes[prefix + idArr[i]];
                    break;
                }
            }
            return rst;
        }
        private_buildNodeData = function(parentItem, idArr) {
            var iter = [], nextNode = private_getTopNode(idArr);
            while (nextNode != null && nextNode != undefined ) {
                if (parentItem) {
                    parentItem[CHILDREN_NODE].push(nextNode);
                } else {
                    rst.push(nextNode);
                }
                iter.push(nextNode);
                nextNode = tmpNodes[prefix + nextNode[NEXT_ID]];
            }
            for (var i = 0; i < iter.length; i++) {
                private_buildNodeData(iter[i], iter[i][SUB_ID]);
            }
        }

        for (var i = 0; i < data.length; i++) {
            tmpNodes[prefix + data[i][NODE_ID]] = data[i];
            data[i][ADHOC_PRE_ID] = EMPTY_ID_VAL;
            data[i][SUB_ID] = [];
            data[i][CHILDREN_NODE] = [];
            if (data[i][P_ID] == EMPTY_ID_VAL) {
                topArr.push(data[i][NODE_ID]);
            }
        }
        for (var i = 0; i < data.length; i++) {
            if (data[i][NEXT_ID] != EMPTY_ID_VAL) {
                tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = data[i][NODE_ID];
            }
            if (data[i][P_ID] != EMPTY_ID_VAL) {
                tmpNodes[prefix + data[i][P_ID]][SUB_ID].push(data[i][NODE_ID]);
            }
        }
        private_buildNodeData(null, topArr);
        //try to release and return
        tmpNodes = null;
        topArr.length = 0;
        return rst;
    },
    reSettleRelatedID: function(treeData) {
        if (treeData && treeData.length > 0) {
            for (var i = 0; i < treeData[CHILDREN_NODE].length; i++) {
                var nI = null;
                if (i < treeData[CHILDREN_NODE].length - 1) {
                    nI = treeData[CHILDREN_NODE][i+1];
                }
                settleNodeRelatedID(null, nI, treeData[CHILDREN_NODE][i]);
            }
        }
    },
    settleNodeRelatedID : function(parentItem, nextItem, node) {
        if (parentItem) {
            node[P_ID] = parentItem[NODE_ID];
        } else {
            node[P_ID] = EMPTY_ID_VAL;
        }
        if (nextItem) {
            node[NEXT_ID] = nextItem[NODE_ID];
        } else {
            node[NEXT_ID] = EMPTY_ID_VAL;
        }
        for (var i = 0; i < node[CHILDREN_NODE].length; i++) {
            var nI = null;
            if (i < node[CHILDREN_NODE].length - 1) {
                nI = node[CHILDREN_NODE][i+1];
            }
            private_settleNodeRelatedID(node, nI, node[CHILDREN_NODE][i]);
        }
    }
}
