import { BEAT_GUILD, eventTarget, PLAY_ANIM_NUT1, PLAY_ANIM_NUT2, PLAY_ANIM_NUT3, PLAY_ANIM_NUT4, PLAY_BREAKING_SOUND, PLAY_DROP_SOUND, SHOW_CARD_POPUP, STOP_GUILD_TAP } from "../Events";
import { Global } from "../Global";


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 = ["Ga", "Gai_lung", "Gay", "Bua",];
    private _isPlay = false;

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

        this.spine.setSkin('Bua');

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

        eventTarget.on(BEAT_GUILD, this.beatGuild, this);
    }

    private beatGuild() {
        this.spine.setAnimation(0, "Dap2", false);
        this.spine.timeScale = 1.3;
    }

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

    private onTouchEnd(): void {
        if (this._count + Global.tool > 3) {
            this._count = 0;
        }
    }

    private onTouchStart(): void {
        eventTarget.emit(STOP_GUILD_TAP);
        if (!this._isPlay) {
            return;
        }
        this._actions[Global.tool + this._count].bind(this)();
        this._count++;
        if (Global.tool + this._count > 3) {
            this._isPlay = false;
        }
    }

    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);
            eventTarget.emit(PLAY_BREAKING_SOUND);
        }, 0.2)

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

    private playSound() {
        eventTarget.emit(PLAY_DROP_SOUND);
    }
}
