/*************************************draw*************************************/
var CanNode = z.util.newClass(z.gv.Node, function () {
}, {
    draw: function (g2d, node) {
        g2d.save();
        var rect = node.getBounds();
        var fill = node.get("fill");
        var stroke = node.get("stroke");
        var ellipse_height = rect.height / 3;
        var bottom_ellipse_rect = {x: rect.x, y: rect.y + rect.height - ellipse_height, width: rect.width, height: ellipse_height};
        z.canvas2d.renderShape(g2d, bottom_ellipse_rect, "ellipse", {fill: fill, stroke: stroke});
        var rect_rect = {x: rect.x, y: rect.y + ellipse_height / 2, width: rect.width, height: rect.height - ellipse_height};
        if (fill) {
            z.canvas2d.renderPath(g2d, [
                {x: rect_rect.x, y: rect_rect.y},
                {x: rect_rect.x, y: rect_rect.y + rect_rect.height},
                {x: rect_rect.x + rect_rect.width, y: rect_rect.y + rect_rect.height},
                {x: rect_rect.x + rect_rect.width, y: rect_rect.y}
            ], {fill: fill});
        }
        if (stroke) {
            z.canvas2d.renderPath(g2d, [
                {x: rect_rect.x, y: rect_rect.y},
                {x: rect_rect.x, y: rect_rect.y + rect_rect.height, "seg": "mt"},
                {x: rect_rect.x + rect_rect.width, y: rect_rect.y + rect_rect.height},
                {x: rect_rect.x + rect_rect.width, y: rect_rect.y}
            ], {stroke: stroke});
        }
        var top_ellipse_rect = {x: rect.x, y: rect.y, width: rect.width, height: ellipse_height};
        if (fill) {
            g2d.lineWidth = 0.6;
        }
        z.canvas2d.renderShape(g2d, top_ellipse_rect, "ellipse", {fill: fill, stroke: fill == null ? stroke : "#eee"});
        g2d.restore();
    }
}, {shape: null, stroke: null});

var CubeNode = z.util.newClass(z.gv.Node, function () {
}, {
    draw: function (g2d, node) {
        g2d.save();
        g2d.lineJoin = "round";
        var rect = node.getBounds();
        var fill = node.get("fill");
        var dark_fill = fill ? z.util.color.darken(fill) : null;
        var lighten_fill = fill ? z.util.color.lighten(fill) : null;
        var stroke = node.get("stroke");

        var r = Math.min(rect.height / 5, rect.width / 5);
        var px = rect.x + rect.width - r;
        var py = rect.y + r;
        z.canvas2d.renderShape(g2d, {
            x: rect.x,
            y: py,
            width: rect.width - r,
            height: rect.height - r
        }, "rect", {fill: fill, stroke: stroke});
        //right
        z.canvas2d.renderPath(g2d, [
            {x: px, y: rect.y + rect.height},
            {x: rect.x + rect.width, y: rect.y + rect.height - r},
            {x: rect.x + rect.width, y: rect.y},
            {x: px, y: py}
        ], {fill: dark_fill, stroke: stroke});
        //top
        z.canvas2d.renderPath(g2d, [{x: rect.x, y: py},
            {x: px, y: py},
            {x: rect.x + rect.width, y: rect.y},
            {x: rect.x + r, y: rect.y, "seg": "close"}
        ], {fill: lighten_fill, stroke: stroke});
        g2d.restore();
    }
}, {shape: null, stroke: null});

var DocNode = z.util.newClass(z.gv.Node, function () {
}, {
    draw: function (g2d, node) {
        g2d.save();
        g2d.lineJoin = "round";
        var rect = node.getBounds();
        var fill = node.get("fill");
        var dark_fill = fill ? z.util.color.darken(fill) : null;
        var stroke = node.get("stroke");
        var dw = Math.min(rect.height, rect.width) * 0.26;
        z.canvas2d.renderPath(g2d, [
            {x: rect.x, y: rect.y},
            {x: rect.x + rect.width, y: rect.y},
            {x: rect.x + rect.width, y: rect.y + rect.height - dw},
            {x: rect.x + rect.width - dw, y: rect.y + rect.height},
            {x: rect.x, y: rect.y + rect.height, "seg": "close"}
        ], {fill: fill, stroke: stroke});
        var offset = 0.88
        if (dark_fill) {
            z.canvas2d.renderPath(g2d, [
                {x: rect.x + rect.width - dw, y: rect.y + rect.height},
                {x: rect.x + rect.width - dw * offset, y: rect.y + rect.height - dw * offset},
                {x: rect.x + rect.width, y: rect.y + rect.height - dw}
            ], {fill: dark_fill});
        }
        if (stroke) {
            z.canvas2d.renderPath(g2d, [
                {x: rect.x + rect.width - dw, y: rect.y + rect.height},
                {x: rect.x + rect.width - dw * offset, y: rect.y + rect.height - dw * offset},
                {x: rect.x + rect.width, y: rect.y + rect.height - dw}
            ], {stroke: stroke});
        }
        g2d.restore();
    }
}, {shape: null});

var AvatarNode = z.util.newClass(z.gv.Node, function () {
}, {
    draw: function (g2d, node, view) {
        var avatarRect = node.getBounds();
        var fill = node.get("fill");
        var cx = avatarRect.x + avatarRect.width / 2, cy = avatarRect.y + avatarRect.height / 2;
        var radius = Math.min(avatarRect.width, avatarRect.height) / 2;
        avatarRect = {
            x: cx - radius, y: cy - radius,
            width: radius * 2, height: radius * 2
        };
        g2d.lineWidth = 2;
        z.canvas2d.renderShape(g2d, avatarRect, "circle", {stroke: fill});//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});
        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);
        }
    }
}, {shape: null});

/*************************************shape*************************************/
z.canvas2d.registerShape("trapezoid", function (g2d, rect) {
    g2d.beginPath();
    z.canvas2d.path(g2d, [
        {x: rect.x + rect.width / 6, y: rect.y},
        {x: rect.x + rect.width - rect.width / 6, y: rect.y},
        {x: rect.x + rect.width, y: rect.y + rect.height},
        {x: rect.x, y: rect.y + rect.height}
    ]);
    g2d.closePath()
});

z.canvas2d.registerShape("cross", function (g2d, rect) {
    var cx = rect.x + rect.width / 2;
    var cy = rect.y + rect.height / 2;
    var hw = Math.min(10, Math.min(rect.width, rect.height) / 3) / 2;
    g2d.beginPath();
    z.canvas2d.path(g2d, [
        {x: rect.x, y: cy - hw},
        {x: cx - hw, y: cy - hw},
        {x: cx - hw, y: rect.y},
        {x: cx + hw, y: rect.y},
        {x: cx + hw, y: cy - hw},
        {x: rect.x + rect.width, y: cy - hw},
        {x: rect.x + rect.width, y: cy + hw},
        {x: cx + hw, y: cy + hw},
        {x: cx + hw, y: rect.y + rect.height},
        {x: cx - hw, y: rect.y + rect.height},
        {x: cx - hw, y: cy + hw},
        {x: rect.x, y: cy + hw}
    ]);
    g2d.closePath()
});

z.canvas2d.registerShape("snip_diagonal_corner_rectangle", function (g2d, rect) {
    var snip_size = Math.min(rect.width / 6, rect.width / 6)
    g2d.beginPath();
    z.canvas2d.path(g2d, [
        {x: rect.x, y: rect.y},
        {x: rect.x + rect.width - snip_size, y: rect.y},
        {x: rect.x + rect.width, y: rect.y + snip_size},
        {x: rect.x + rect.width, y: rect.y + rect.height},
        {x: rect.x + snip_size, y: rect.y + rect.height},
        {x: rect.x, y: rect.y + rect.height - snip_size}
    ]);
    g2d.closePath()
});