// 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
        //-1 平常 0 防御 1 左面攻击 2 右面攻击 3左面躲闪  4右面躲闪 5左面挨打 6 右面挨打

        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._init()


    },
    _init(){
        this.status = -1

        // Log.info("所有动画名字 对手", this.dragonBonesManager._init())
        this.scales = 1;
        this.times = 0.7;

        // this.test()
        //破防数值
        this.initDefend();
        //开始表演
        this.startAuto()


    },
    //开始 自动动作
    startAuto() {
        // this.dragonBonesManager.timeScale(this.times)
        // clearTimeout(this.defendTime)
        this.unschedule(this.auto)
        var time = UtilsNode.getRandom(2, 1, 2)
        // Log.info("进来时间",time)
        this.scheduleOnce(this.auto, time)
    },
    stopAuto() {
        this.unschedule(this.auto)
    },
    auto() {
        switch (UtilsNode.getRandom(2, 1, 2)) {
            case 0:
                // this.strike_defend()
                break;
            case 1:
                this.strike_right()
                break;
            case 2:
                this.strike_left()
                break;
        }
    },
    //减少破防数值 (次数)
    atkDefend(callback) {
        this.defend--
        if (this.defend <= 0) {
            if (callback) {
                //破防
                callback();
                this.initDefend();
            }
        }
    },
    //减少破防数值 (力量)
    atkDefendPow(data, callback) {
        // if (data.power >= 80) {
        //     if (callback) {
        //         //破防
        //         callback();
        //         this.initDefend();
        //     }
        // }
        lifes[1].addDefendLife(-(data.power/2),callback)
    },
    initDefend() {
        this.defend = 3;
    },

    //被攻击
    beAttacked(state, data) {
        Log.info("现在力量是", data.power)
        switch (state) {
            case 0:

                break;
            //    玩家 左拳   需要右闪避
            case 1:
                if (this.status != 4 && this.status != 0) {
                    this.strike_rightGetThrashing()
                    lifes[1].addLife(-(data.power/2), function () {
                        Log.info("玩家 赢了")
                        gameManager.gameOver(0)
                    }.bind(this))
                    // Log.info("现在血量",lifes[1].getLife())
                } else {
                    if (this.status == 0) {
                        Log.info("现在是 破防")
                        this.atkDefendPow(data, function () {
                            this.strike_rightGetThrashing()
                            lifes[1].addLife(-(data.power/2), function () {
                                Log.info("玩家 赢了")
                                gameManager.gameOver(0)
                            }.bind(this))
                        }.bind(this))
                    }
                }
                break;
            case 2:
                if (this.status != 3 && this.status != 0) {
                    this.strike_leftGetThrashing()
                    lifes[1].addLife(-(data.power/2), function () {
                        Log.info("玩家 赢了")
                        gameManager.gameOver(0)
                    }.bind(this))
                    // Log.info("现在血量",lifes[1].getLife())
                } else {
                    if (this.status == 0) {
                        Log.info("现在是 破防")
                        this.atkDefendPow(data, function () {
                            this.strike_leftGetThrashing()
                            lifes[1].addLife(-(data.power/2), function () {
                                Log.info("玩家 赢了")
                                gameManager.gameOver(0)
                            }.bind(this))
                        }.bind(this))
                    }
                }

                break;
        }
    },


    playState(state) {
        if (this.actionCallback) {
            this.actionCallback(this.status)
        }
        switch (state) {
            case 0:

                break;
            case 1:
                // rival.strike_rightGetThrashing()
                // lifes[1].addLife(strike.ATK,function () {
                //     Log.info("玩家 赢了")
                // }.bind(this))
                // Log.info("现在血量",lifes[1].getLife())
                break;
            case 2:
                // rival.strike_leftGetThrashing()
                // lifes[1].addLife(strike.ATK,function () {
                //     Log.info("玩家 赢了")
                // }.bind(this))
                // Log.info("现在血量",lifes[1].getLife())
                break;
        }
    },
    // whereStrike_right (){
    //     this.strike_right()
    //     setTimeout(function () {
    //
    //         this.whereStrike_right()
    //     }.bind(this),3000)
    // },
    whereStrike_rightMiss() {
        this.strike_rightMiss()
        setTimeout(function () {

            this.whereStrike_leftMiss()
        }.bind(this), 1000)
    },
    whereStrike_leftMiss() {
        this.strike_leftMiss()
        setTimeout(function () {

            this.whereStrike_strike_defend()
        }.bind(this), 1000)
    },
    whereStrike_strike_defend() {
        this.strike_defend()
        setTimeout(function () {
            this.dragonBonesManager.timeScale(this.times)
        }.bind(this), 3000)
        setTimeout(function () {
            this.whereStrike_rightMiss()
        }.bind(this), 5000)
    },

    test() {
        setTimeout(function () {
            this.strike_right()
        }.bind(this), 1000)
        setTimeout(function () {
            this.strike_left()
        }.bind(this), 3000)
        setTimeout(function () {
            this.strike_defend()
        }.bind(this), 5000)
        setTimeout(function () {
            this.strike_rightGetThrashing()
        }.bind(this), 7000)
        setTimeout(function () {
            this.strike_leftGetThrashing()
        }.bind(this), 9000)
        setTimeout(function () {
            this.strike_rightMiss()
        }.bind(this), 11000)
        setTimeout(function () {
            this.strike_leftMiss()
        }.bind(this), 13000)
    },

    //右拳
    strike_right() {
        if (!this.check()) {
            return
        }
        this.status = 2
        if (this.actionCallback) {
            this.actionCallback(this.status)
        }

        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_left() {
        if (!this.check()) {
            return
        }
        this.status = 1
        if (this.actionCallback) {
            this.actionCallback(this.status)
        }
        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_defend() {
        if (!this.check()) {
            return
        }
        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)
        //
        // this.defendTime =
        // setTimeout(function () {
        //     if (this.status == 0) {
        //         this.dragonBonesManager.timeScale(0)
        //         this.defendTime = setTimeout(function () {
        //             if (this.status == 0) {
        //                 this.dragonBonesManager.timeScale(this.times)
        //             }else{
        //                 clearTimeout(this.defendTime)
        //             }
        //         }.bind(this),UtilsNode.getRandom(2,1000,2000))
        //     }else{
        //         clearTimeout(this.defendTime)
        //     }
        //
        // }.bind(this),500)


    },
    //右面挨打
    strike_rightGetThrashing() {
        // if (!this.check()) {
        //     return
        // }
        this.status = 6
        this.dragonBonesManager.timeScale(this.times)
        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_leftGetThrashing() {
        // if (!this.check()) {
        //     return
        // }
        this.status = 5
        this.dragonBonesManager.timeScale(this.times)
        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_rightMiss() {
        if (!this.check()) {
            return
        }

        this.status = 4
        if (this.actionCallback) {
            this.actionCallback(this.status)
        }
        this.node.scale = new cc.Vec2(this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[4], function (type) {
            this.idle()
        }.bind(this), 1)
    },
    //左面躲闪
    strike_leftMiss() {
        if (!this.check()) {
            return
        }
        this.status = 3
        if (this.actionCallback) {
            this.actionCallback(this.status)
        }
        this.node.scale = new cc.Vec2(-this.scales, this.scales);
        this.dragonBonesManager.play(this.dragonBonesManager._init()[4], function (type) {
            this.idle()
        }.bind(this), 1)
    },

    idle() {
        // if (this.actionCallback) {
        //     this.actionCallback(this.status)
        // }


        if (!gameManager.getGameType()) {
            this.startAuto()
        }else{
            this.stopAuto()
        }

        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
    },
    //准备攻击
    readyATK(data) {
        Log.info("现在玩家 的打拳数据", data)
        switch (data.der) {
            //右闪避
            case 1:
               switch ( UtilsNode.getRandom(2,0,3)) {
                   case 0:
                       this.strike_rightMiss()
                       break;
                   case 1:
                       this.strike_defend()
                       break;
               }

                break;
            //    左闪避
            case 3:
                switch ( UtilsNode.getRandom(2,0,5)) {
                    case 0:
                        this.strike_leftMiss()
                        break;
                    case 1:
                        this.strike_defend()
                        break;
                }
                break;


        }
    },


    setActionCallback(actionCallback) {
        this.actionCallback = actionCallback
    },
    actionCallback() {
    }

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