import { _decorator, ccenum, Component, Node } from 'cc';
import { Cards_Constants } from 'db://assets/src/constants/Cards_Constants';
const { ccclass, property } = _decorator;

export enum UIEventType
{
    EVENT_INIT_STATE,
    EVENT_RESTART_GAME,
    EVENT_SHOW_START_PANEL,
    EVENT_SAVE_CARDS,
    EVENT_REMOVE_CARDS,
}
ccenum(UIEventType);

@ccclass('UIEventsHandler')
export class UIEventsHandler extends Component 
{
    public fn_SubscribeEvent_NO_PARAM(type: UIEventType, method: () => void)
    {
        this.node.on(type.toString(), () => method());
    }

    public fn_SubscribeEvent_ONE_PARAM(type: UIEventType, method: (state) => void)
    {
        this.node.on(type.toString(), (state) => method(state));
    }

    public fn_TriggerEvent_NO_PARAM(type: UIEventType)
    {
        this.node.emit(type.toString());
    }

    public fn_TriggerEvent_ONE_PARAM(type: UIEventType, state: boolean)
    {
        this.node.emit(type.toString(), state);
    }

    public fn_OnAllEventsTriggered()
    {
        setTimeout(() => 
        {
            this.destroy();    // for unsubscribing
        }, Cards_Constants.MILLI_SEC);
    }
}

