'use strict'

let bandTreeOprObj = {
    treeObj : null,
    currentNode: null,
    innerCounter: 1,
    canTrickEvent: false,
    iniTree: function(rptTpl) {
        let me = bandTreeOprObj;
        let bandList = rptTpl[JV.NODE_BAND_COLLECTION];
        me.buildTreeData(bandList);
        let rootNode = {Name: "布局框(勾选表示：此布局框是报表外框，受报表边框属性影响)", isParent: true};
        rootNode[JV.BAND_PROP_SUB_BANDS] = bandList;
        // me.treeObj = $.fn.zTree.init($("#band_tree_reversed"), bandSetting, bandList);
        me.treeObj = $.fn.zTree.init($("#band_tree_reversed"), bandSetting, rootNode);
        me.treeObj.expandAll(true);
        me.setupMergeBorderChk();
    },
    setupMergeBorderChk: function() {
        let me = bandTreeOprObj;
        let nodes = me.treeObj.getNodes();
        me.treeObj.setChkDisabled(nodes[0], true);
        function checkIsMergeBorder(bandNode) {
            if (bandNode[JV.BAND_PROP_MERGE_BORDER] != null && bandNode[JV.BAND_PROP_MERGE_BORDER] != undefined) {
                if (stringUtil.convertStrToBoolean(bandNode[JV.BAND_PROP_MERGE_BORDER])) {
                    me.treeObj.checkNode(bandNode, true);
                    return false;
                }
            }
            if (bandNode[JV.BAND_PROP_SUB_BANDS] && bandNode[JV.BAND_PROP_SUB_BANDS].length > 0) {
                for (let subBandNode of bandNode[JV.BAND_PROP_SUB_BANDS]) {
                    if (!checkIsMergeBorder(subBandNode)) {
                        return false;
                    }
                }
            }
            return true;
        }
        for (let bNode of nodes) {
            checkIsMergeBorder(bNode);
        }
    },
    copyBandList: function (isAllParent) {
        let me = this, rst = null;
        if (me.treeObj) {
            rst = [];
            let nodes = me.treeObj.getNodes()[0][JV.BAND_PROP_SUB_BANDS];
            let private_copy_nodes = function (parentNode, item) {
                item[JV.PROP_ID] = parentNode[JV.PROP_ID];
                item[JV.BAND_PROP_NAME] = parentNode[JV.BAND_PROP_NAME];
                item[JV.BAND_PROP_STYLE] = parentNode[JV.BAND_PROP_STYLE];
                item[JV.BAND_PROP_CONTROL] = parentNode[JV.BAND_PROP_CONTROL];
                if (parentNode[JV.BAND_PROP_HEIGHT]) item[JV.BAND_PROP_HEIGHT] = parentNode[JV.BAND_PROP_HEIGHT];
                if (parentNode[JV.BAND_PROP_WIDTH]) item[JV.BAND_PROP_WIDTH] = parentNode[JV.BAND_PROP_WIDTH];
                item[JV.BAND_PROP_DISPLAY_TYPE] = parentNode[JV.BAND_PROP_DISPLAY_TYPE];
                item[JV.BAND_PROP_ALIGNMENT] = parentNode[JV.BAND_PROP_ALIGNMENT];
                item[JV.BAND_PROP_MERGE_BORDER] = parentNode[JV.BAND_PROP_MERGE_BORDER];
                if (isAllParent) item.isParent = true;
                if (parentNode[JV.BAND_PROP_SUB_BANDS]) {
                    item.items = [];
                    for (let subNode of parentNode[JV.BAND_PROP_SUB_BANDS]) {
                        let subItem = {};
                        private_copy_nodes(subNode, subItem);
                        item.items.push(subItem);
                    }
                }
            }
            for (let node of nodes) {
                let item = {};
                if (isAllParent) item.isParent = true;
                private_copy_nodes(node, item);
                rst.push(item);
            }
        } else {
            console.log("the band tree is not ready!");
        }
        return rst;
    },
    buildTreeData: function(bandList){
        let rst = [], startIdx = 1;
        //zTreeHelper.createTree(result, setting, "rptTplTree", me);
        let private_setBandId = function (parentBand) {
            if (parentBand[JV.BAND_PROP_SUB_BANDS]) {
                for (let band of parentBand[JV.BAND_PROP_SUB_BANDS]) {
                    band.ID = startIdx;
                    band.ParentID = parentBand.ID;
                    startIdx++;
                    private_setBandId(band);
                }
            }
        }
        for (let band of bandList) {
            band.ID = startIdx;
            band.ParentID = -1;
            startIdx++;
            private_setBandId(band);
        }

        return rst;
    },
    createBandFromNode: function(node) {
        let me = this, rst = {};
        rst[JV.BAND_PROP_ALIGNMENT] = node[JV.BAND_PROP_ALIGNMENT];
        rst[JV.BAND_PROP_DISPLAY_TYPE] = node[JV.BAND_PROP_DISPLAY_TYPE];
        let posIdx = JV.LAYOUT.indexOf(node[JV.BAND_PROP_ALIGNMENT])
        switch(posIdx) {
            case 0:
            case 1:
                rst[JV.BAND_PROP_HEIGHT] = node[JV.BAND_PROP_HEIGHT];
                break;
            case 2:
            case 3:
                rst[JV.BAND_PROP_WIDTH] = node[JV.BAND_PROP_WIDTH];
                break;
        }
        rst[JV.PROP_NAME] = node[JV.PROP_NAME];
        rst[JV.PROP_CONTROL] = node[JV.PROP_CONTROL];
        rst[JV.PROP_STYLE] = node[JV.PROP_STYLE];
        if (node[JV.BAND_PROP_MERGE_BORDER] !== undefined) {
            rst[JV.BAND_PROP_MERGE_BORDER] = node[JV.BAND_PROP_MERGE_BORDER];
        } else {
            rst[JV.BAND_PROP_MERGE_BORDER] = 'F';
        }
        if (node[JV.BAND_PROP_SUB_BANDS]) {
            rst[JV.BAND_PROP_SUB_BANDS] = [];
            for (let subNode of node[JV.BAND_PROP_SUB_BANDS]) {
                rst[JV.BAND_PROP_SUB_BANDS].push(me.createBandFromNode(subNode));
            }
        }
        return rst;
    },
    createDftBand: function () {
        let me = this, rst = {};
        rst[JV.BAND_PROP_ALIGNMENT] = 'Top';
        rst[JV.BAND_PROP_DISPLAY_TYPE] = 'EveryPage';
        rst[JV.BAND_PROP_HEIGHT] = '3';
        rst[JV.PROP_NAME] = 'newBand_' + me.innerCounter;
        me.innerCounter++;
        rst[JV.PROP_CONTROL] = 'Default';
        rst[JV.PROP_STYLE] = 'Default_None';
        rst[JV.BAND_PROP_MERGE_BORDER] = 'F';
        return rst;
    },
    addHoverDom: function(treeId, treeNode) {
        let me = bandTreeOprObj, sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length > 0 || treeNode.nodeType === RT.NodeType.TEMPLATE) return;
        let addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增布局框' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        let btn = $("#addBtn_"+treeNode.tId);
        if (btn) btn.bind("click", function(){
            let newBand = me.createDftBand();
            let newNodes = [], isSilent = false;
            newNodes.push(newBand);
            me.treeObj.addNodes(treeNode, -1, newNodes, isSilent);
        });
    },
    removeHoverDom: function(treeId, treeNode) {
        $("#addBtn_"+treeNode.tId).unbind().remove();
    },
    onBeforeDrag: function (treeId, treeNodes) {
        let rst = true;
        for (let node of treeNodes) {
            if (node.level === 0) {
                rst = false;
                break;
            }
        }
        return rst;
    },
    onBeforeDrop: function (treeId, treeNodes, targetNode, moveType, isCopy) {
        let me = bandTreeOprObj, rst = true;
        if (targetNode.level === 0) {
            if (moveType !== 'inner') {
                rst = false;
            }
        }
        return rst;
    },
    onClick: function(event,treeId,treeNode) {
        let me = bandTreeOprObj;
        me.currentNode = treeNode;
        me.canTrickEvent = false;
        //then refresh the band tab properties
        //边框样式borderStyles
        $("#borderStyles").get(0).selectedIndex = rpt_tpl_cfg_helper.reportCfg.borderArr.indexOf(treeNode[JV.PROP_STYLE]);
        //边框合并
        // $("#mergeBandBorder").get(0).checked = stringUtil.convertStrToBoolean(treeNode[JV.BAND_PROP_MERGE_BORDER]);
        //位置
        let posIdx = JV.LAYOUT.indexOf(treeNode[JV.BAND_PROP_ALIGNMENT])
        $("#bandAlignment").get(0).selectedIndex = posIdx;
        //高与宽
        me.setupWidthHeightByPosition(posIdx);
        //频率
        $("#pageFrequency").get(0).selectedIndex = JV.PAGE_STATUS.indexOf(treeNode[JV.BAND_PROP_DISPLAY_TYPE])
        //
        me.canTrickEvent = true;
    },
    onCheck: function (event,treeId,treeNode) {
        let me = bandTreeOprObj;
        let nodes = me.treeObj.getNodes();
        function unCheckMergeBorder(bandNode) {
            if (bandNode[JV.BAND_PROP_MERGE_BORDER] != null && bandNode[JV.BAND_PROP_MERGE_BORDER] != undefined) {
                bandNode[JV.BAND_PROP_MERGE_BORDER] = 'F';
            }
            if (bandNode[JV.BAND_PROP_SUB_BANDS] && bandNode[JV.BAND_PROP_SUB_BANDS].length > 0) {
                for (let subBandNode of bandNode[JV.BAND_PROP_SUB_BANDS]) {
                    unCheckMergeBorder(subBandNode)
                }
            }
        }
        for (let bNode of nodes) {
            unCheckMergeBorder(bNode);
        }
        if (treeNode.checked) {
            treeNode[JV.BAND_PROP_MERGE_BORDER] = 'T';
        }
    },
    onBeforeRemove: function(treeId, treeNode){
        let rst = true;
        if (treeNode.band_s && treeNode.band_s.length > 0) {
            rst = false;
        } else {
            rst = confirm("在删除前请确认报表模板内部没有引用，否则会造成混淆。");
        }
        return rst;
    },
    setupWidthHeightByPosition: function (posIdx) {
        let me = this, treeNode = me.currentNode;
        switch(posIdx) {
            case 0:
            case 1:
                $("#bandHeight").get(0).disabled = false;
                $("#bandHeight").get(0).value = treeNode[JV.BAND_PROP_HEIGHT];
                $("#bandWidth").get(0).disabled = true;
                $("#bandWidth").get(0).value = "";
                break;
            case 2:
            case 3:
                $("#bandHeight").get(0).disabled = true;
                $("#bandHeight").get(0).value = "";
                $("#bandWidth").get(0).disabled = false;
                $("#bandWidth").get(0).value = treeNode[JV.BAND_PROP_WIDTH];
                break;
            default:
                $("#bandHeight").get(0).disabled = true;
                $("#bandHeight").get(0).value = "";
                $("#bandWidth").get(0).disabled = true;
                $("#bandWidth").get(0).value = "";
                break;
        }
    },
    bandStyleChange: function (dom) {
        let me = this;
        if (me.currentNode) {
            me.currentNode[JV.PROP_STYLE] = rpt_tpl_cfg_helper.reportCfg.borderArr[dom.selectedIndex];
        }
    },
    bandAlignmentChange: function (dom) {
        let me = this;
        if (me.currentNode) {
            let posIdx = dom.selectedIndex;
            me.currentNode[JV.BAND_PROP_ALIGNMENT] = JV.LAYOUT[posIdx];
            me.setupWidthHeightByPosition(posIdx);
        }
    },
    bandHeightWidthChange: function (dom) {
        let me = this, treeNode = me.currentNode;
        if (me.currentNode) {
            let posIdx = $("#bandAlignment").get(0).selectedIndex;
            switch(posIdx) {
                case 0:
                case 1:
                    treeNode[JV.BAND_PROP_HEIGHT] = dom.value;
                    break;
                case 2:
                case 3:
                    treeNode[JV.BAND_PROP_WIDTH] = dom.value;
                    break;
            }
        }
    },
    changeBandHeight: function (bandName, newHeight) {
        let me = this;
        let private_setup_band_height = function (bNode) {
            let rst = false;
            if (bNode[JV.PROP_NAME] === bandName) {
                bNode[JV.BAND_PROP_HEIGHT] = newHeight;
                rst = true;
            }
            if (bNode[JV.BAND_PROP_SUB_BANDS] && bNode[JV.BAND_PROP_SUB_BANDS].length > 0) {
                for (let sbNode of bNode[JV.BAND_PROP_SUB_BANDS]) {
                    if (private_setup_band_height(sbNode)) {
                        rst = true;
                        break;
                    }
                }
            }
            return rst;
        };
        if (me.currentNode && me.currentNode[JV.PROP_NAME] === bandName) {
            me.currentNode[JV.BAND_PROP_HEIGHT] = newHeight;
        } else {
            let nodes = me.treeObj.getNodes();
            for (let node of nodes) {
                if (private_setup_band_height(node)) {
                    break;
                }
            }
        }
    },
    bandShowFrequencyChange: function (dom) {
        let me = this;
        if (me.currentNode) {
            me.currentNode[JV.BAND_PROP_DISPLAY_TYPE] = JV.PAGE_STATUS[dom.selectedIndex];
        }
    },
    extractBands: function (rptTpl) {
        let me = this;
        if (rptTpl) {
            let newBandList = [];
            let nodes = me.treeObj.getNodes()[0][JV.BAND_PROP_SUB_BANDS];
            for (let node of nodes) {
                newBandList.push(me.createBandFromNode(node));
            }
            rptTpl[JV.NODE_BAND_COLLECTION] = newBandList;
        }
    }

};
