var TPPathCalc = {
    init: function () {
        this.initView();
        this.initForm();
    },
    initView: function () {
        this.gView = z.gv.GView({
            appendTo: "#gvDiv",
            movable: false,
            data: this._createTestTPData()
        });
        pro.GVUtil.initToolbar(this.gView);
        pro.GVUtil.initTooltip(this.gView, true);

        this.listView = new z.widget.ListView({
            appendTo: "#listDiv",
            auto_select: true,
            hoverable: true,
            select_on_hover: true
        });

        this.listView.onSelectBatchChange(function () {
            this._updateDataAlpha(this.listView.getLastSelected());
        }, this);
    },
    initForm: function () {
        var nodes = [];
        this.gView.eachData(function (data) {
            if (z.type.isNode(data)) {
                var name = data.get("name");
                nodes.push({value: name, label: "node-" + name})
            }
        });
        z.dom.initSelectOptions("[ze-model=src_node]", nodes);
        z.dom.initSelectOptions("[ze-model=dst_node]", nodes);
        this.form = new z.form.Form("#controlForm");
        this.form.setValue({directed: true});
        this.form.onFormChange(this._handleFormChange, this);
    },
    _handleFormChange: function () {
        this.listView.setData([]);
        this._updateAllDataAlpha(1);

        var value = this.form.getValue();
        var directed = value.directed;
        if (directed === true) {
            this.gView.eachData(function (data) {
                if (z.type.isLink(data)) {
                    data.set("to_arrow_shape", 'arrow');
                }
            })
        } else {
            this.gView.eachData(function (data) {
                if (z.type.isLink(data)) {
                    data.remove("to_arrow_shape");
                }
            })
        }
        var src_node = value.src_node;
        var dst_node = value.dst_node;
        if (!src_node || !dst_node) {
            return;
        }
        var srcNode = this.gView.findData("name", src_node);
        var dstNode = this.gView.findData("name", dst_node);
        if (!srcNode || !dstNode) {
            return;
        }

        var pathArr = pro.GVPathCalc.calcPathsBetween(srcNode, dstNode, this._sortByWeight, null, directed, value.disjoint);// [];
        var listDataArr = [];
        pathArr.forEach(function (path, index) {
            var weight = 0;
            path.forEach(function (data) {
                if (z.type.isLink(data)) {
                    weight += data.get("weight");
                }
            });
            listDataArr.push(new z.Data({
                name: index + 1 + " (" + weight + ")",
                path: path
            }));
        });
        this.listView.setData(listDataArr);
        if (listDataArr.length === 0) {
            this._updateAllDataAlpha(0.2);
        }
    },
    _updateAllDataAlpha: function (alpha) {
        this.gView.eachData(function (data) {
            data.set("alpha", alpha);
        });
    },
    _updateDataAlpha: function (pathData) {
        if (!pathData) {
            this._updateAllDataAlpha(1);
            return;
        }
        var path = pathData.get("path") || [];
        this.gView.eachData(function (data) {
            if (path.indexOf(data) > -1) {
                data.set("alpha", 1);
            } else {
                data.set("alpha", 0.2);
            }
        })
    },
    _sortByWeight: function (p1, p2) {
        var w1 = 0;
        p1.forEach(function (data) {
            if (z.type.isLink(data)) {
                w1 += data.get("weight");
            }
        });
        var w2 = 0;
        p2.forEach(function (data) {
            if (z.type.isLink(data)) {
                w2 += data.get("weight");
            }
        });
        if (w1 < w2) {
            return -1;
        }
        if (w1 > w2) {
            return 1;
        }
        return 0;
    },
    _createTestTPData: function () {
        var dataArr = [];
        for (var i = 1; i < 9; i++) {
            dataArr.push(new z.gv.Node({
                name: i + "",
                label_fill: "#fff", label_position: "center",
                x: 100 + (i / 2 | 0) * 160,
                y: 200 + ((i === 1 || i === 8) ? 0 : (i % 2 - 0.5) * 200)
            }));
        }
        dataArr.push(this._createLink(dataArr[0], dataArr[1]));
        dataArr.push(this._createLink(dataArr[1], dataArr[2]));
        dataArr.push(this._createLink(dataArr[1], dataArr[3]));
        dataArr.push(this._createLink(dataArr[3], dataArr[1]));
        dataArr.push(this._createLink(dataArr[3], dataArr[4]));
        dataArr.push(this._createLink(dataArr[3], dataArr[5]));
        dataArr.push(this._createLink(dataArr[3], dataArr[5]));
        dataArr.push(this._createLink(dataArr[5], dataArr[6]));
        dataArr.push(this._createLink(dataArr[5], dataArr[7]));
        dataArr.push(this._createLink(dataArr[5], dataArr[1], {type: "ra_top"}));
        dataArr.push(this._createLink(dataArr[0], dataArr[2]));
        dataArr.push(this._createLink(dataArr[2], dataArr[4]));
        dataArr.push(this._createLink(dataArr[4], dataArr[6]));
        dataArr.push(this._createLink(dataArr[6], dataArr[4]));
        dataArr.push(this._createLink(dataArr[6], dataArr[5]));
        dataArr.push(this._createLink(dataArr[6], dataArr[7]));
        return dataArr;
    },
    _createLink: function (from, to, props) {
        var weight = +Math.random() * 100 | 0;
        return new z.gv.Link(from, to, z.util.mergeObject({
            to_arrow_shape: "arrow",
            label_rotation: false,
            label_font: "12px Arial, sans-serif",
            name: weight,
            weight: weight
        }, props))
    }
};
z.ready(function () {
    TPPathCalc.init();
});