/**
 * 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, TREE_LEVEL = 'treeLevel', TOP_BILL_ID = "topBillID";

let tree_Data_Helper = {
    buildTreeNodeDirectly: function(data, addLevel) {
        let topArr = [], rst = [], tmpNodes = {}, prefix = "id_";
        let private_getStartNode = function (idArr) {
            let tmpNodeRst = null;
            for (let i = 0; i < idArr.length; i++) {
                if (parseInt(tmpNodes[prefix + idArr[i]][ADHOC_PRE_ID]) === EMPTY_ID_VAL) {
                    tmpNodeRst = tmpNodes[prefix + idArr[i]];
                    break;
                }
            }
            return tmpNodeRst;
        };
        let private_buildNodeData = function(parentItem, idArr, treeLevel, tbID) {
            let iter = [], nextNode = private_getStartNode(idArr), pushedIds = [];
            while (nextNode !== null && nextNode !== undefined ) {
                if (parentItem) {
                    parentItem[CHILDREN_NODE].push(nextNode);
                } else {
                    rst.push(nextNode);
                }
                iter.push(nextNode);
                pushedIds.push(nextNode[NODE_ID]);
                nextNode[TOP_BILL_ID] = tbID;
                if (parentItem === null) {
                    nextNode[TOP_BILL_ID] = nextNode[NODE_ID];
                    if (nextNode.flags && nextNode.flags.length > 0) {
                        for (let flag of nextNode.flags) {
                            if (flag.fieldName === "fixed") {
                                nextNode[TOP_BILL_ID] = flag.flag;
                                break;
                            }
                        }
                    }
                }
                if (addLevel) nextNode[TREE_LEVEL] = treeLevel;
                nextNode = tmpNodes[prefix + nextNode[NEXT_ID]];
                if (nextNode === null || nextNode === undefined) {
                    //备注： 考虑到实际数据的健壮性，有些节点会掉链子，需要用 parentItem[SUB_ID] 比对已经加上的节点，如发现加上的节点数量不够，那就得在这里补充上去
                    if (parentItem) {
                        if (parentItem[SUB_ID].length > iter.length) {
                            for (let subId of parentItem[SUB_ID]) {
                                if (pushedIds.indexOf(subId) < 0) {
                                    let restNode = tmpNodes[prefix + subId];
                                    if (addLevel) restNode[TREE_LEVEL] = treeLevel;
                                    restNode[TOP_BILL_ID] = tbID;
                                    parentItem[CHILDREN_NODE].push(restNode);
                                    iter.push(restNode);
                                }
                            }
                        }
                    } else {
                        if (idArr.length > iter.length) {
                            for (let topId of idArr) {
                                if (pushedIds.indexOf(topId) < 0) {
                                    let restNode = tmpNodes[prefix + topId];
                                    if (addLevel) restNode[TREE_LEVEL] = treeLevel;
                                    restNode[TOP_BILL_ID] = restNode[NODE_ID];
                                    if (restNode.flags && restNode.flags.length > 0) {
                                        for (let flag of restNode.flags) {
                                            if (flag.fieldName === "fixed") {
                                                restNode[TOP_BILL_ID] = flag.flag;
                                                break;
                                            }
                                        }
                                    }
                                    rst.push(restNode);
                                    iter.push(restNode);
                                }
                            }
                        }
                    }
                }
            }
            pushedIds = [];
            for (let i = 0; i < iter.length; i++) {
                let rtbID = tbID;
                if (parentItem === null) {
                    rtbID = iter[i][TOP_BILL_ID];
                }
                private_buildNodeData(iter[i], iter[i][SUB_ID], (treeLevel + 1), rtbID);
            }
        };
        //1. 给每个节点设置key, 顺便找Top Node
        for (let 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 (parseInt(data[i][P_ID]) === EMPTY_ID_VAL) {
                topArr.push(data[i][NODE_ID]);
            }
        }
        //2. 通过key，设置好兄弟/父子关系
        for (let i = 0; i < data.length; i++) {
            if (parseInt(data[i][NEXT_ID]) !== EMPTY_ID_VAL) {
                if (tmpNodes[prefix + data[i][NEXT_ID]] !== undefined){
                    if (tmpNodes[prefix + data[i][NEXT_ID]][P_ID] === data[i][P_ID]) {
                        tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = data[i][NODE_ID];
                    } else {
                        tmpNodes[prefix + data[i][NEXT_ID]][ADHOC_PRE_ID] = EMPTY_ID_VAL;
                        data[i][NEXT_ID] = EMPTY_ID_VAL;
                    }
                }
            }
            if (parseInt(data[i][P_ID]) !== EMPTY_ID_VAL) {
                tmpNodes[prefix + data[i][P_ID]][SUB_ID].push(data[i][NODE_ID]);
            }
        }
        //3. 开build
        private_buildNodeData(null, topArr, 0, -1);
        //try to release and return
        tmpNodes = null;
        topArr.length = 0;
        return rst;
    },

    getFlatArray: function (srcArr, destArr) {
        let private_put = function (parentItem) {
            destArr.push(parentItem);
            if (parentItem.items) {
                for (let subItem of parentItem.items) {
                    private_put(subItem);
                }
            }
        }
        for (let node of srcArr) {
            private_put(node);
        }
        for (let item of destArr) {
            delete item[CHILDREN_NODE];
            delete item[SUB_ID];
            delete item[ADHOC_PRE_ID];
        }
    }

    // reSettleRelatedID: function(treeData) {
    //     if (treeData && treeData.length > 0) {
    //         for (let i = 0; i < treeData[CHILDREN_NODE].length; i++) {
    //             let 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 (let i = 0; i < node[CHILDREN_NODE].length; i++) {
    //         let nI = null;
    //         if (i < node[CHILDREN_NODE].length - 1) {
    //             nI = node[CHILDREN_NODE][i+1];
    //         }
    //         private_settleNodeRelatedID(node, nI, node[CHILDREN_NODE][i]);
    //     }
    // }
};
