// const ResPath = [
//     {
//         bg:[
//             "Theme/Theme1/BG0/BG1-hd.pvr.ccz/spriteFrame",
//             "Theme/Theme1/BG0/BG2-hd.pvr.ccz/spriteFrame",
//         ],
//         monster:[
//             "Theme/Theme1/Items/Monsters01-hd",
//             "Theme/Theme1/Items/Monsters02-hd",
//         ],
//         obs:[
//             "Theme/Theme1/Items/Object01-hd",
//             "Theme/Theme1/Items/Object02-hd",
//         ],
//         pathImg:"Theme/Theme1/BGlv/BG-hd.pvr.ccz/spriteFrame",
//         tmx:"Theme/Theme1/BG1lv/BGPath"

import { Asset, log, SpriteFrame, TiledMapAsset } from "cc";
import { GameResData } from "../DataItem/ItemData";
import { dataMgr } from "../Frames/DataManager";
import { GameInfo } from "../GameInfo";
import { resMgr } from "../Frames/ResourcesMgr";

        
//     },
//     {
//         bg:[
//             "Theme/Theme2/BG0/BG1-hd.pvr.ccz/spriteFrame",
//             "Theme/Theme2/BG0/BG2-hd.pvr.ccz/spriteFrame",
//         ],
//         monster:[
//             "Theme/Theme2/Items/Monsters01-hd",
//             "Theme/Theme2/Items/Monsters02-hd",
//         ],
//         obs:[
//             "Theme/Theme2/Items/Object01-hd",
//             "Theme/Theme2/Items/Object02-hd",
//         ],
//         pathImg:"Theme/Theme2/BGlv/BG-hd.pvr.ccz/spriteFrame",
//         tmx:"Theme/Theme2/BG1lv/BGPath"
//     },
//     {
//         bg:[
//             "Theme/Theme2/BG0/BG1-hd.pvr.ccz/spriteFrame",
//             "Theme/Theme2/BG0/BG2-hd.pvr.ccz/spriteFrame",
//         ],
//         monster:[
//             "Theme/Theme2/Items/Monsters01-hd",
//             "Theme/Theme2/Items/Monsters02-hd",
//         ],
//         obs:[
//             "Theme/Theme2/Items/Object01-hd",
//             "Theme/Theme2/Items/Object02-hd",
//         ],
//         pathImg:"Theme/Theme2/BGlv/BG-hd.pvr.ccz/spriteFrame",
//         tmx:"Theme/Theme2/BG1lv/BGPath"
//     },
// ]

class GameResMgr{
    private _resData: GameResData[] = null;
    static instance: GameResMgr = null;

    async loadGameRes(){
        this._resData = dataMgr.getAllDataByName("GameResPath");
        for(let i = 0; i < this._resData.length; i++){
            const data = this._resData[i];
            let path;
            let name;
            if(data.replace[0]){
                path = data.path.replace(data.replace[0], String(GameInfo.Instance.CurTheme));
                name = data.resName.replace(data.replace[0], String(GameInfo.Instance.CurTheme));
            }
            if(data.replace[1]){
                path = path.replace(data.replace[1], String(GameInfo.Instance.CurLv));
                name = name.replace(data.replace[1], String(GameInfo.Instance.CurLv));
            }
            //通过上面的名字和路径 加载资源
            await resMgr.load(path, name, data.atlas);
        }
    }
    //获取当前关卡的背景图片
    getMapBg(style: number): SpriteFrame{
        const name: string = `${GameInfo.Instance.CurTheme}-${style}`;
        return resMgr.getSpriteFrame(name);
    }
    //获取当前关卡的路径图片
    getPathBg(): SpriteFrame{
        const name: string = `${GameInfo.Instance.CurTheme}-${GameInfo.Instance.CurLv}-pathBg`;
        return resMgr.getSpriteFrame(name);
    }
    //获取当前关卡的瓦片地图
    getTmx(): TiledMapAsset{
        const name: string = `${GameInfo.Instance.CurTheme}-${GameInfo.Instance.CurLv}-tmx`;
        return resMgr.getTmxAsset(name);
    }
    //获取当前主题下的某个怪物图片
    getMonsterFrame(name: string): SpriteFrame{
        name = `${GameInfo.Instance.CurLv}-name`;
        return resMgr.getSpriteFrame(name);
    }
    //获取当前主题的style格式下的某个障碍物的图片
    getObsFrame(style: number, index: number | string){
        //拼接精灵帧的名字
        // 1 => 00 01  12 => 000 12
        let i: string;
        if(typeof(index) === "number"){
            i = "000" + index;
            i = GameInfo.Instance.CurTheme + "-" + style + "-cloud" + i.slice(i.length - 2);
        }
        else{
            i = GameInfo.Instance.CurTheme + "-" + style + "-cloud" + index;
        }
        return resMgr.getSpriteFrame(i);
    }

}

export const gameResMgr: GameResMgr = GameResMgr.instance = new GameResMgr();

