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


cc.Class({
    extends: cc.Component,

    properties: {

        coinNode: cc.Node,

        totalRateLabel: cc.Label,

        coinCount: 0,

        totalRate: {
            get: function() {
                if (!this._totalRate) {
                    this._totalRate = 0;
                }
                return this._totalRate;
            },
            set: function(value) {
                this._totalRate = value;
                this.totalRateLabel.string = DWTool.coinParse(this._totalRate);
            }
        },

        coinSpine: sp.Skeleton,
        
        isPlay: false,

        isPlaying: false,
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
    },

    getOwner() {
        return this.node;
    },

    /**
     * 初始化静态金币Spine动画
     * @param {Number} coinIndex 金币下标，默认[0,1,2]
     */
    initStatic(coinIndex) {
        let animKeyArray = ['jinbi_1', 'jinbi_2', 'jinbi_3'];
        let animKey;
        if(coinIndex <= animKeyArray.length - 1) {
            animKey = animKeyArray[coinIndex]
        } else {
            animKey = coinIndex % animKeyArray.length;
        }
        
        this.coinSpine.setAnimation(0, animKey, true)
    },

    /**
     * 初始化飞行金币Spine动画
     */
    initAnim() {
        let animKeyArray = ['jinbi_zhuangdong1', 'jinbi_zhuangdong2', 'jinbi_zhuangdong3']
        //随机设置转动动画
        let ran = Math.floor(Math.random() * 3);
        let animKey = animKeyArray[ran]

        this.totalRateLabel.node.active = false;
        this.coinSpine.setAnimation(0, animKey, true)
    },

    showAnimation() {
        
        if (this.isPlaying) { return; }

        this.node.stopAllActions();

        this.node.active = true;
        this.isPlayed = true;
        this.isPlaying = true;

        let jumpHeight = 175;
        let duration = 0.3;

        let animationArray = [];

        let moveAction1 = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
        let moveAction2 = cc.moveBy(duration, 0, -95).easing(cc.easeCubicActionIn());

        animationArray.push(moveAction1);
        animationArray.push(moveAction2);

        while(jumpHeight > 0.1) {
            jumpHeight = jumpHeight - (jumpHeight / 3);
            duration = duration - (duration / 3);
            let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
            let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
            animationArray.push(upAction);
            animationArray.push(downAction);
        }

        let callback = cc.callFunc(() => {
            this.isPlaying = false;
        });

        animationArray.push(callback);

        this.node.runAction(cc.sequence(animationArray));
    },

    updateAnimation() {

        if (this.isPlaying) { return; }

        this.node.stopAllActions();
        
        this.isPlaying = true;

        let upAction = cc.moveBy(0.1, 0, 30);
        let downAction = cc.moveBy(0.1, 0, -30);

        let callback = cc.callFunc(() => {
            this.isPlaying = false;
        });
        this.coinNode.runAction(cc.sequence(upAction, downAction, callback));
    },

    start () {

    },

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