#!/usr/bin/env bash
set -euo pipefail

notify() {
    notify-send "󰂯 Bluetooth" "$1"
}

require_bluetoothctl() {
    if ! command -v bluetoothctl >/dev/null 2>&1; then
        notify "bluetoothctl ist nicht installiert."
        exit 1
    fi
}

wofi_pick() {
    wofi --dmenu --prompt "$1" --insensitive
}

adapter_powered() {
    { bluetoothctl show 2>/dev/null || true; } | awk -F': ' '/Powered/ {print $2; exit}'
}

device_name() {
    local mac="$1"
    bluetoothctl info "$mac" 2>/dev/null | awk -F': ' '/Name/ {print $2; exit}'
}

pick_device() {
    local prompt="$1"
    bluetoothctl devices |
        awk '{mac=$2; $1=""; $2=""; sub(/^  */, ""); printf "󰂯  %s  [%s]\n", $0, mac}' |
        wofi_pick "$prompt"
}

selected_mac() {
    sed -n 's/.*\[\([0-9A-Fa-f:]\{17\}\)\].*/\1/p'
}

scan_devices() {
    notify "Scan laeuft fuer 8 Sekunden."
    timeout 8 bluetoothctl scan on >/dev/null 2>&1 || true
    bluetoothctl scan off >/dev/null 2>&1 || true

    local selection mac name
    selection="$(pick_device "󰂯  Geraet verbinden")"
    [ -n "$selection" ] || exit 0
    mac="$(printf '%s\n' "$selection" | selected_mac)"
    name="$(device_name "$mac")"
    [ -n "$name" ] || name="$mac"

    bluetoothctl pair "$mac" >/dev/null 2>&1 || true
    bluetoothctl trust "$mac" >/dev/null 2>&1 || true
    bluetoothctl connect "$mac" && notify "Verbunden mit $name."
}

connect_saved_device() {
    local selection mac name
    selection="$(pick_device "󰛳  Gekoppeltes Geraet verbinden")"
    [ -n "$selection" ] || exit 0
    mac="$(printf '%s\n' "$selection" | selected_mac)"
    name="$(device_name "$mac")"
    [ -n "$name" ] || name="$mac"
    bluetoothctl connect "$mac" && notify "Verbunden mit $name."
}

disconnect_device() {
    local selection mac name
    selection="$(
        bluetoothctl devices Connected |
            awk '{mac=$2; $1=""; $2=""; sub(/^  */, ""); printf "󰂯  %s  [%s]\n", $0, mac}' |
            wofi_pick "󰅖  Bluetooth trennen"
    )"

    [ -n "$selection" ] || exit 0
    mac="$(printf '%s\n' "$selection" | selected_mac)"
    name="$(device_name "$mac")"
    [ -n "$name" ] || name="$mac"
    bluetoothctl disconnect "$mac" && notify "$name getrennt."
}

remove_device() {
    local selection mac name
    selection="$(pick_device "󰆴  Geraet entfernen")"
    [ -n "$selection" ] || exit 0
    mac="$(printf '%s\n' "$selection" | selected_mac)"
    name="$(device_name "$mac")"
    [ -n "$name" ] || name="$mac"
    bluetoothctl remove "$mac" && notify "$name entfernt."
}

require_bluetoothctl

power="$(adapter_powered)"
[ -n "$power" ] || power="unknown"

choice="$(
    printf '%s\n' \
        "󰂯  Geraet suchen und verbinden" \
        "󰂲  Bluetooth ein/aus" \
        "󰛳  Gekoppelte Geraete" \
        "󰅖  Verbundenes Geraet trennen" \
        "󰆴  Geraet entfernen" \
        "󰩟  Status: $power" |
        wofi_pick "󰂯  Bluetooth"
)"

case "$choice" in
    *"Geraet suchen"*)
        bluetoothctl power on >/dev/null
        bluetoothctl agent on >/dev/null 2>&1 || true
        bluetoothctl default-agent >/dev/null 2>&1 || true
        scan_devices
        ;;
    *"Bluetooth ein/aus"*)
        if [ "$power" = "yes" ]; then
            bluetoothctl power off && notify "Bluetooth ausgeschaltet."
        else
            bluetoothctl power on && notify "Bluetooth eingeschaltet."
        fi
        ;;
    *"Gekoppelte Geraete"*)
        connect_saved_device
        ;;
    *"Verbundenes Geraet trennen"*)
        disconnect_device
        ;;
    *"Geraet entfernen"*)
        remove_device
        ;;
    *"Status:"*)
        bluetoothctl show | wofi --dmenu --prompt "󰩟  Bluetoothstatus"
        ;;
esac
