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

const { ccclass, property } = cc._decorator;

@ccclass
export default class ActionScreen extends cc.Component {
    @property(cc.Node)
    private bottleIdle: cc.Node = null;
    @property(cc.Node)
    private bottleAction: cc.Node = null;

    private _buaController: BuaController = null;
    private _spine: sp.Skeleton = null;

    onLoad() {
        eventTarget.on(SHOW_ACTION_SCREEN, this.showScreen, this);
        eventTarget.on(SHOW_START_SCREEN, this.hideScreen, this);
        eventTarget.on(PLAY_ANIM_NUT1, this.hideBottle, this);
        eventTarget.on(PLAY_ANIM_NUT2, this.hideBottle, this);
        eventTarget.on(PLAY_ANIM_NUT3, this.hideBottle, this);
        eventTarget.on(PLAY_ANIM_NUT4, this.hideBottle, this);
        this._buaController = this.getComponentInChildren(BuaController);
        this._spine = this.bottleIdle.getComponent(sp.Skeleton);
    }

    private hideBottle() {
        this.bottleAction.active = true;
        this.bottleIdle.active = false;
    }

    private showScreen() {
        this.bottleAction.active = false;
        this.bottleIdle.active = true;
        this._spine.setAnimation(0, 'Idle', true);

        this.node.active = true;
        this._buaController.onSetTool();
        this.node.position = cc.v3();
    }

    private hideScreen() {
        this.node.active = false;
    }

}
