// 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 () {
        rival = this


        this.An = this.node.getComponent(cc.Animation)


        this.Animations = utils.getAnimations(this.An);
        Log.info("所有表演",this.Animations)

        setTimeout(function () {
            this.initListener()
        }.bind(this),500)


        this.loopTimeFunc();
    },
    loopTimeFunc(){
        this.loopTime = this.node.getComponent("loopTime")
        this.loopTime.startLoop();
        this.loopTime.setStartLoopListener(function () {
        //    每隔多少秒的回调
           var random  = UtilsNode.getRandom(2,0,2)
            this.setAn(random);
        }.bind(this));
    },

    setAn(index){
        this.playLeft();
    },


    initListener(){
        player.setLeftokListener(function () {
            this.playLeft_bad();
        }.bind(this))
        player.setRightokListener(function () {
            this.playRight_bad();
        }.bind(this))
    },

    // leftok(b){
    //     Log.info("打到了",b)
    //     if (this.LeftokListener) {
    //         this.LeftokListener();
    //     }
    // },
    // //左侧击打监听
    // setLeftokListener(LeftokListener){
    //     this.LeftokListener = LeftokListener;
    // },
    // initListener(){
    //     utils.setAnimationCallBack(this.An,"finished",,function () {
    //         Log.info("现在回来的是什么")
    //     }.bind(this));
    // },

    playLeft_bad(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[1].name);
            // this.initListener();
            utils.setAnimationCallBack(this.An,"finished",this.Animations[1].name,function () {
                // Log.info("现在回来的是什么",this.Animations[1].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playLeft(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[3].name);
            // this.initListener();
            utils.setAnimationCallBack(this.An,"finished",this.Animations[3].name,function () {
                // Log.info("现在回来的是什么",this.Animations[1].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playRight_bad(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[2].name);
            utils.setAnimationCallBack(this.An,"finished",this.Animations[2].name,function () {
                // Log.info("现在回来的是什么",this.Animations[2].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playRight(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[2].name);
            utils.setAnimationCallBack(this.An,"finished",this.Animations[2].name,function () {
                // Log.info("现在回来的是什么",this.Animations[2].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playMid(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[3].name);
            utils.setAnimationCallBack(this.An,"finished",this.Animations[3].name,function () {
                // Log.info("现在回来的是什么",this.Animations[3].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playLeftBad(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[4].name);
            utils.setAnimationCallBack(this.An,"finished",this.Animations[4].name,function () {
                // Log.info("现在回来的是什么",this.Animations[3].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },
    playRightBad(){
        this.nowPlaying(function () {
            utils.startAnimation(this.An,this.Animations[5].name);
            utils.setAnimationCallBack(this.An,"finished",this.Animations[5].name,function () {
                // Log.info("现在回来的是什么",this.Animations[3].name)
                this.setAn_normal();
            }.bind(this));
        }.bind(this));

    },

    setAn_normal(){
        utils.startAnimation(this.An,this.Animations[0].name);
    },
    nowPlaying(callback){

        // Log.info("当前播放的是",this.An.currentClip ,this.Animations[0].name)
        if (this.An.currentClip == null || this.An.currentClip.name ==this.Animations[0].name ) {
            if (callback) {
                callback();
            }
        }
    }
    // update (dt) {},
});
