
import { AudioConst } from "../../../data/const/TypeConst";
import GameDataCenter from "../../../data/GameDataCenter";
import EventMng from "../../../manager/EventMng";
import { ResCollector } from "../../compment/ResCollector";
import Context from "../Context";
import { FguiViewModel } from "./FguiViewModel";
import { IUIController } from "./IUIController";

export interface BtnEvent {
    btn: fairygui.GObject,
    func: Function
}
export abstract class FguiViewCtrl<T extends FguiViewModel> implements IUIController {
    ThirdPkgs: string[] = [];
    /**通知事件列表 */
    private _notifyEventList: Map<string, Function> = new Map<string, Function>;
    /** 定时器集合 */
    private m_Timers: Set<NodeJS.Timer> = new Set<NodeJS.Timer>();
    protected get Context(): Context {
        return this[Context.CONTEXT_KEY] as Context;
    }

    protected get VM(): T {
        return this[FguiViewModel.VIEW_MODEL_KEY] as T;
    }
    /**
     * UI初始化或者一些无法通过代码生成绑定的节点
     * 只执行一次
    */
    abstract OnInited(): void;
    abstract OnShow(intent?: any): void;
    abstract OnHide(isDispose?: boolean): void;
    OnDisplay(visible: boolean): void {

    }
    onCollectRes(resCollector: ResCollector, param: any): void {

    }

    GetThirdPkgs(): string[] {
        return this.ThirdPkgs;
    }

    OnUpdate(elapseTime: number): void {
    }
    OnLateUpdate(elapseTime: number): void {
    }
    OnDestroy(): void {
    }
    offEvent() {
        try {
            let self = this;
            this._notifyEventList.forEach((f, key) => {
                EventMng.off(key, f, self);
            }, this);
            this._notifyEventList.clear();
        } catch (error) {
            console.error("onDisable->" + error)
        }
    }
    /**注册notice事件，disable的时候会自动移除 */
    initEvent(eventName: string, cb: Function) {
        EventMng.on(eventName, cb, this);
        this._notifyEventList.set(eventName, cb);
    }

    /**
     * 增加定时器
     * @param callback 
     * @param delta 单位毫秒ms
     */
    AddTimer(callback: Function, delta: number) {
        let timer = setInterval(() => {
            callback.apply(this);
        }, delta);
        this.m_Timers.add(timer);
        return timer;
    }

    /** 移除所有定时器 */
    RemoveTimers() {
        this.m_Timers.forEach(element => {
            clearInterval(element);
        });
        this.m_Timers.clear();
    }

    /**
     * 一次性定时器
     * @param callback 
     * @param delta 单位毫秒ms
     */
    AddTimerOnce(callback: Function, delta: number) {
        let timer = setInterval(() => {
            clearInterval(timer)
            this.m_Timers.delete(timer);
            callback();
        }, delta);
        this.m_Timers.add(timer);
        return timer
    }

    /** 按钮点击注册器 */
    private btnClicks: BtnEvent[] = [];
    RegisterClick(btn: fairygui.GObject, func: Function, params: any = [], audio: AudioConst = AudioConst.effect_click) {
        let target = this
        let call = (event: any) => {
            GameDataCenter.audio.playEffect(audio)
            func.apply(target, [event, params])
        }
        btn.onClick(call, this);
        this.btnClicks.push({ btn: btn, func: call });
    }

    RemoveAllClick() {
        this.btnClicks.forEach(element => {
            element.btn.offClick(element.func, this);
        });
        this.btnClicks = []
    }

    public CloseUI() {

    }
}
