import { _decorator, CCInteger, Component, tween, Vec3, Node, Animation, Tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SpinAni')
export class SpinAni extends Component {

    @property(Node)
    private rotateNode: Node = null;

    @property(Animation)
    private buttonAnimation: Animation = null;

    @property(CCInteger)
    private idleRotationSpeed: number = 120;

    @property(CCInteger)
    private runRotationSpeed: number = 540;

    @property(CCInteger)
    private time: number = 1;

    onLoad() {
        this.playIdleSpin();
        this.buttonAnimation.play('BtnSpin_idle_animation');
    }

    private playIdleSpin() {
        Tween.stopAllByTarget(this.rotateNode);
        const duration = 360 / this.idleRotationSpeed * this.time;
        tween(this.rotateNode)
            .by(duration, { eulerAngles: new Vec3(0, 0, -360) })
            .repeatForever()
            .start();
    }

    public startSpin() {
        Tween.stopAllByTarget(this.rotateNode);
        const duration = 360 / this.runRotationSpeed * this.time;
        tween(this.rotateNode)
            .by(duration, { eulerAngles: new Vec3(0, 0, -360) })
            .repeatForever()
            .start();
        this.buttonAnimation.play('BtnSpin_spin_animation');

        this.buttonAnimation.once(Animation.EventType.FINISHED, () => {
            this.buttonAnimation.play('BtnSpin_idle_animation');
        });
    }

    public stopSpin() {
        this.playIdleSpin();
        this.buttonAnimation.off(Animation.EventType.FINISHED);
        this.buttonAnimation.play('BtnSpin_idle_animation');
    }

}



