// 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: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },
    setMap : function(Map,type){
        this.actorsNode = Map;

        this._an(type);
        this._init();
    },
    _an :function (type){
        this.ansName = [];
        if (type == 0) {
            this.ansName.push("RedIdleAnim");
            this.ansName.push("RedJumpAnim");
            this.ansName.push("RedDownCryAnim");
            this.ansName.push("RedLoseCryAnim");
            this.ansName.push("RedWinAnim");
        }
        if (type == 1) {
            this.ansName.push("BlueIdleAnim");
            this.ansName.push("BlueJumpAnim");
            this.ansName.push("BlueDownCryAnim");
            this.ansName.push("BlueLoseCryAnim");
            this.ansName.push("BlueWinAnim");
        }

    },
    _init : function(){
        this.indexes = 0;
        this.addY = 0;
        this.node.setPosition(this.actorsNode[0].x,this.actorsNode[0].y+this.addY);
        this.inputTag = true;
        // 一步两步的 基本记录数组位置的索引
        this.indexes = 0;
        //记录所有操作 索引的数组
        this.Records = [];
        //按了多少次
        this.Recordsindex = 0;

        this.Records.push(this.indexes);

        this.player = UtilsNode.getNode("player",this.node);




        this.gameOverTag = true;

    },
    addOne : function () {
        if (!this.gameOverTag) {
            return;
        }
        if (!this.inputTag) {
            return;
        }
        let temp = this.indexes;
        this.Records.push(temp);
        this.indexes++;
        app.bgm.playha();
        this.playerSetPosition();
    },
    addTwo : function () {
        if (!this.gameOverTag) {
            return;
        }
        if (!this.inputTag) {
            return;
        }
        let temp = this.indexes;
        this.Records.push(temp);
        this.indexes+=2;
        app.bgm.playhei();
        this.playerSetPosition();
    },
    addAuto : function () {
        if (!this.gameOverTag) {
            return;
        }
        if (!this.inputTag) {
            return;
        }
        let temp = this.indexes;
        this.Records.push(temp);

        let actor = this.actorsNode[this.indexes+1].getComponent("actor");
        if (actor.type == 2) {
            let x = UtilsNode.getRandom(2,0,10);

            if (x < 8) {
                this.indexes+=2;
            }else{
                this.indexes+=1;
            }

        }else{
            this.indexes+=1;
        }
        this.playerSetPosition();
    },
    playerSetPosition : function () {
        if (this.indexes >= this.actorsNode.length-1) {
            this.indexes = this.actorsNode.length-1;

        }
        this.checkType();
        // console.log("记录数组日常遍历",this.Records);



        var finished = cc.callFunc(function(target, score) {

            // app.camera.y = this.node.y-640;
            // var myAction = cc.sequence(cc.moveTo(1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y)))


            // console.log("镜头现在高度",app.camera.y);
            // console.log("玩家现在高度",this.node.y);

            if (this.callBackCameraMove!=null) {
                this.callBackCameraMove(this.node);
            }
            if (this.callBackMove!=null) {
                this.callBackMove(this.indexes);
            }
            //播放动画
            this.playAnEnd();
            this.playAnEnd_bg();

            if (this.indexes==8) {
                if (this.callBackInfo) {
                    this.callBackInfo();
                }
            }


            // setTimeout(function () {
            //     this.inputTag = true;
            // }.bind(this),500);
            // console.log("现在的位置",this.node);
        }, this);//动作完成后会给玩家加100分
        // var myAction = cc.sequence(cc.jumpTo(1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y)), finished);
        var myAction = cc.sequence(
            cc.jumpTo(0.1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY-20), 200, 1),
            cc.moveTo(0.05,cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY+10)),
             finished);
        // this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y);
        this.node.runAction(myAction);
        this.inputTag = false;
        this.playAnStart();
    },
    playAnStart : function(){
        let actor = this.actorsNode[this.indexes].getComponent("actor");
        let an = this.player.getComponent(cc.Animation);
        let Clips = an.getClips();
        console.log("当前动画列表",Clips);
        // console.log("地板什么类型的",actor.type);
        an.play(this.ansName[1]);
        // switch (actor.type) {
        //     case  2:
        //         if (this.actorsNode[this.indexes].userData==null ) {
        //             let data = {type : true};
        //             this.actorsNode[this.indexes].userData = data;
        //             //要掉下去了
        //             an.play();
        //         }
        //
        //         break;
        //     case  0:
        //     case  1:
        //         an.play();
        //         break;
        //     case  3:
        //         an.play();
        //         break;
        // }
    },
    playAnEnd : function(){
        let actor = this.actorsNode[this.indexes].getComponent("actor");
        let an = this.player.getComponent(cc.Animation);
        let Clips = an.getClips();
        console.log("地板什么类型的",actor.type);
        let temp = app.bg.actorps[this.indexes];
        switch (actor.type) {
            case  2:
                // if (this.actorsNode[this.indexes].userData==null ) {
                //     let data = {type : true};
                //     this.actorsNode[this.indexes].userData = data;
                //     //要掉下去了
                //     an.play();
                // }
                an.play(this.ansName[2]);
                break;
            case  0:
            case  1:
                an.play();


                let temp1 = app.bg.actorps[this.indexes-1];
                console.log("落地之后这个是什么类型的",temp,"上个类型",temp1);
                if (temp.q != null || temp1.q!=null) {
                    this.node.setScale(1,-1);
                    this.node.setRotation(180);
                    console.log("我应该脸往左",temp,temp1);
                }
                if (temp.w!= null || temp1.w!=null) {
                    this.node.setScale(1,1);
                    this.node.setRotation(0);
                    console.log("我应该脸往右",temp,temp1);
                }
                if (this.callBackJump!=null) {
                    this.callBackJump(temp);
                }
                break;
            case  3:
                if (this.callBackJump!=null) {
                    this.callBackJump(temp);
                }
                an.play(this.ansName[4]);
                break;
        }
    },



    showsSmoke : function(b){
        let node = UtilsNode.getNode("qi",this.node);
        node.getComponent("kill").setActive(b);
    },

    playAnEnd_bg : function(){
        let actor = this.actorsNode[this.indexes].getComponent("actor");
        let an = this.actorsNode[this.indexes].getComponent("itemmanager");
        this.showsSmoke(true);

        console.log("地板什么类型的",actor.type);
        switch (actor.type) {
            case  2:
                if (this.actorsNode[this.indexes].userData==null ) {
                    let data = {type : true};
                    this.actorsNode[this.indexes].userData = data;
                    //要掉下去了
                    an.play();
                }
                an.playwater();
                break;
            case  0:
            case  1:
                an.play();
                break;
            case  3:
                an.play();
                break;
        }
    },


    startGame : function(b){
        this.gameOverTag = b;
    },

    //检查地板类型
    checkType : function () {
       let actor = this.actorsNode[this.indexes].getComponent("actor");
        let an = this.player.getComponent(cc.Animation);
        let Clips = an.getClips();
        console.log("地板什么类型的",actor.type);
        switch (actor.type) {
            case  2:
                //要掉下去了
                if (this.callBackTrap!=null) {
                    this.callBackTrap();
                }
                this.inputTag = false;
                // console.log("记录数组陷阱",this.Records);
                setTimeout(function () {
                    this.indexes = this.Records[this.Records.length-1];
                    this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY);
                    UtilsNode.getNode("player",this.node).setPosition(0,0);
                    this.inputTag = true;
                    an.play(this.ansName[0]);
                    this.playAnEnd_bg();
                }.bind(this),1500);
                break;
            case  0:
            case  1:
                this.inputTag = false;
                // console.log("记录数组陷阱",this.Records);
                setTimeout(function () {
                    // this.indexes = this.Records[this.Records.length-1];
                    // this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y);
                    this.inputTag = true;
                    an.play(this.ansName[0]);
                }.bind(this),200);
                break;
            case  3:
                setTimeout(function () {
                    if (this.callBackGameOver != null) {
                        this.callBackGameOver();
                    }
                    // an.play(this.ansName[4]);
                    this.winOrLose(true);
                    // actor.setGameOverShow(false);
                    let star = UtilsNode.getNode("star",this.actorsNode[this.indexes]);
                    star.getComponent("endstart").endGame();

                }.bind(this),200);
                break;
        }
    },

    winOrLose : function(b){
        this.gameOverTag = false;
        let an = this.player.getComponent(cc.Animation);
        if (b) {
            an.play(this.ansName[4]);
        }else{
            an.play(this.ansName[3]);
        }

    },


    //todo 回调区域------------------------------------------------------


    callBackGameOver : function(){},
    setCallBackGameOver : function (callBackGameOver) {
        this.callBackGameOver = callBackGameOver;
    },

    callBackTrap : function(){},
    setCallBackTrap : function (callBackTrap) {
        this.callBackTrap = callBackTrap;
    },
    callBackInfo : function(){},
    setCallBackInfo : function (callBackInfo) {
        this.callBackInfo = callBackInfo;
    },
    callBackJump : function(){},
    setCallBackJump : function (callBackJump) {
        this.callBackJump = callBackJump;
    },
    callBackCameraMove : function(){},
    setCallBackCameraMove : function (callBackCameraMove) {
        this.callBackCameraMove = callBackCameraMove;
    },
    callBackMove : function(){},
    setCallBackMove : function (callBackMove) {
        this.callBackMove = callBackMove;
    },

    // update (dt) {},
});
