const GameStatesStatic = require('GameStates');
const PlayerState = require('PlayerState');

const GameProgress = cc.Enum({
    default: 0,
    Race: 1,
    LongJump: 2,
    Hurdle: 3,
    RideBike: 4,
    Javelin: 5,
});
cc.Class({
    extends: cc.Component,

    properties: {
        readyRunTag: 0,
        //当前进行的项目
        type: 0,
        Canvas: {
            default: null,
            type: cc.Node
        },
        Hero: null,
        HeroControlScript: null,
        CurrentProgress: {
            default: GameProgress.Race,
            type: cc.Enum(GameProgress)
        },
    },
    // onLoad: function () {
    //     this.Hero = this.node.parent;
    //     this.HeroControlScript = this.Hero.getComponent('HeroControl');
    // },
    setSkins: function (skinName) {
        //随机皮肤
        this.spine.setSkin(skinName);
    },
    onLoad: function () {
        this.Hero = this.node.parent;
        this.HeroControlScript = this.Hero.getComponent('HeroControl');
        
        var spine = this.spine = this.getComponent('sp.Skeleton');
        // cc.log("现在 播放 骨骼动画对象", spine);
        this._hasStop = true;

        this.setType();
        this.initData();

        this.idle();
        // .setStartListener()用来设置开始播放动画的事件监听。
        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) {

                }
                this.readyRunTag++;
                // this._hasStop = false;
            }
            this.setStatus(animationName);

        }.bind(this));
        // .setInterruptListener()用来设置动画被打断的事件监听。
        spine.setInterruptListener(function (trackEntry) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";

            if (this.interruptListener != null) {
                this.interruptListener(animationName);
            }
        }.bind(this));
        // .setEndListener()用来设置动画播放完后的事件监听。
        spine.setEndListener(function (trackEntry) {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            // cc.log("现在 停止 的动画名字是", animationName);
            if (animationName == this.statusArr[6] || animationName == this.statusArr[5]) {

            }
            if (this.endListener != null) {

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

            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
        }.bind(this));
        spine.setCompleteListener(function (trackEntry, loopCount) {

        }.bind(this));
        // .setEventListener()用来设置动画播放过程中帧事件的监听。
        spine.setEventListener(function (trackEntry, event) {

                   }.bind(this));
    },
    initData: function () {
        //初始化 动作列表
        this.statusArr = [
            //0
            "none",
            //1
            "idle",
            //2
            "idle-run",
            //3
            "Run1",
            //4
            "Run2",
            //5
            "Run3",
            //6
            "Run4",
            //7
            "Run5",
            //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",

        ];
        this.arr = [];

        //todo 跑步动作
        this.statusArr_run = [
            //0
            "Run1",
            //1
            "Run2",
            //2
            "Run3",
            //3
            "Run4",
            //4
            "Run5",
        ];

        this.arr.push(this.statusArr_run);

        //todo 跳远
        var a = [];

        this.statusArr_longjump = a.concat(this.statusArr_run);
        //5
        this.statusArr_longjump.push("longjump-Test");
        //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_6",
            //3
            "bybike_4",
            //4
            "bybike_5",
            //5
            "bybike_5bike",
            //6
            "bybike_5boy",
            //7
            "bybike_redy",
            //8
            "bybike_redy_bike",
            //9
            "bybike_redy",
            //10
            "bybike_3",
        ];

        this.arr.push(this.statusArr_bike);

        //todo 标枪项目
        this.statusArr_javelin = [
            //0
            "javelin_1",
            //1
            "javelin_2",
            //2
            // "javelin_2a",
            //2
            "javelin_3",
            //3
            // "javelin_3a",
            //3
            "javelin_4",
            //4
            "javelin_5",
            //5
            "javelin_redy",
            //6
            "javelin_5",
        ];
        this.arr.push(this.statusArr_javelin);
        //todo 标枪
        this.statusArr_biaoqiang = [
            "Biaoqiang01",
            "Biaoqiang02",
            "Biaoqiang03",
        ];

        //踉跄数组
        this.staggerArr = [];

        this.staggerArr.push(this.statusArr_run[3]);
        this.staggerArr.push(this.statusArr_javelin[3]);
        this.staggerArr.push(this.statusArr_bike[3]);



        //初始化动作状态
        this.setStatus(this.statusArr[1]);
        this.setMyTag(null);
    },
    setMyTag: function (obj) {
        this.myTag = obj;
    },
    idle: function () {
        this.spine.timeScale = 1.0;
        this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
    },
    idleadd: function () {
        this.spine.addAnimation(0, this.statusArr_redyrun[0], true);
    },
    addidle: function () {
        this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
    },
    test: function () {
        this.spine.setAnimation(0, this.statusArr_run[2], true);
    },
    walk: function () {
        // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
        this.spine.setAnimation(0, this.statusArr[3], true);
    },
    walkadd: function () {
        // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
        this.spine.addAnimation(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;
    },
    readyRun_add: function () {
        // this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
        // this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
        this.spine.addAnimation(0, this.statusArr_run[0], true);
        //奔跑等级
        this.walkLv = 0;
    },
    SetCurrentAnimationIdx: function (idx) {
        this.spine.addAnimation(0, this.arr[this.type][idx], true);
    },
    SetCurrentAnimationIdx_set: function (idx) {
        this.spine.setAnimation(0, this.arr[this.type][idx], true);
    },
    //todo 设置对应动作
    setCurrentSpeed: function () {
        this.walkLv = this.getSpeedLv();
        // cc.log("当前速度","档位",this.walkLv);
        this.spine.addAnimation(0, this.arr[this.type][this.walkLv], true);
        this.spine.timeScale = 1;
    },
    stagger: function () {
        this.spine.setAnimation(0, this.arr[this.type][3], false);
    },
    longjump: function (jumpTimer) {
        // this.spine.timeScale = 1.5;
        this.spine.setAnimation(0, this.statusArr_longjump[5], false);
        this.spine.timeScale = 1/jumpTimer;
        // console.log("timeScale ==", jumpTimer, this.spine.timeScale);
        var OutTimer = jumpTimer* 1000 + 300;

        // console.log("jumpTimer ==", jumpTimer, OutTimer);
        setTimeout(function () {

            if (this.timeOutLongJumpListener != null) {
                this.timeOutLongJumpListener();
            }
        }.bind(this), OutTimer);
    },
    setTimeOutLongJumpListener: function (timeOutLongJumpListener) {
        this.timeOutLongJumpListener = timeOutLongJumpListener;
    },
    longjump_stagger: function () {
        this.spine.setAnimation(0, this.statusArr_longjump[1], false);
        this.setCurrentSpeed();
    },
    hurdling: function (callBack) {
        this.spine.setAnimation(0, this.statusArr_hurdling[5], false);
        this.spine.addAnimation(0, this.statusArr_hurdling[this.walkLv], true);
        this.spine.timeScale = 1.5;
        if (callBack != null) {
            this.spine.setInterruptListener(function (trackEntry) {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                if ("hurdling_1" == animationName) {
                    callBack(animationName);
                    this.spine.timeScale = 1;
                }
            }.bind(this));

        }
        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;
    },
    //按屏幕了
    setOnTouchListener: function () {
        this.setType();
        // 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;
        }
    },
    //todo 跨栏 跳跃
    setOnHurdlingListener: function (callBack) {
        this.setType();
        this.hurdling(callBack);
    },
    //todo 跨栏 踉跄
    setOnHurdlingStaggerListener: function () {
        this.setType();
        this.hurdling_stagger();
    },
    //todo 跳远跳跃
    setOnLongJumpListener: function (jumpTimer) {
        this.setType();
        this.longjump(jumpTimer);
    },
    //todo 跳远 踉跄
    setOnLongJumpStaggerListener: function () {
        this.setType();
        this.stagger();
    },
    //摔倒了
    setOnTripListener: function () {
        this.setType();
        this.stagger();
    },

    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_bike[9], false);
        this.spine.addAnimation(0, this.statusArr_bike[0], true);
        this.walkLv = 0;

    },
    //todo 快下车 没时间解释了
    downBike: function (callBack) {
        this.setType();
        this.spine.setAnimation(0, this.statusArr_bike[4], false);
        if (callBack != null) {
            this.spine.setEndListener(function (trackEntry) {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                if (animationName == this.statusArr_bike[0]) {
                    callBack(animationName);
                }
            }.bind(this));
        }
        this.walkLv = 0;
    },
    //todo 快拿标枪 没时间解释了
    upJavelin: function () {
        this.setType();
        this.spine.setAnimation(0, this.statusArr_javelin[1], true);
    },
    //todo 快扔标枪 没时间解释了
    upJavelin_go: function (callBack) {
        this.setType();
        this.spine.setAnimation(0, this.statusArr_javelin[6], false);
        this.spine.timeScale = 1.5;

        setTimeout(function () {
            this.spine.timeScale = 1;
        }.bind(this), 600);
        // this.walkLv = 0;
        if (callBack != null) {
            this.spine.setEndListener(function (trackEntry) {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                callBack(animationName);
            }.bind(this));
        }
    },
    //todo 快扔标枪 没时间解释了
    javelin: function () {
        this.setType();
        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);
    },
    setType: function () {
        var temp = this.Canvas.getComponent("GameStates").CurrentProgress;
        // cc.log("现在那标枪",temp);
        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 TouchLayout = cc.find("TouchLayout");
        var lv = TouchLayout.getComponent("NodeTouch").Hero.getComponent("HeroControl").PlayerStateScript.CurrentSpeed;
        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;
    }
});

