const ArtistManager = require('../utils/ArtistManager');

cc.Class({
    extends: cc.Component,

    properties: {
        
        nickLabel: cc.Label,

        runMan: cc.Node,

        artistSkeleton: sp.Skeleton,

        direction: {
            get: function () {
                if (!this._direction) {
                    this._direction = 1;
                }
                return this._direction;
            },
            set: function(value) {
                this._direction = value;
            }
        },

        _accSpeed: 50, 

        _maxX: 270,


    },

    init(artistData) {
        this.artistData = artistData;
        // this.nickLabel.string = artistData.nick;
        this.walkAnimation = [];
        this.standAnimation = [];

        this.node.x = Math.random()*(Math.random()>0.5?1:-1) * 150;

        // this.artistData.jobId 固定为2吧, 暂时只有舞者的骨骼动画
        ArtistManager.loadStar(this.artistData.starId)
        .then((skeletonData) => {
            this.artistSkeleton.skeletonData = skeletonData;
            var sd = skeletonData.getRuntimeData(true);
            if (sd) {   
                /**
                 * item: Animation {name: "stand1", timelines: Array(113), duration: 2}
                 */
                this.walkAnimation = sd.animations.filter(item => item.name.indexOf('walk') != -1).map(animation => animation.name) || [];
                this.standAnimation = sd.animations.filter(item => item.name.indexOf('stand') != -1).map(animation => animation.name) || [];
            }
           this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
        }).catch((err) => {
            console.log(err);
        });
    },


    onLoad () {
        this.node.y = 20;
        this.isStand = false;

        this.totalDistance = 0;
    },



    update (dt) {


        if (this.isStand) { return; }

        // 每一帧移动多少距离, 这是固定的
        let distance = this._accSpeed * dt;
        this.totalDistance += distance;
    
        if (Math.floor(this.totalDistance) == 340) {
            
            this.totalDistance = 0;
            let random = Math.floor(Math.random() * 2);

            if (random === 0) { // walk
                
                let walk = Math.floor(Math.random() * this.walkAnimation.length);
                if (walk === 0) {
                    this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], true);
                } else {
                    this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], false);
                    this.artistSkeleton.setCompleteListener(() => {
                        this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
                        this.artistSkeleton.completeListener = null;
                    });
                }

            } else { // stand
                
                let stand = Math.floor(Math.random() * this.standAnimation.length);
                this.artistSkeleton.setAnimation(0, this.standAnimation[stand], false);
                this.artistSkeleton.setCompleteListener(() => {
                    this.isStand = false;
                    this.artistSkeleton.completeListener = null;
                    this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
                });
                this.isStand = true;
                return;
            }
        }

        // 判断方向
        if (this.direction < 0) {
            this.runMan.scaleX = -1;

            if ((this.node.x + distance) > this._maxX) {
                this.direction = 1;
                this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
            } else {
                this.node.x = this.node.x + distance;
            }

        } else {
            this.runMan.scaleX = 1;

            if ((this.node.x - distance) < -this._maxX) {
                this.direction = -1;
                this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
            } else {
                this.node.x = this.node.x - distance;
            }
        }
    },
});
