// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        allowDiagonals: {
            default: false,
            serializable: false,
        },

        diagonalCost: {
            default: false,
            serializable: false,
        },


        dotTiebreaker: {
            default: false,
            serializable: false,
        },


        badSorting: {
            default: true,
            serializable: false,
        },
    },

    setAllowDiagonals(event) {
        // cc.log('event',event);
        this.allowDiagonals = event.isChecked;
        this.draw();
    },

    setDiagonalCost(event) {
        // cc.log('event',event);
        this.diagonalCost = event.isChecked;
        this.draw();
    },

    setDotTiebreaker(event) {
        // cc.log('event',event);
        this.dotTiebreaker = event.isChecked;
        this.draw();
    },

    setBadSorting(event) {
        // cc.log('event',event);
        this.badSorting = event.isChecked;
        this.draw();
    },

    start() {

        // AStar.Init();
        this.draw();

    },

    clearMap() {
        let map = AStar.getMap();
        for (var i = 0; i < 32; i++) {
            for (var j = 0; j < 32; j++) {
                map[i][j].solid = false;
            }
        }
        this.draw();
    },

    randomMap() {
        let map = AStar.getMap();
        for (var i = 0; i < 32; i++) {
            for (var j = 0; j < 32; j++) {
                map[i][j].solid = Math.random() > 0.8;
            }
        }
        this.draw();
    },

    draw() {
        // console.log('this.allowDiagonals=', this.allowDiagonals,
        //     'this.diagonalCost=', this.diagonalCost,
        //     'this.dotTiebreaker=', this.dotTiebreaker,
        //     'this.badSorting=', this.badSorting)

        AStar.setInitInfo(this.allowDiagonals, this.diagonalCost, this.dotTiebreaker, this.badSorting);

        var ctx = this.getComponent(cc.Graphics);
        let map = AStar.getMap();

        var start = { x: 2, y: 2 };
        AStar.setStart(start);
        var end = { x: 31, y: 31 };

        ctx.fillColor = new cc.Color().fromHEX('#EEEEEE');
        ctx.strokeColor = new cc.Color().fromHEX('#888888');
        ctx.rect(0, 0, 500, 500);
        ctx.stroke();
        ctx.fill();

        //设置AStar类里面的graphics
        AStar.setGraphics(ctx);

        var r = AStar.pathFind(start.x, start.y, end.x, end.y);
        // cc.log('得到的路径 = ', r);

        ctx.strokeColor = new cc.Color().fromHEX('#888888');
        ctx.rect(0.5, 0.5, 500, 500);
        ctx.stroke();

        ctx.fillColor = new cc.Color().fromHEX('#333333');
        ctx.strokeColor = new cc.Color().fromHEX('#333333');
        for (let i = 0; i < 32; i++) {
            for (let j = 0; j < 32; j++) {
                if (map[i][j].solid) {

                    ctx.rect(i * 10, j * 10, 10, 10);
                    ctx.stroke();
                    ctx.fill();
                }
                else {

                    ctx.rect(i * 10 - 0.5, j * 10 - 0.5, 10, 10);
                    ctx.stroke();
                }
            }
        }


        // return;
        ctx.fillColor = new cc.Color().fromHEX('#FF7777');
        ctx.rect(start.x * 10, start.y * 10, 10, 10);
        ctx.stroke();
        ctx.fill();

        ctx.fillColor = new cc.Color().fromHEX('#7777FF');
        ctx.rect(end.x * 10, end.y * 10, 10, 10);
        ctx.stroke();
        ctx.fill();

        ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
        for (let i = 0; i < r.length; i++) {
            if (i == 0) ctx.moveTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
            else ctx.lineTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
        }
        ctx.stroke();
    }
});
