local ResourceManager = {
    sceneAbPath = {}
}

local AB_SUFFIX = ".ab"

local ResMgr = CS.BF.BFMain.Instance.ResMgr

function ResourceManager:getSceneAssetBundlePath(path)
    local abPath = self.sceneAbPath[path]
    if abPath == nil then
        local start_index = string.find(path, '/')
        if start_index == nil then
            abPath = path .. AB_SUFFIX
        else
            abPath = string.sub(path, start_index + 1) .. AB_SUFFIX
        end
        self.sceneAbPath[path] = abPath
    end
    return abPath
end

function ResourceManager:getSceneLoadPath(scenePath)
    if USE_AB then
        return ResMgr:GetSceneLoadPath(self:getSceneAssetBundlePath(scenePath))
    else
        return scenePath:gsub("^%l", string.upper)
    end
end

function ResourceManager:loadAsync(path, type, callback)
    return self:loadOriginAssetAsync(path, type, function(assetPath, asset)
        local obj = CS.UnityEngine.Object.Instantiate(asset)
        callback(assetPath, obj)
    end)
end

function ResourceManager:loadOriginAssetAsync(path, type, callback)
    return ResMgr:LoadAsync(path, type, callback)
end

function ResourceManager:loadSceneAsync(scenePath, callback)
    ResMgr:LoadSceneAsync(self:getSceneAssetBundlePath(scenePath), callback)
end

function ResourceManager:unloadScene(path)
    ResMgr:UnloadScene(self:getSceneAssetBundlePath(path))
end

function ResourceManager:unload(path, immediately)
    if immediately then
        ResMgr:Unload(path, true)
    else
        ResMgr:Unload(path, false)
    end
end

function ResourceManager:unloadAllDelayAssets()
    ResMgr:UnloadAllDelayAssets()
end

function ResourceManager:clear()
    ResMgr:Clear()
end

function ResourceManager:destroyPrefab(prefab)
    CS.UnityEngine.Object.Destroy(prefab)
end

function ResourceManager:containsAsset(assetPath)
    return ResMgr:ContainsAsset(assetPath)
end

if NOT_PUBLISH then
    ResourceManager._ReleaseloadOriginAssetAsync = ResourceManager.loadOriginAssetAsync
    function ResourceManager:loadOriginAssetAsync(path, type, callback)
        local function checkAsset(assetPath, asset)
            if asset == nil then
                Logger.logError("[LOAD_ERROR]load error:%s", assetPath)
            end
            callback(assetPath, asset)
        end
        self:_ReleaseloadOriginAssetAsync(path, type, checkAsset)
    end

    ResourceManager._ReleaseloadAsync = ResourceManager.loadAsync
    function ResourceManager:loadAsync(...)
        if self._debugLoadAsyncFuncMap == nil then
            self._debugLoadAsyncFuncMap = {
                [UIPrefabManager.loadUIWidgetAsync] = true,
                [EffectManager.loadUIEffectAsync] = true,
                [EffectManager.loadEffectAsync] = true,
                [EffectManager.loadBattleEffectAsync] = true,
                [EffectManager.loadHeroShowEffectAsync] = true,
                [ModelManager.loadHeroAsync] = true,
                [ModelManager.loadWeaponAsync] = true,
                [ModelManager.loadModelAsync] = true,
                [ModelManager.loadHeroShowAsync] = true,
                [ModelManager.loadMonsterAsync] = true,
                [SpineManager.loadUISpinePrefabAsync] = true,
                [SpineManager.loadMeshSpinePrefabAsync] = true,
                [SpineManager.loadHeroAsync] = true,
                [SpineManager.loadBattleEffectAsync] = true,
            }
        end
        local currFunc = debug.getinfo(2, "f").func
        if self._debugLoadAsyncFuncMap[currFunc] == nil then
            Logger.logFatal("you can not call ResourceManager:loadAsync directly")
        end
        self:_ReleaseloadAsync(...)
    end
end


return ResourceManager