// 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: {
     // index : 0

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        player = this
        this.scales = 1;
        //管理器
        this.dragonBonesManager = this.getComponent("dragonBonesManager")
        this.dragonBonesManager.frame(function (event) {
            // if (event.type === dragonBones.EventObject.COMPLETE) {
            //     if (event.animationState.name === "attack") {
            //         Debug.Log("attack 动作播放完毕！！！");
            //         //TODO:
            //     }
            // }
            // Log.info("玩家是什么",event.type,event.animationState.name,event.name ,event.data,"现在状态",this.status )
            this.playState(this.status)
        }.bind(this));



        this.initListener()
        this._init()
    },

    _init(){
        //-1 平常 0 防御 1 左面攻击 2 右面攻击 3左面躲闪  4右面躲闪
        this.status = -1
        // var l= this.life.getComponent("lifeManager")
        //
        // Log.info("lifeManager",l.getLife())

        // Log.info("现在血量管理",lifes[0].getLife())



        // Log.info("所有动画名字 玩家",this.dragonBonesManager._init())

        // this.dragonBonesManager.play(this.dragonBonesManager._init()[0],function (type) {
        //      Log.info("播放完毕 type",this.index)
        //      // Log.info("播放完毕 listener",listener)
        //      // Log.info("播放完毕 target",target)
        //  }.bind(this),1)


    },
    initListener(){
        rival.setActionCallback(function (status) {
            Log.info("现在对手状态是",status)
            this.rivalStatus = status
            switch (status) {
                case 0:

                    break;
                case 1:
                    if (this.status != 4 && this.status != 0) {
                        this.takeaBeating()
                        lifes[0].addLife(strike.ATK, function () {
                            Log.info("玩家 输了")
                            gameManager.gameOver(1)
                        }.bind(this))
                        // Log.info("现在血量",lifes[1].getLife())
                    } else {
                        if (this.status == 0) {
                            Log.info("现在是 破防")
                            this.atkDefendPow(null, function () {
                                this.takeaBeating()
                                lifes[0].addLife(strike.ATK, function () {
                                    Log.info("玩家 输了")
                                    gameManager.gameOver(1)
                                }.bind(this))
                            }.bind(this))
                        }
                    }
                    break;
                case 2:
                    if (this.status != 3 && this.status != 0) {
                        this.takeaBeating()
                        lifes[0].addLife(strike.ATK, function () {
                            Log.info("玩家 输了")
                            gameManager.gameOver(1)
                        }.bind(this))
                        // Log.info("现在血量",lifes[1].getLife())
                    } else {
                        if (this.status == 0) {
                            // Log.info("现在防御住了")
                            // this.atkDefend(function () {
                            //     this.strike_leftGetThrashing()
                            // }.bind(this))
                            Log.info("现在是 破防")
                            this.atkDefendPow(null, function () {
                                this.takeaBeating()
                                lifes[0].addLife(strike.ATK, function () {
                                    Log.info("玩家 输了")
                                    gameManager.gameOver(1)
                                }.bind(this))
                            }.bind(this))
                        }
                    }
            }
        }.bind(this))
    },
    playState(state){
        rival.beAttacked(state,strike.playerATKData)
    },
    atkDefendPow(data, callback) {
        // if (data.power >= 80) {
        //     if (callback) {
        //         //破防
        //         callback();
        //         this.initDefend();
        //     }
        // }
        lifes[0].addDefendLife(-20,callback)
    },

    playLeft(data){
        if (!this.check()) {
            return
        }
        this.status = 1
        strike.playerATKData = data
        rival.readyATK(data)

        this.node.scale =new cc.Vec2(this.scales, this.scales);

        this.dragonBonesManager.play(this.dragonBonesManager._init()[1],function (type) {

            this.idle()
        }.bind(this),1)
    },
    playMid(data){
        if (!this.check()) {
            return
        }
        strike.playerATKData = data
        this.status = 0
        this.node.scale =new cc.Vec2(this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[2],function (type) {
            this.idle()
        }.bind(this),1)
    },
    playRight(data){
        if (!this.check()) {
            return
        }
        strike.playerATKData = data
        rival.readyATK(data)
        this.status = 2
        this.node.scale =new cc.Vec2(-this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[1],function (type) {
            this.idle()
        }.bind(this),1)
    },
    // //右面挨打
    // strike_rightGetThrashing(){
    //     this.node.scale =new cc.Vec2(0.5, 0.5);
    //     this.dragonBonesManager.play(this.dragonBonesManager._init()[3],function (type) {
    //         this.idle()
    //     }.bind(this),1)
    // },
    // //左面挨打
    // strike_leftGetThrashing(){
    //     this.node.scale =new cc.Vec2(-0.5, 0.5);
    //     this.dragonBonesManager.play(this.dragonBonesManager._init()[3],function (type) {
    //         this.idle()
    //     }.bind(this),1)
    // },
    //右面躲闪
    strike_rightMiss(data){
        if (!this.check()) {
            return
        }
        this.status = 4
        strike.playerATKData = data
        this.node.scale =new cc.Vec2(-this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[3],function (type) {
            this.idle()
        }.bind(this),1)
    },
    //左面躲闪
    strike_leftMiss(data){
        if (!this.check()) {
            return
        }
        this.status = 3
        strike.playerATKData = data
        this.node.scale =new cc.Vec2(this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[3],function (type) {
            this.idle()
        }.bind(this),1)
    },


    idle(){
        this.node.scale =new cc.Vec2(this.scales, this.scales);
        this.dragonBonesManager.playNotEvent(this.dragonBonesManager._init()[0],0)
        this.status = -1
    },
    check(){
        var b = false
        if (this.status == -1) {
            b = true
        }
        return b
    },
    takeaBeating(){
        game.startAn();
    }

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