// Region & Language.
//
// GNOME keeps language and formats under System; the setting itself is the
// machine's locale, which localectl owns. Panama does not store a copy of it --
// there is exactly one system locale and localectl is where it lives, so a
// preference here would be a second source of truth that drifts the moment
// anything else changes it.
//
// Keyboard layout is deliberately not repeated here even though GNOME groups it
// with region. It is a compositor setting that applies instantly, it lives on
// the Keyboard page with the rest of the typing settings, and showing it twice
// invites the two views to disagree.

import QtQuick
import qs.config
import qs.services

SettingsPage {
    id: root

    title: "Region & Language"
    lede: SystemLocale.pendingRestart
        ? "Your new language applies to programs started after you sign out and back in."
        : "The language and regional formats this machine uses."

    Component.onCompleted: if (SystemLocale.locales.length === 0) SystemLocale.refresh()

    SettingsCard {
        title: "Language"
        subtitle: "Changing this needs your password, and takes effect for programs started afterwards."

        PickerRow {
            id: languagePicker

            label: "Current language"
            detail: SystemLocale.pendingRestart
                ? "Chosen, but not in use until you sign out and back in"
                : "Used by programs that ask the system what language to speak"
            value: SystemLocale.currentLabel || "Reading…"
            divider: false

            SearchPicker {
                width: parent.width
                items: SystemLocale.locales
                current: SystemLocale.current
                placeholder: "Search languages and regions"
                emptyText: SystemLocale.scanning ? "Reading installed locales…" : "No locales are installed"
                onPicked: value => {
                    SystemLocale.set(value);
                    languagePicker.collapse();
                }
            }
        }
    }

    SettingsCard {
        visible: SystemLocale.lastError !== ""
        title: "Language problem"
        subtitle: SystemLocale.lastError
    }

    SettingsCard {
        title: "Formats"
        subtitle: "Dates, times, and numbers follow the language above. The desktop's own clock formatting is on the Appearance page."

        ActionRow {
            label: "Clock and date display"
            detail: "How the desktop itself shows the time"
            action: "Open appearance"
            onTriggered: ShellState.openSettings("appearance")
        }

        ActionRow {
            label: "Regional formats"
            detail: "Separate per-category formats (LC_TIME, LC_NUMERIC) are owned by Fedora"
            action: "Open system"
            divider: false
            onTriggered: SystemSettings.openGnomePanel("system", "region")
        }
    }
}
