const GameStatesStatic = require('GameStates');
const PlayerState = require('PlayerState');
var BarrierSuperClass = require("BarrierSuper");
cc.Class({
    extends: BarrierSuperClass,

    properties: {
        readyRunTag: 0,
        //当前进行的项目
        type: 0,
        Canvas: {
            default: null,
            type: cc.Node
        },
    },

    onLoad: function () {

        // var self = this;
        // cc.eventManager.addListener({
        //     event: cc.EventListener.TOUCH_ALL_AT_ONCE,
        //     onTouchesBegan () {
        //         self.toggleTimeScale();
        //     }
        // }, this.node);
    },

    start: function () {
        var spine = this.spine = this.getComponent('sp.Skeleton');
        // cc.log("现在 播放 骨骼动画对象", spine);
        this._hasStop = true;
        // this.setType();
        this.initData();
        // cc.log(this.node.parent.name);
        if (this.node.parent.name == "javetemp_Hero") {
            var HeroControl = cc.find("Hero").getComponent("HeroControl");
            HeroControl.playAudioByName("JavelinFlying");//播放标枪飞的声音
        }
        // this.getSpeedLv();

        // cc.log("当前游戏", this.Canvas.getComponent("GameStates").CurrentProgress);
        // this.javelin();
        // this.spine.setAnimation(0, "langan", true);
        // this.walk();


        // .setStartListener()用来设置开始播放动画的事件监听。

    },

    setListener: function () {
        var spine = this.spine = this.getComponent('sp.Skeleton');
        spine.setStartListener(function (trackEntry) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("[track %s][animation %s] start.", trackEntry.trackIndex, animationName);
            // cc.log("现在 播放 的动画名字是", animationName);

            if (animationName == this.arr[this.type][this.walkLv]) {
                if (this.readyRunTag == 0) {
                    // cc.log("走着");
                    // this.player.getComponent("playerlinkagebg").readyRun(this.walkLv);
                }
                this.readyRunTag++;
                // this._hasStop = false;
            }
            this.setStatus(animationName);
        }.bind(this));
        // .setInterruptListener()用来设置动画被打断的事件监听。
        spine.setInterruptListener(function (trackEntry) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("[track %s][animation %s] interrupt.", trackEntry.trackIndex, animationName);

            if (this.interruptListener != null) {
                this.interruptListener(animationName);
                // cc.log("现在 打断 的动画名字是", animationName);
                // this.spine.timeScale = 1;
            }
        }.bind(this));
        // .setEndListener()用来设置动画播放完后的事件监听。
        spine.setEndListener(function (trackEntry) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("[track %s][animation %s] end.", trackEntry.trackIndex, animationName);

            // cc.log("现在 停止 的动画名字是", animationName);
            if (animationName == this.statusArr[6] || animationName == this.statusArr[5]) {

                // this._hasStop = true;
            }
            if (this.endListener != null) {

                this.endListener(animationName);
                // this.spine.timeScale = 1;
            }


        }.bind(this));
        // .setDisposeListener()用来设置动画将被销毁的事件监听。
        spine.setDisposeListener(function (trackEntry) {

            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("[track %s][animation %s] will be disposed.", trackEntry.trackIndex, animationName);
        }.bind(this));
        // .setCompleteListener用来设置动画播放一次循环结束后的事件监听。
        spine.setCompleteListener(function (trackEntry, loopCount) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // if (animationName === 'attack') { //如果当前为射击状态,立马清除
            //     // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
            //     this.spine.clearTrack(1);
            // }
            // cc.log("[track %s][animation %s] complete: %s", trackEntry.trackIndex, animationName, loopCount);
        }.bind(this));
        // .setEventListener()用来设置动画播放过程中帧事件的监听。
        spine.setEventListener(function (trackEntry, event) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
        }.bind(this));
    },


    initData: function () {
        //初始化 动作列表
        this.statusArr = [
            //0
            "none",
            //1
            "idle",
            //2
            "idle-run",
            //3
            "run-01",
            //4
            "run-02",
            //5
            "run-03",
            //6
            "run-04",
            //7
            "run-05",
            //8
            "hurdling_1",
            //9
            "hurdling_2",
            //10
            "longjump-01",
            //11
            "longjump-02",

            "bybike_1",

            "bybike_2",

            "bybike_3",

            "bybike_4",

            "bybike_5",

            "bybike_5bike",

            "bybike_5boy",

            "bybike_redy",

            "bybike_redy_bike",

            "bybike_redy_boy",

            "Biaoqiang01",
            "Biaoqiang02",
            "Biaoqiang03",

            "javelin_1",
            "javelin_2",
            "javelin_2a",
            "javelin_3",
            "javelin_3a",
            "javelin_4",
            "javelin_5",
            "javelin_redy"

        ];
        //todo 准备跑步动作
        this.statusArr_redyrun = [
            //0
            "idle",
            //1
            "idle-run",

        ];

        // Race:0,
        // Hurdle:1,
        // Javelin:2,
        // LongJump:3,
        // RideBike:4,
        this.arr = [];

        //todo 跑步动作
        this.statusArr_run = [
            //0
            "run-01",
            //1
            "run-02",
            //2
            "run-03",
            //3
            "run-05",
            //4
            "run-04",
        ];

        this.arr.push(this.statusArr_run);


        //todo 跳远
        var a = [];

        this.statusArr_longjump = a.concat(this.statusArr_run);
        //5
        this.statusArr_longjump.push("longjump-01");
        //6
        this.statusArr_longjump.push("longjump-02");

        this.arr.push(this.statusArr_longjump);

        //todo 跨栏
        a = [];

        this.statusArr_hurdling = a.concat(this.statusArr_run);
        //5
        this.statusArr_hurdling.push("hurdling_1");
        //6
        this.statusArr_hurdling.push("hurdling_2");

        this.arr.push(this.statusArr_hurdling);

        //todo 蹬三轮
        this.statusArr_bike = [
            //0
            "bybike_1",
            //1
            "bybike_2",
            //2
            "bybike_3",
            //3
            "bybike_4",
            //4
            "bybike_5",
            //5
            "bybike_5bike",
            //6
            "bybike_5boy",
            //7
            "bybike_redy",
            //8
            "bybike_redy_bike",
            //9
            "bybike_redy_boy",
        ];

        this.arr.push(this.statusArr_bike);

        //todo 标枪项目
        this.statusArr_javelin = [
            //0
            "javelin_1",
            //1
            "javelin_2",
            //2
            "javelin_2a",
            //3
            "javelin_3",
            //4
            "javelin_3a",
            //5
            "javelin_4",
            //6
            "javelin_5",
            //7
            "javelin_redy"
        ];

        this.arr.push(this.statusArr_javelin);

        // cc.log("所有动作", this.arr);



        //todo 标枪
        this.statusArr_biaoqiang = [
            "Biaoqiang",
            "Biaoqiang02",
            "Biaoqiang03",
        ];






        //初始化动作状态
        this.setStatus(this.statusArr[1]);
        //初始化跳跃状态
        // this.setJumpTag(0);
        //设置当前没有 任何加速 机会
        this.setMyTag(null);
    },
    setMyTag: function (obj) {
        this.myTag = obj;
    },

    idle: function () {
        this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
    },


    walk: function () {
        // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
        this.spine.setAnimation(0, this.statusArr[3], true);
    },

    readyRun: function () {
        // this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
        // this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
        this.spine.setAnimation(0, this.statusArr_run[0], true);
        //奔跑等级
        this.walkLv = 0;

    },
    stagger: function () {
        // cc.log("帅哦到了",this.arr[this.type][3]);
        // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
        this.spine.setAnimation(0, this.arr[this.type][3], false);
        // this.setCurrentSpeed();
    },
    longjump: function () {
        // this.spine.timeScale = 1.5;
        this.spine.setAnimation(0, this.statusArr_longjump[5], false);
        setTimeout(function () {
            this.spine.setAnimation(0, this.statusArr_longjump[6], false);

            if (this.timeOutLongJumpListener != null) {
                this.timeOutLongJumpListener();
            }

            // this.spine.addAnimation(0, this.statusArr_longjump[this.walkLv], true,0.5);
        }.bind(this), 1000);

        setTimeout(function () {
            this.paused();
        }.bind(this), 1500);
        setTimeout(function () {
            this.paused();
            // this.setCurrentSpeed();
        }.bind(this), 2500);

    },

    timeOutLongJumpListener: function () {

    },
    setTimeOutLongJumpListener: function (timeOutLongJumpListener) {
        this.timeOutLongJumpListener = timeOutLongJumpListener;
    },

    longjump_stagger: function () {
        this.spine.setAnimation(0, this.statusArr_longjump[1], false);
        // this.setCurrentSpeed();
    },
    hurdling: function () {
        this.spine.setAnimation(0, this.statusArr_hurdling[5], false);
        this.spine.addAnimation(0, this.statusArr_hurdling[this.walkLv], true);
        // this.setCurrentSpeed();
    },
    hurdling_stagger: function () {
        this.spine.setAnimation(0, this.statusArr_hurdling[1], false);
        // this.setCurrentSpeed();
    },


    //单纯的停止
    stop: function () {
        // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
        this.spine.clearTrack(0);
        this.setStatus(this.statusArr_redyrun[0]);
    },

    //调用一次  暂停  再调用  恢复
    paused: function () {
        // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
        if (this.spine.paused) {
            this.spine.paused = false;
        } else {
            this.spine.paused = true;
        }
    },

    /**
     * 设置当前的运动状态
     * @param status
     */
    setStatus: function (status) {
        this.myStatus = status;
        // cc.log("设置当前播放动画是",status);
        // this.player.getComponent("playerlinkagebg").upDatawalkLv(this.walkLv);
    },
    //按屏幕了
    setOnTouchListener: function () {
        // this.jump();
        // this.toggleTimeScale();
        // cc.log("什么名字",this.spine.animation);

        // cc.log("当前游戏 按屏幕了", this.Canvas.getComponent("GameStates").CurrentProgress);
        if (this.spine.animation == this.statusArr_redyrun[0]) {
            switch (this.type) {
                case 0:
                    this.readyRun();
                    break;
                case 1:
                    this.upBike();
                    break;
            }
            return;
        }
        this.setType();
        if (this._hasStop) {
            // this.setCurrentSpeed();
        }
    },
    //todo 跨栏 跳跃
    setOnHurdlingListener: function () {
        this.setType();
        // if (this._hasStop) {
        //     this.speedUp();
        // }
        this.hurdling();
    },
    //todo 跨栏 踉跄
    setOnHurdlingStaggerListener: function () {
        this.setType();
        this.hurdling_stagger();
    },
    //todo 跳远跳跃
    setOnLongJumpListener: function () {
        this.setType();
        // if (this._hasStop) {
        //     this.speedUp();
        // }
        this.longjump();
    },
    //todo 跳远 踉跄
    setOnLongJumpStaggerListener: function () {
        this.setType();
        this.longjump_stagger();
    },
    //摔倒了
    setOnTripListener: function () {
        this.setType();
        // this.jump();
        // this.toggleTimeScale();

        this.stagger();
        // this.speedDown();
        //
        // this.upBike();


    },

    toggleTimeScale: function () {
        // .timeScale当前骨骼中所有动画的时间缩放率。
        if (this.spine.timeScale === 1.0) {
            this.spine.timeScale = 0.3;
        } else {
            this.spine.timeScale = 1.0;
        }
    },
    setEndListener: function (endListener) {
        this.endListener = endListener
    },
    endListener: function () {

    },
    setInterruptListener: function (interruptListener) {
        this.interruptListener = interruptListener
    },
    interruptListener: function () {

    },
    //todo 快上车 没时间解释了
    upBike: function () {
        this.setType();
        // this.spine.setAnimation(0, this.statusArr_run[0], false);
        this.spine.setAnimation(0, this.statusArr_bike[9], false);
        this.spine.addAnimation(0, this.statusArr_bike[0], true);
        // this.setStatus(this.statusArr[this.walkLv]);
        this.walkLv = 0;

    },
    //todo 快拿标枪 没时间解释了
    upJavelin: function () {
        this.setType();
        // this.spine.setAnimation(0, this.statusArr_run[0], false);
        this.spine.setAnimation(0, this.statusArr_javelin[0], true);
        this.walkLv = 0;
        // this.setStatus(this.statusArr[this.walkLv]);
        // this.setCurrentSpeed();
    },
    //todo 快扔标枪 没时间解释了
    upJavelin_go: function () {
        this.setType();
        // this.spine.setAnimation(0, this.statusArr_run[0], false);
        this.spine.setAnimation(0, this.statusArr_javelin[6], false);
        this.walkLv = 0;
        // this.setStatus(this.statusArr[this.walkLv]);
        // this.setCurrentSpeed();
    },
    //todo 标枪 没时间解释了
    javelin: function (data, starListener, endListener) {
        // var spine = this.spine = this.getComponent('sp.Skeleton');

        this.initData();
        this.setListener();

        var actionBy = cc.jumpBy(data.duration, data.position, data.y, data.height, data.jumps);
        // var actionrotateBy = cc.rotateBy(data.duration,60);
        // this.node.runAction(actionrotateBy);
        //设置标枪动画
        //  tPrefab.getChildByName("jave").getComponent('sp.Skeleton').timeScale = 1/(data.duration*0.3);
        this.spine.timeScale = 1 / (data.duration * 0.3);
        
        var start = cc.callFunc(function () {
            if (starListener != null) {
                // this.spine.timeScale = 0.2;
                starListener();
            }
        }.bind(this), this);

        var stop = cc.callFunc(function () {
            if (endListener != null) {
                endListener();
                // this.spine.timeScale = 1;
            }
        }.bind(this), this);
        var myAction = cc.sequence(start, actionBy, stop);
        this.node.runAction(myAction);
        // var anim = this.getComponent(cc.Animation);
        // cc.log("要播放动画",this.spine);
        // anim.play();
        // this.setType();
        // this.spine.setAnimation(0, this.statusArr_run[0], false);
        this.spine.clearTrack();
        // this.spine.setAnimation(0, this.statusArr_biaoqiang[0], false);
        // this.spine.addAnimation(0, this.statusArr_biaoqiang[1], false);
        // this.spine.addAnimation(0, this.statusArr_biaoqiang[2], false);
        this.walkLv = 0;
        // this.setStatus(this.statusArr[this.walkLv]);
        // this.setCurrentSpeed();
    },




    setType: function () {
        var temp = this.Canvas.getComponent("GameStates").CurrentProgress;


        switch (temp) {
            case GameStatesStatic.GameProgress.Race:
                this.type = 0;
                // cc.log("当前的typssssse","s1");
                break;
            case GameStatesStatic.GameProgress.LongJump:
                this.type = 1;
                // cc.log("当前的typssssse","s2");
                break;
            case GameStatesStatic.GameProgress.Hurdle:
                this.type = 2;
                // cc.log("当前的typssssse","s3");
                break;
            case GameStatesStatic.GameProgress.RideBike:
                this.type = 3;
                // cc.log("当前的typssssse","s4");
                break;
            case GameStatesStatic.GameProgress.Javelin:
                this.type = 4;
                // cc.log("当前的typssssse","s5");
                break;
        }


    },
    getSpeedLv: function () {
        var lv = this.Canvas.getComponent("NodeTouch").Hero.getComponent("HeroControl").PlayerStateScript.CurrentSpeed;
        // PlayerState.SlowSpeed
        // PlayerState.MiddleSpeed
        // PlayerState.HightSpeed
        // cc.log("现在是什么啊",PlayerState);
        // cc.log("当前速度",lv);
        var state = PlayerState.SpeedRangeForAnimation;
        if (lv >= state.SlowSpeed[0] && lv <= state.SlowSpeed[1]) {

            return 0;
        }
        if (lv >= state.MiddleSpeed[0] && lv <= state.MiddleSpeed[1]) {
            return 1;
        }
        if (lv >= state.HightSpeed[0] && lv <= state.HightSpeed[1]) {
            return 2;
        }




        return 0;
    }


    // start () {
    //
    // },

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

