var OrgChartUtil = {
    createLink: function (from, to, props, dataArr) {
        var link = new z.gv.Link(from, to, z.util.mergeObject({
            stroke: "#3b8a91"
        }, OrgChartUtil._getLinkProps(props, from)));
        if (dataArr) {
            dataArr.push(link);
        }
        return link;
    },
    createRectNode: function (props) {
        var node = OrgChartUtil._createNode(z.util.mergeObject({
            shape: "rect", width: 160, height: 50
        }, props));
        var center = props.center;
        if (center) {
            node.setCenter(center);
        }
        return node;
    },
    addLinkRectNode: function (dataArray, from, node_props, link_props) {
        var node = OrgChartUtil.createRectNode(node_props);
        dataArray.push(node);
        dataArray.push(OrgChartUtil.createLink(from, node, OrgChartUtil._getLinkProps(link_props, from)));
        return node;
    },

    addLinkDepartment: function (dataArr, from, node_props, link_props) {
        var node = OrgChartUtil._createDepartment(node_props);
        dataArr.push(node);
        dataArr.push(OrgChartUtil.createLink(from, node, OrgChartUtil._getLinkProps(link_props, from)));
        return node;
    },

    createAvatar: function (props, dataArr) {
        var avatar_position = props.avatar_position;
        var _props = {fill: "#3b8a91"};
        if (avatar_position === "left" || avatar_position === "right") {
            _props.width = 250;
            _props.height = 60
        } else {
            _props.width = 200;
            _props.height = 120
        }
        var avatar = new Avatar(z.util.mergeObject(_props, props));
        var center = props.center;
        if (center) {
            avatar.setCenter(center);
        }
        if (dataArr) {
            dataArr.push(avatar)
        }
        return avatar;
    },


    _createDepartment: function (props) {
        return OrgChartUtil.createRectNode(z.util.mergeObject({
            width: 200,
            height: 40
        }, props));
    },
    _createNode: function (props) {
        return new z.gv.Node(z.util.mergeObject({
            fill: "#70ba50", stroke: null, label_position: "center", label_fill: "#fefefe"
        }, props))
    },
    _getLinkProps: function (link_props, from) {
        if (z.type.isString(link_props)) {
            return {
                type: link_props
            }
        }
        return link_props;
    }
};


var Avatar = z.util.newClass(z.gv.Node, function () {
}, {
    drawAvatar: function (g2d, avatarRect, node, view) {
        var fill = node.get("fill");
        var cx = avatarRect.x + avatarRect.width / 2, cy = avatarRect.y + avatarRect.height / 2;
        var r = Math.min(avatarRect.width, avatarRect.height) / 2;
        avatarRect = {
            x: cx - r,
            y: cy - r,
            width: r * 2,
            height: r * 2
        };
        g2d.lineWidth = 1;
        z.canvas2d.renderShape(g2d, avatarRect, "circle", {stroke: fill, fill: "#FFF"});//outline
        var gap = 4;
        avatarRect = {
            x: avatarRect.x + gap,
            y: avatarRect.y + gap,
            width: avatarRect.width - gap * 2,
            height: avatarRect.height - gap * 2
        };
        z.canvas2d.renderShape(g2d, avatarRect, "circle", {fill: fill});//fill
        var img = z.util.image.get(node.get("avatar"), function () {
            view.update();
        });
        if (img) {
            var ip1 = z.math.calcPointOnCircle({x: cx, y: cy}, avatarRect.width / 2, 225);
            var ip2 = z.math.calcPointOnCircle({x: cx, y: cy}, avatarRect.width / 2, 45);
            var imgRect = {
                x: ip1.x,
                y: ip1.y,
                width: ip2.x - ip1.x,
                height: ip2.y - ip1.y
            };
            g2d.drawImage(img, imgRect.x, imgRect.y, imgRect.width, imgRect.height);
        }
    },
    drawLabel: function (g2d, labelRect, offset, node) {
        offset = offset | 0;
        var fill = node.get("fill");
        var name = node.get("name");
        var title = node.get("title");
        z.canvas2d.renderShape(g2d, labelRect, "rect", {fill: fill});//outline
        var cx = {
            x: labelRect.x + labelRect.width / 2,
            y: labelRect.y + labelRect.height / 2 + offset
        };
        z.canvas2d.renderText(g2d, name, cx, {fill: node.get("name_color")}, "top", {font: node.get("name_font")});
        if (title) {
            cx.y += 2;
            z.canvas2d.renderText(g2d, title, cx, {fill: node.get("title_color")}, "bottom", {font: node.get("title_font")});
        }
    },
    draw: function (g2d, node, view) {
        var rect = node.getBounds();
        var avatar_position = node.get("avatar_position");
        //left right top
        var avatarRect, labelRect, labelOffset = 0;
        var offset = rect.height / 8;
        if (avatar_position === "left" || avatar_position === "right") {
            var avatarRadius = rect.height / 2;
            avatarRect = {
                x: rect.x,
                y: rect.y,
                width: avatarRadius * 2,
                height: avatarRadius * 2
            };
            var avatarCenter = {
                x: avatarRect.x + avatarRadius,
                y: avatarRect.y + avatarRadius
            };
            var labelHeight = Math.sqrt(Math.pow(avatarRadius, 2) - Math.pow(avatarRadius - offset, 2)) * 2;
            var ltPoint = {
                x: avatarCenter.x + avatarRadius - offset,
                y: avatarCenter.y - labelHeight / 2
            };
            labelRect = {
                x: ltPoint.x,
                y: ltPoint.y,
                width: rect.x + rect.width - ltPoint.x,
                height: labelHeight
            };
            if (avatar_position === "right") {
                avatarRect.x = rect.x + rect.width - avatarRadius * 2;
                labelRect.x = rect.x
            }
        } else {//top
            avatarRect = {
                x: rect.x,
                y: rect.y,
                width: rect.width,
                height: rect.height / 2 + offset
            };
            labelRect = {
                x: rect.x,
                y: rect.y + rect.height / 2 - 2,
                width: rect.width,
                height: rect.height / 2 + 2
            };
            labelOffset = 6;
        }
        this.drawLabel(g2d, labelRect, labelOffset, node);
        this.drawAvatar(g2d, avatarRect, node, view);
    }
}, {
    width: 260, height: 80, alpha: 0,
    name_font: "italic normal bold 16px/28px Arial",
    name_color: "#fff",
    title_font: "italic normal normal 14px/22px Arial",
    title_color: "#eee"
});