import { Prefab, resources } from 'cc';
import { GameoverPage } from './popup.gameoverpage';
import { SettingPage } from './popup.settingpage';

export enum EPopup {
    SETTINGPAGE,
    GAMEOVERPAGE
}

const CONFIG = {
 
    [EPopup.SETTINGPAGE]: {
        component: SettingPage,
        path: 'prefab/pop-up/SettingPage',
        script: './popup.settingpage.ts',
        prefab: null
    },
    [EPopup.GAMEOVERPAGE]: {
        component: GameoverPage,
        path: 'prefab/pop-up/GameoverPage',
        script: './popup.gameoverpage.ts',
        prefab: null
    }
}

export async function GetPopPrefab(p: EPopup): Promise<Prefab> {
    let conf = CONFIG[p];
    if (!conf.prefab) {
        conf.prefab = await mtec.cc.loadResAsync(conf.path, Prefab, resources);
    }

    return conf.prefab;
}

export async function GetPopComponent<P extends EPopup>(p: P): Promise<(typeof CONFIG)[P]['component']> {
    let conf = CONFIG[p];
    if (!conf.component) {
        conf.component = await import(conf.script).then(m => m.default);
    }

    return conf.component;
}

export type PopCtor<P extends EPopup> = typeof CONFIG[P]['component'];

export type PopIns<P extends EPopup> = InstanceType<PopCtor<P>>;

export type PopParams<P extends EPopup> = Parameters<PopIns<P>['onShow']>;

export type PopReturn<P extends EPopup> = Awaited<ReturnType<PopIns<P>['show']>>;