import { eventTarget, PLAY_ANIM_NUT1, PLAY_ANIM_NUT2, PLAY_ANIM_NUT3, PLAY_ANIM_NUT4, SHOW_ACTION_SCREEN, STOP_ANIM_CHUM } from "../Events";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ChumController extends cc.Component {
    private spine: sp.Skeleton = null;

    private _count = 0;
    private _actions: Function[] = [];

    protected onLoad(): void {
        this.spine = this.node.getComponent(sp.Skeleton);

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this._actions = [this.playAnimationNut1, this.playAnimationNut2, this.playAnimationNut3, this.playAnimationNut4];
        eventTarget.on(STOP_ANIM_CHUM, this.stopAnimation, this);

        eventTarget.on(PLAY_ANIM_NUT1, this.playAnimationNut1, this);
        eventTarget.on(PLAY_ANIM_NUT2, this.playAnimationNut2, this);
        eventTarget.on(PLAY_ANIM_NUT3, this.playAnimationNut3, this);
        eventTarget.on(PLAY_ANIM_NUT4, this.playAnimationNut4, this);
    }

    private onTouchStart(): void {

    }

    private stopAnimation(): void {
        this.spine.setAnimation(0, "default", true);
    }

    public playAnimationNut1(): void {
        this.playAnimation("nut1")
        this.spine.timeScale = 1;
    }

    public playAnimationNut2(): void {
        this.playAnimation("nut2")
        this.spine.timeScale = 1;
    }

    public playAnimationNut3(): void {
        this.node.active = true;
        this.playAnimation("nut3")
        this.spine.timeScale = 0.5;
    }

    public playAnimationNut4(): void {
        this.playAnimation("nut4", false)
        this.spine.timeScale = 0.5;
    }

    private playAnimation(animName: string, isLoop: boolean = true): void {
        this.node.active = true;
        this.spine.setAnimation(0, animName, isLoop);
    }

}
