CCCPaste
-- Apps Management Module for SimpleOS

local apps = {}

-- Default apps storage
local appList = {}
local appsFile = "os/data/apps.dat"

-- Default apps
local defaultApps = {
    {
        name = "Shell",
        symbol = ">",
        command = "shell"
    },
    {
        name = "Files",
        symbol = "F",
        command = "list"
    },
    {
        name = "Edit",
        symbol = "E",
        command = "edit"
    },
    {
        name = "Lua",
        symbol = "L",
        command = "lua"
    },
    {
        name = "Snake",
        symbol = "S",
        command = "os/games/snake"
    },
    {
        name = "Tetris",
        symbol = "T",
        command = "os/games/tetris"
    },
    {
        name = "Pong",
        symbol = "P",
        command = "os/games/pong"
    },
    {
        name = "Mines",
        symbol = "M",
        command = "os/games/minesweeper"
    },
    {
        name = "2048",
        symbol = "2",
        command = "os/games/2048"
    },
    {
        name = "Breakou",
        symbol = "B",
        command = "os/games/breakout"
    }
}

-- Ensure data directory exists
local function ensureDataDir()
    if not fs.exists("os/data") then
        fs.makeDir("os/data")
    end
end

-- Load apps from file
function apps.load()
    ensureDataDir()
    if fs.exists(appsFile) then
        local file = fs.open(appsFile, "r")
        if file then
            local content = file.readAll()
            file.close()
            local loaded = textutils.unserialize(content)
            if loaded then
                appList = loaded
                return
            end
        end
    end
    -- Use defaults if no saved apps
    appList = defaultApps
    apps.save()
end

-- Save apps to file
function apps.save()
    ensureDataDir()
    local file = fs.open(appsFile, "w")
    if file then
        file.write(textutils.serialize(appList))
        file.close()
    end
end

-- Get all apps
function apps.getAll()
    return appList
end

-- Add a new app
function apps.add(name, symbol, command)
    table.insert(appList, {
        name = name,
        symbol = symbol or "#",
        command = command
    })
    apps.save()
end

-- Remove an app by index
function apps.remove(index)
    if index > 0 and index <= #appList then
        table.remove(appList, index)
        apps.save()
        return true
    end
    return false
end

-- Edit an app
function apps.edit(index, name, symbol, command)
    if index > 0 and index <= #appList then
        appList[index].name = name or appList[index].name
        appList[index].symbol = symbol or appList[index].symbol
        appList[index].command = command or appList[index].command
        apps.save()
        return true
    end
    return false
end

-- Run an app
function apps.run(index)
    if index > 0 and index <= #appList then
        local app = appList[index]
        term.setBackgroundColor(colors.black)
        term.setTextColor(colors.white)
        term.clear()
        term.setCursorPos(1, 1)
        
        -- Run the command
        shell.run(app.command)
        
        -- Wait for key press to return
        print()
        print("Press any key to return to desktop...")
        os.pullEvent("key")
        return true
    end
    return false
end

-- Get app count
function apps.count()
    return #appList
end

-- Get app by index
function apps.get(index)
    return appList[index]
end

return apps