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


const { ccclass, property } = cc._decorator;

@ccclass
export default class BuaController extends cc.Component {
    private spine: sp.Skeleton = null;
    private _count = 0;
    private _actions: Function[] = [];
    private _skins = ["Bua", "Ga", "Gai_lung", "Gay"];
    private _isPlay = false;
    private _beatTotal = [1, 2, 3, 4];

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

        this.spine.setSkin('Bua');

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this._actions = [this.playAnimationDap1, this.playAnimationDap2, this.playAnimationDap3, this.playAnimationDap4];
        this._isPlay = true;
    }

    public onSetTool() {
        this.spine.setSkin(this._skins[Global.tool % this._skins.length]);
        this.node.position = cc.v3(0, 0, 0);
    }

    private onTouchStart(): void {
        this._actions[this._count].bind(this)();
        // this._actions[3 - Global.tool].bind(this)();

        console.log('this._count', this._count)
        this._count = (this._count + 1) % this._actions.length;
    }

    private playAnimationDap1(): void {
        this.spine.setAnimation(0, "Dap1", false);
        this.spine.timeScale = 1;
        this.playSound();
        this.scheduleOnce(() => {
            eventTarget.emit(PLAY_ANIM_NUT1);
        }, 0.5)
    }

    private playAnimationDap2(): void {
        this.spine.setAnimation(0, "Dap2", false);
        this.spine.timeScale = 1.2;
        this.playSound();
        this.scheduleOnce(() => {
            eventTarget.emit(PLAY_ANIM_NUT2);
        }, 0.4)
    }

    private playAnimationDap3(): void {
        this.spine.setAnimation(0, "Dap3", false);
        this.spine.timeScale = 1.4;
        this.playSound();
        this.scheduleOnce(() => {
            eventTarget.emit(PLAY_ANIM_NUT3);
        }, 0.3)
    }

    private playAnimationDap4(): void {
        this.spine.setAnimation(0, "Dap4", false);
        this.spine.timeScale = 1.6;
        this.playSound();
        this.scheduleOnce(() => {
            eventTarget.emit(PLAY_ANIM_NUT4);
        }, 0.2)

        cc.tween(this.node)
            .delay(0.5)
            .call(() => {
                eventTarget.emit(SHOW_CARD_POPUP);
            })
            .by(0.5, { position: cc.v3(1000,) })
            .start();
    }

    private playSound() {
    }
}
