
package = {}

local function filename(url)
    local path = url:split("/")
    return path[#path]
end

function package.install_from_git(url)
    
end

function package.install_from_contentdb(url)
    
end

function package.can_update()
    
end

function package.update()
    
end

function package.info()
    
end

function package.is_installed(kind, name)
    local path = core.get_user_path()
    if kind == "mod" or kind == "modpack" then
        path = path.."/mods/"..name
    elseif kind == "game" then
        path = path.."/games/"..name
    elseif kind == "txp" then
        path = path.."/textures/"..name
    end
    return core.is_dir(path)
end

local loading_screenshots = {}
local screenshot_path = core.get_cache_path().."/cdb"
function package.screenshot(pkg, url, level)
    if not url then
        url = pkg.thumbnail
    end
    
    if not level then
        level = 2
    end
    
    if not url then
        return default_textures.."no_screenshot.png"
    end
    
    url = url:gsub("/thumbnails/[0-9]+/", "/thumbnails/"..level.."/")
    
    if loading_screenshots[url] then
        return loading_screenshots[url]
    end
    
    local path = screenshot_path.."/"
                    ..string.format(
                        "%s-%s-%s-l%d-%s",
                        pkg.type,
                        pkg.author,
                        pkg.name,
                        level,
                        filename(url)
                    )
    
    if file_exists(path) then
        return path
    end
    
    local state = imfs.state(default_textures.."loading_screenshot.png")
    loading_screenshots[url] = state
    
    core.handle_async(function(args)
        return core.download_file(args.url, args.path)
    end, {path = path, url = url},
    function(success)
        state(path)
        loading_screenshots[url] = nil
    end)
    
    return state
end

local function alphabetic_sort(a, b)
    return (a.title ~= "" and a.title or a.name) < (b.title ~= "" and b.title or b.name)
end

function package.games(dir, sort)
    if not dir then
        dir = core.get_gamepath()
    end
    local out = {}
    for _, x in ipairs(core.get_dir_list(dir, true)) do
        local info = core.get_content_info(dir.."/"..x)
        if info.type == "game" then
            out[#out +1] = info
        end
    end
    if sort ~= false then
        table.sort(out, alphabetic_sort)
    end
    return out
end

function package.mods(dir, sort)
    if not dir then
        dir = core.get_modpath()
    end
    local out = {}
    for _, x in ipairs(core.get_dir_list(dir, true)) do
        local info = core.get_content_info(dir.."/"..x)
        if info.type == "mod" or info.type == "modpack" then
            out[#out +1] = info
        end
    end
    if sort ~= false then
        table.sort(out, alphabetic_sort)
    end
    return out
end

function package.texturepacks(dir, sort)
    if not dir then
        dir = core.get_texturepath()
    end
    local out = {}
    for _, x in ipairs(core.get_dir_list(dir, true)) do
        local info = core.get_content_info(dir.."/"..x)
        if info.type == "txp" then
            out[#out +1] = info
        end
    end
    if sort ~= false then
        table.sort(out, alphabetic_sort)
    end
    return out
end

function package.all()
    local out = {}
    table.insert_all(out, package.mods(nil, false))
    table.insert_all(out, package.games(nil, false))
    table.insert_all(out, package.texturepacks(nil, false))
    table.sort(out, alphabetic_sort)
    return out
end
