-- home.lua - Home screen dynamic app rendering
-- Handles system apps and discovered third-party apps

-- System apps with their navigation keys and colors
local system_apps = {
    -- Row 1
    {name = "Phone", icon = "phone", color = "#4CAF50", nav = "dialer"},
    {name = "Messages", icon = "message", color = "#2196F3", nav = "messages"},
    {name = "Contacts", icon = "contacts", color = "#FF9800", nav = "contacts"},
    {name = "Browser", icon = "browser", color = "#F44336", nav = "browser"},
    -- Row 2
    {name = "Gallery", icon = "gallery", color = "#9C27B0", nav = nil},
    {name = "Camera", icon = "camera", color = "#00BCD4", nav = "camera"},
    {name = "Settings", icon = "settings", color = "#607D8B", nav = "settings"},
    {name = "Music", icon = "music", color = "#E91E63", nav = "music"},
    -- Row 3
    {name = "Calendar", icon = "calendar", color = "#3F51B5", nav = nil},
    {name = "Clock", icon = "clock", color = "#009688", nav = nil},
    {name = "Notes", icon = "notes", color = "#795548", nav = nil},
    {name = "Maps", icon = "maps", color = "#FF5722", nav = nil},
    -- Row 4
    {name = "Store", icon = "store", color = "#8BC34A", nav = "store"},
    {name = "Files", icon = "files", color = "#CDDC39", nav = nil},
    {name = "Calculator", icon = "calculator", color = "#FFC107", nav = nil},
    {name = "Weather", icon = "weather", color = "#673AB7", nav = nil},
}

-- State
local installed_apps = {}
local home_document = nil  -- Store document reference

-- Initialize on load (receives document from onload event)
function initHome(doc)
    print("[Home] Initializing home screen...")
    home_document = doc

    -- Get all installed apps
    if mosis and mosis.apps then
        installed_apps = mosis.apps.getInstalled() or {}
        print("[Home] Found " .. #installed_apps .. " installed apps")

        -- Log each app
        for _, app in ipairs(installed_apps) do
            local app_type = app.is_system_app and "System" or "Third-party"
            print("[Home] " .. app_type .. " app: " .. app.name .. " (" .. app.package_id .. ")")
        end
    else
        print("[Home] Warning: mosis.apps API not available")
        installed_apps = {}
    end

    -- Render all apps
    renderInstalledApps()
end

-- Generate a color based on package_id
function getAppColor(package_id)
    local colors = {
        "#BB86FC", "#03DAC6", "#FF9800", "#2196F3",
        "#4CAF50", "#F44336", "#E91E63", "#3F51B5",
        "#009688", "#795548", "#FF5722", "#673AB7"
    }

    -- Simple hash of package_id to pick a color
    local hash = 0
    for i = 1, #package_id do
        hash = hash + package_id:byte(i)
    end

    return colors[(hash % #colors) + 1]
end

-- Get first letter for placeholder icon
function getAppInitial(name)
    return name:sub(1, 1):upper()
end

-- Render all installed apps into the grid
function renderInstalledApps()
    -- Use stored document reference
    if not home_document then
        print("[Home] Could not get document reference")
        return
    end

    local grid = home_document:GetElementById("installed-apps")
    if not grid then
        print("[Home] installed-apps container not found")
        return
    end

    -- Clear existing content
    grid.inner_rml = ""

    if #installed_apps == 0 then
        print("[Home] No apps to display")
        return
    end

    -- Build HTML for each app
    local html = ""
    for _, app in ipairs(installed_apps) do
        local color = getAppColor(app.package_id)
        local initial = getAppInitial(app.name)
        local icon_html

        -- Check if app has an icon
        if app.icon and app.icon ~= "" then
            local icon_path = app.icon

            -- Handle /system/ paths - map to shared assets
            if icon_path:sub(1, 8) == "/system/" then
                -- Map /system/icons/foo.tga to ../../icons/foo.tga (relative to home)
                icon_path = "../../" .. icon_path:sub(9)  -- Remove "/system/" prefix
                print("[Home] Mapped system icon: " .. app.icon .. " -> " .. icon_path)
            elseif icon_path:sub(1, 1) ~= "/" and not icon_path:find(":/") then
                -- Relative path - prepend install_path
                if app.install_path and app.install_path ~= "" then
                    icon_path = app.install_path .. "/" .. icon_path
                end
            end

            -- Use img tag for actual icon
            icon_html = '<img src="' .. icon_path .. '" style="width: 48px; height: 48px;"/>'
            print("[Home] Loading icon: " .. icon_path)
        else
            -- Fallback to initial letter
            icon_html = '<span style="font-size: 28px; color: #000000;">' .. initial .. '</span>'
        end

        html = html .. [[
            <div class="app-icon">
                <div class="app-icon-image" style="background-color: ]] .. color .. [[;"
                     onclick="launchApp(']] .. app.package_id .. [[')">
                    ]] .. icon_html .. [[
                </div>
                <span class="app-icon-label">]] .. app.name .. [[</span>
            </div>
        ]]
    end

    grid.inner_rml = html
    print("[Home] Rendered " .. #installed_apps .. " apps")
end

-- Get app info by package_id
function getAppInfo(package_id)
    for _, app in ipairs(installed_apps) do
        if app.package_id == package_id then
            return app
        end
    end
    return nil
end

-- Launch an app by package_id
function launchApp(package_id)
    print("[Home] Launching app: " .. package_id)

    if mosis and mosis.apps then
        local success = mosis.apps.launch(package_id)
        if success then
            print("[Home] App sandbox started: " .. package_id)

            -- Get app info for sandbox switching and UI loading
            local app_info = getAppInfo(package_id)
            if app_info and app_info.install_path and app_info.entry_point then
                -- Switch sandbox context to this app (registers timer, fs, json, crypto APIs)
                if switchAppSandbox then
                    switchAppSandbox(package_id, app_info.install_path)
                    print("[Home] Sandbox context switched to: " .. package_id)
                end

                -- Now load the app's UI document
                local entry_path = app_info.install_path .. "/" .. app_info.entry_point
                print("[Home] Loading app screen: " .. entry_path)
                local loaded = loadScreen(entry_path)
                if loaded then
                    print("[Home] App UI loaded: " .. package_id)
                else
                    print("[Home] Failed to load app UI: " .. entry_path)
                end
            else
                print("[Home] App info missing entry point: " .. package_id)
            end
        else
            print("[Home] Failed to launch app: " .. package_id)
        end
    else
        print("[Home] Cannot launch app: mosis.apps not available")
    end
end

-- initHome() is called via onload in home.rml
