import { eventTarget, 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 {
    @property(cc.Node)
    private chumNode: cc.Node = null;

    private _chumCtrl: ChumController = null;
    private spine: sp.Skeleton = null;
    private _count = 0;
    private _actions: Function[] = [];
    private _skins = ["Bua", "Ga", "Gai_lung", "Gay"]

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

        // this.spine.setSkin(this._skins[this.index % this._skins.length]);
        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];
    }

    public onSetTool() {
        this.spine.setSkin(this._skins[Global.tool % this._skins.length])
    }

    private onTouchStart(): void {
        this._actions[this._count].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._chumCtrl.playAnimationNut1();
    }

    private playAnimationDap2(): void {
        this.spine.setAnimation(0, "Dap2", false);
        this.spine.timeScale = 1.2;
        this.playSound();
        this._chumCtrl.playAnimationNut2();
    }

    private playAnimationDap3(): void {
        this.spine.setAnimation(0, "Dap3", false);
        this.spine.timeScale = 1.4;
        this.playSound();
        this._chumCtrl.playAnimationNut3();
    }

    private playAnimationDap4(): void {
        this.spine.setAnimation(0, "Dap4", false);
        this.spine.timeScale = 1.6;
        this.playSound();
        this._chumCtrl.playAnimationNut4();

        this.scheduleOnce(() => {
            eventTarget.emit(SHOW_CARD_POPUP);
        }, 0.5)
    }

    private playSound() {
        // this.node.getComponent(cc.AudioSource).play();
    }
}
