// Choosing where the weather reading is for.
//
// A search box rather than latitude and longitude fields: nobody knows their
// own coordinates, and a control that demands them is one nobody ever uses. The
// coordinates are what actually get stored -- the name is only a label.

import QtQuick
import qs.config
import qs.services
import qs.modules.clipboard

Column {
    id: root

    spacing: 0

    SearchField {
        id: query
        width: parent.width
        placeholder: "Search for a town or city"
        onTextChanged: Geocoding.search(query.text)
    }

    Repeater {
        model: Geocoding.results

        SettingRow {
            id: place

            required property var modelData
            required property int index

            label: place.modelData.name
            detail: [place.modelData.admin, place.modelData.country].filter(part => !!part).join(", ")
            value: place.modelData.latitude.toFixed(2) + ", " + place.modelData.longitude.toFixed(2)
            controlWidth: 150
            divider: place.index < Geocoding.results.length - 1
            activatable: true
            onActivated: {
                if (Geocoding.choose(place.modelData))
                    query.text = "";
            }
        }
    }

    SettingRow {
        width: parent.width
        visible: Geocoding.searching || Geocoding.lastError !== ""
        label: Geocoding.searching ? "Searching…" : "No result"
        detail: Geocoding.searching ? "" : Geocoding.lastError
        divider: false
    }
}
