-- settings.lua - Settings app functionality
-- Handles toggles, navigation, and system settings

local settings_doc = nil

-- Settings state
local settings_state = {
    wifi = true,
    wifi_network = "MosisNetwork",
    bluetooth = false,
    airplane_mode = false,
    location = true,
    location_mode = "High accuracy",
    brightness = 80,
    auto_brightness = true,
    dark_mode = true,
    font_size = "Default",
    sleep_timeout = "5 minutes",
    sound_volume = 70,
    ring_volume = 80,
    vibration = true,
    dnd = false,
    battery_percent = 85,
    battery_status = "Not charging",
    storage_used = 32,
    storage_total = 128
}

-- Initialize settings
function initSettings(doc)
    print("[Settings] Initializing...")
    settings_doc = doc
    updateAllToggles()
    updateAllSubtitles()
end

-- Update all toggle states
function updateAllToggles()
    updateToggle("wifi", settings_state.wifi)
    updateToggle("bluetooth", settings_state.bluetooth)
    updateToggle("airplane", settings_state.airplane_mode)
    updateToggle("location", settings_state.location)
end

-- Update a single toggle
function updateToggle(name, state)
    if not settings_doc then return end

    local toggle = settings_doc:GetElementById("toggle-" .. name)
    if toggle then
        if state then
            toggle:SetClass("active", true)
        else
            toggle:SetClass("active", false)
        end
    end
end

-- Update all subtitles
function updateAllSubtitles()
    if not settings_doc then return end

    -- WiFi
    local wifi_sub = settings_doc:GetElementById("subtitle-wifi")
    if wifi_sub then
        if settings_state.wifi then
            wifi_sub.inner_rml = "Connected to " .. settings_state.wifi_network
        else
            wifi_sub.inner_rml = "Off"
        end
    end

    -- Bluetooth
    local bt_sub = settings_doc:GetElementById("subtitle-bluetooth")
    if bt_sub then
        bt_sub.inner_rml = settings_state.bluetooth and "On" or "Off"
    end

    -- Battery
    local bat_sub = settings_doc:GetElementById("subtitle-battery")
    if bat_sub then
        bat_sub.inner_rml = settings_state.battery_percent .. "% - " .. settings_state.battery_status
    end

    -- Storage
    local storage_sub = settings_doc:GetElementById("subtitle-storage")
    if storage_sub then
        storage_sub.inner_rml = settings_state.storage_used .. " GB of " .. settings_state.storage_total .. " GB used"
    end

    -- Location
    local loc_sub = settings_doc:GetElementById("subtitle-location")
    if loc_sub then
        if settings_state.location then
            loc_sub.inner_rml = "On - " .. settings_state.location_mode
        else
            loc_sub.inner_rml = "Off"
        end
    end
end

-- Toggle WiFi
function toggleWifi()
    settings_state.wifi = not settings_state.wifi
    print("[Settings] WiFi: " .. tostring(settings_state.wifi))

    updateToggle("wifi", settings_state.wifi)
    updateAllSubtitles()

    if showToast then
        showToast(settings_state.wifi and "WiFi enabled" or "WiFi disabled")
    end
end

-- Toggle Bluetooth
function toggleBluetooth()
    settings_state.bluetooth = not settings_state.bluetooth
    print("[Settings] Bluetooth: " .. tostring(settings_state.bluetooth))

    updateToggle("bluetooth", settings_state.bluetooth)
    updateAllSubtitles()

    if showToast then
        showToast(settings_state.bluetooth and "Bluetooth enabled" or "Bluetooth disabled")
    end
end

-- Toggle Airplane Mode
function toggleAirplaneMode()
    settings_state.airplane_mode = not settings_state.airplane_mode
    print("[Settings] Airplane mode: " .. tostring(settings_state.airplane_mode))

    if settings_state.airplane_mode then
        -- Disable wireless when airplane mode is on
        settings_state.wifi = false
        settings_state.bluetooth = false
        updateToggle("wifi", false)
        updateToggle("bluetooth", false)
    end

    updateToggle("airplane", settings_state.airplane_mode)
    updateAllSubtitles()

    if showToast then
        showToast(settings_state.airplane_mode and "Airplane mode on" or "Airplane mode off")
    end
end

-- Toggle Location
function toggleLocation()
    settings_state.location = not settings_state.location
    print("[Settings] Location: " .. tostring(settings_state.location))

    updateToggle("location", settings_state.location)
    updateAllSubtitles()

    if showToast then
        showToast(settings_state.location and "Location enabled" or "Location disabled")
    end
end

-- Open WiFi settings
function openWifiSettings()
    print("[Settings] Opening WiFi settings...")
    if navigateTo then
        navigateTo("wifi_settings")
    else
        if showToast then
            showToast("WiFi settings")
        end
    end
end

-- Open Bluetooth settings
function openBluetoothSettings()
    print("[Settings] Opening Bluetooth settings...")
    if showToast then
        showToast("Bluetooth settings")
    end
end

-- Open Display settings
function openDisplaySettings()
    print("[Settings] Opening Display settings...")
    if navigateTo then
        navigateTo("display_settings")
    else
        if showToast then
            showToast("Display settings")
        end
    end
end

-- Open Sound settings
function openSoundSettings()
    print("[Settings] Opening Sound settings...")
    if showToast then
        showToast("Sound settings")
    end
end

-- Open Notifications settings
function openNotificationsSettings()
    print("[Settings] Opening Notifications settings...")
    if showToast then
        showToast("Notification settings")
    end
end

-- Open Battery settings
function openBatterySettings()
    print("[Settings] Opening Battery settings...")
    if showToast then
        showToast("Battery: " .. settings_state.battery_percent .. "%")
    end
end

-- Open Storage settings
function openStorageSettings()
    print("[Settings] Opening Storage settings...")
    if showToast then
        local used_percent = math.floor(settings_state.storage_used / settings_state.storage_total * 100)
        showToast("Storage: " .. used_percent .. "% used")
    end
end

-- Open Lock Screen settings
function openLockScreenSettings()
    print("[Settings] Opening Lock Screen settings...")
    if showToast then
        showToast("Lock screen settings")
    end
end

-- Open Privacy settings
function openPrivacySettings()
    print("[Settings] Opening Privacy settings...")
    if showToast then
        showToast("Privacy settings")
    end
end

-- Open Location settings
function openLocationSettings()
    print("[Settings] Opening Location settings...")
    if showToast then
        showToast("Location settings")
    end
end

-- Open About Phone
function openAboutPhone()
    print("[Settings] Opening About Phone...")
    if navigateTo then
        navigateTo("about_phone")
    else
        if showToast then
            showToast("Mosis Virtual Phone v1.0")
        end
    end
end

-- Open User Profile
function openUserProfile()
    print("[Settings] Opening User Profile...")
    if showToast then
        showToast("User profile")
    end
end

-- Search settings
function searchSettings(query)
    print("[Settings] Searching: " .. query)
    -- TODO: Implement settings search
end

-- Get setting value
function getSetting(key)
    return settings_state[key]
end

-- Set setting value
function setSetting(key, value)
    settings_state[key] = value
    print("[Settings] Set " .. key .. " = " .. tostring(value))
    updateAllToggles()
    updateAllSubtitles()
end
