
/**
 * 加载资源工具类
 * @description 
 * @author 冯聪
 */
export default class LoadResUtil {

    /**
     * 日志标签
     * @description 
     */
    private static _logTag = "[LoadResUtil]";

    /**
     * 预加载本地资源 
     * @description   https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param  paths  加载路径
     * @param  type   资源类型(cc.Asset)
     * @returns Promise
     */
    public preload(paths: string | any, type: typeof cc.Asset): Promise<any> {
        return new Promise<cc.AssetManager.RequestItem[]>((resolve: (data: cc.AssetManager.RequestItem[]) => void, reject) => {
            cc.resources.preload(paths, type,
                //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                //onComplete
                (err: Error = null, data: cc.AssetManager.RequestItem[]): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 加载本地资源 
     * @description   https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param  paths  加载路径
     * @param  type   资源类型(cc.Asset)
     * @returns Promise
     */
    public load<T extends cc.Asset>(paths: string | any, type: typeof cc.Asset): Promise<any> {
        return new Promise<T>((resolve: (data: T) => void, reject) => {
            cc.resources.load(paths, type,
                //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                //onComplete
                (err: Error = null, data: T): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 预加载文件夹资源 
     * @description   https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param   dir   文件夹路径
     * @param   type  资源类型(cc.Asset)
     * @returns Promise
     */
    public preloadDir(dir: string, type: typeof cc.Asset): Promise<any> {
        return new Promise<cc.AssetManager.RequestItem[]>((resolve: (data: cc.AssetManager.RequestItem[]) => void, reject) => {
            cc.resources.preloadDir(dir, type,  //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                (err: Error = null, data: cc.AssetManager.RequestItem[]): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 加载文件夹资源 
     * @description   https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param   dir   文件夹路径
     * @param   type  资源类型(cc.Asset)
     * @returns Promise
     */
    public loadDir<T extends cc.Asset>(dir: string, type: typeof cc.Asset): Promise<any> {
        return new Promise<T>((resolve: (data: T) => void, reject) => {
            cc.resources.loadDir(dir, type,
                //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                //onComplete
                (err: Error = null, data: T | any): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 加载远程资源 
     * @description      https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param   url      加载路径
     * @param   options  
     * @returns Promise
     */
    public loadRemote<T extends cc.Asset>(url: string, options: Record<string, any>): Promise<any> {
        return new Promise<T>((resolve: (data: T) => void, reject) => {
            cc.assetManager.loadRemote(url, options,
                //onComplete
                (err: Error = null, data: T): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 预加载场景 
     * @description     https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param   url     加载路径
     * @param   options  
     * @returns Promise
     */
    public preloadScene(sceneName: string, options: Record<string, any>) {
        return new Promise<void>((resolve: () => void, reject) => {
            cc.resources.preloadScene(sceneName, options,
                //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                //onComplete
                (err: Error = null): void => {
                    if (!err) {
                        resolve();
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }

    /**
     * 加载场景 
     * @description     https://docs.cocos.com/creator/manual/zh/release-notes/subpackage-upgrade-guide.html
     * @param   url     加载路径
     * @param   options 
     * @returns Promise
     */
    public loadScene(sceneName: string, options: Record<string, any>): Promise<any> {
        return new Promise<any>((resolve: (data: any) => void, reject) => {
            cc.resources.loadScene(sceneName, options,
                //onProgress
                (finish: number, total: number, item: cc.AssetManager.RequestItem) => {

                },
                //onComplete
                (err: Error = null, data: cc.SceneAsset): void => {
                    if (!err) {
                        resolve(data);
                    }
                    else {
                        reject(err);
                    }
                });
        });
    }
}
