var FlowChartUtil = {
    WIDTH: 160,
    HEIGHT: 50,
    createStartEnd: function (props) {
        return new z.gv.Node(z.util.mergeObject({
            _type: "startend",
            shape: "roundrect",
            roundrect_radius: 0.3,
            fill: "#f96872",
            stroke: null,
            label_fill: "#ffffff",
            label_position: "center",
            width: FlowChartUtil.WIDTH,
            height: FlowChartUtil.HEIGHT
        }, props))
    },
    createProcess: function (props) {
        return new z.gv.Node(z.util.mergeObject({
            _type: "process",
            shape: "rect",
            fill: "#3e8bb9",
            stroke: null,
            label_fill: "#ffffff",
            label_position: "center",
            width: FlowChartUtil.WIDTH,
            height: FlowChartUtil.HEIGHT
        }, props))
    },
    createDecision: function (props) {
        return new z.gv.Node(z.util.mergeObject({
            _type: "decision",
            shape: "diamond",
            fill: "#51c69b",
            stroke: null,
            label_fill: "#ffffff",
            label_position: "center",
            width: FlowChartUtil.WIDTH,
            height: FlowChartUtil.HEIGHT * 2
        }, props))
    },
    createLink: function (from, to, props) {
        return new z.gv.Link(from, to, z.util.mergeObject({
            to_arrow_shape: "arrow",
            stroke: "#3e8bb9",
            label_box_fill: "#ffffff"
        }, FlowChartUtil._getLinkProps(props, from)));
    },
    addLinkProcess: function (dataArr, from, node_props, link_props) {
        var node = FlowChartUtil.createProcess(node_props);
        dataArr.push(node);
        dataArr.push(FlowChartUtil.createLink(from, node, FlowChartUtil._getLinkProps(link_props, from)));
        return node;
    },
    addLinkDecision: function (dataArr, from, node_props, link_props) {
        var node = FlowChartUtil.createDecision(node_props);
        dataArr.push(node);
        dataArr.push(FlowChartUtil.createLink(from, node, FlowChartUtil._getLinkProps(link_props, from)));
        return node;
    },

    addRow: function (dataArr, row_props, title_props) {
        var textSize = 30;
        var x = row_props.x;
        var y = row_props.y;
        var row_height = row_props.height;
        var rowNode = new z.gv.Node(z.util.mergeObject({
            selectable: false,
            shape: "rect",
            stroke: null,
            outline_padding: -0.5,
            outline_stroke_line_width: 0.5,
            outline_stroke: "#ccc",
            fill: null//important
        }, row_props));
        var textNode = new z.gv.Node(z.util.mergeObject({
            selectable: false,
            parent: rowNode,

            width: row_height,
            height: textSize,
            x: x + textSize / 2 - row_height / 2,
            y: y + row_height / 2 - textSize / 2,

            label_position: "center",
            label_fill: "#ffffff",
            rotation: -90,

            shape: "rect",
            fill: "#52aac2"
        }, title_props));
        dataArr.push(rowNode);
        dataArr.push(textNode);
        return rowNode;
    },

    _isDecision: function (node) {
        return node.get("_type") === "decision";
    },
    _getLinkProps: function (link_props, from) {
        if (z.type.isString(link_props)) {
            if (FlowChartUtil._isDecision(from)) {
                return {
                    name: link_props
                }
            } else {
                return {
                    type: link_props
                }
            }
        }
        return link_props;
    }
};