// Bluetooth, at page size.
//
// Paired devices first, because reconnecting to something you already own is
// what you are here for nine times out of ten; discovered devices follow.
// Battery is shown where BlueZ reports it, which is the one thing people
// routinely open a terminal for.

import QtQuick
import Quickshell
import Quickshell.Bluetooth
import qs.config
import qs.services

Column {
    id: root

    spacing: 0

    function primaryAction(device: var): void {
        if (device.connected) {
            device.disconnect();
            return;
        }
        if (device.paired) {
            device.connect();
            return;
        }
        device.pair();
    }

    function stateLabel(device: var): string {
        if (device.pairing)
            return "Pairing…";
        if (device.connected)
            return device.batteryAvailable
                ? `Connected · ${Math.round(device.battery * 100)}% battery`
                : "Connected";
        if (device.paired)
            return "Paired";
        return device.address || "Not paired";
    }

    Repeater {
        model: Connectivity.bluetoothDevices

        SettingRow {
            id: entry

            required property var modelData
            required property int index

            width: parent.width
            label: entry.modelData.name || entry.modelData.address || "Unknown device"
            detail: root.stateLabel(entry.modelData)
            divider: entry.index < Connectivity.bluetoothDevices.length - 1
            controlWidth: 200
            activatable: !entry.modelData.pairing
            onActivated: root.primaryAction(entry.modelData)

            Row {
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                spacing: 7

                SettingsButton {
                    anchors.verticalCenter: parent.verticalCenter
                    enabled: !entry.modelData.pairing
                    text: entry.modelData.connected
                        ? "Disconnect"
                        : (entry.modelData.paired ? "Connect" : "Pair")
                    onClicked: root.primaryAction(entry.modelData)
                }

                SettingsButton {
                    anchors.verticalCenter: parent.verticalCenter
                    visible: entry.modelData.paired
                    text: "Forget"
                    onClicked: entry.modelData.forget()
                }
            }
        }
    }

    SettingRow {
        width: parent.width
        visible: Connectivity.bluetoothDevices.length === 0
        label: !Connectivity.adapter
            ? "No Bluetooth adapter"
            : (Connectivity.adapter.enabled ? "Looking for devices…" : "Bluetooth is off")
        detail: Connectivity.adapter && !Connectivity.adapter.enabled
            ? "Turn it on above to discover devices"
            : "Put the device into pairing mode to make it appear"
        divider: false
    }
}
