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

notify() {
    notify-send "󰤨 Netzwerk" "$1"
}

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

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

wifi_status() {
    nmcli -t -f WIFI general | head -n 1
}

active_connection() {
    nmcli -t -f NAME,TYPE connection show --active | awk -F: '$2 ~ /wireless|ethernet/ {print $1; exit}'
}

connect_wifi() {
    nmcli radio wifi on
    nmcli device wifi rescan >/dev/null 2>&1 || true

    local selection ssid security password
    selection="$(
        nmcli -t -f IN-USE,SSID,SECURITY,SIGNAL device wifi list |
            awk -F: '
                $2 != "" {
                    icon = $1 == "*" ? "󰁥" : "󰤨"
                    lock = $3 == "" ? "offen" : "gesichert"
                    printf "%s  %s  [%s, %s%%]\n", icon, $2, lock, $4
                }
            ' |
            wofi_pick "󰤨  WLAN verbinden"
    )"

    [ -n "$selection" ] || exit 0
    ssid="$(printf '%s\n' "$selection" | sed -E 's/^[^ ]+  //; s/  \[.*$//')"
    security="$(nmcli -t -f SSID,SECURITY device wifi list | awk -F: -v ssid="$ssid" '$1 == ssid {print $2; exit}')"

    if [ -n "$security" ]; then
        password="$(printf '' | wofi --dmenu --password --prompt "󰌾  Passwort fuer $ssid")"
        [ -n "$password" ] || exit 0
        nmcli device wifi connect "$ssid" password "$password" && notify "Verbunden mit $ssid."
    else
        nmcli device wifi connect "$ssid" && notify "Verbunden mit $ssid."
    fi
}

saved_connections() {
    local selection name
    selection="$(
        nmcli -t -f NAME,TYPE connection show |
            awk -F: '$2 ~ /wireless|ethernet/ {printf "󰛳  %s\n", $1}' |
            wofi_pick "󰛳  Gespeicherte Verbindungen"
    )"

    [ -n "$selection" ] || exit 0
    name="${selection#*  }"
    nmcli connection up "$name" && notify "Verbindung $name gestartet."
}

disconnect_network() {
    local conn
    conn="$(active_connection)"

    if [ -z "$conn" ]; then
        notify "Keine aktive Netzwerkverbindung gefunden."
        exit 0
    fi

    nmcli connection down "$conn" && notify "Verbindung $conn getrennt."
}

require_nmcli

current="$(active_connection)"
[ -n "$current" ] || current="Nicht verbunden"

choice="$(
    printf '%s\n' \
        "󰤨  WLAN verbinden" \
        "󰖩  WLAN ein/aus" \
        "󰛳  Gespeicherte Verbindungen" \
        "󰅖  Aktive Verbindung trennen" \
        "󰑓  Netzwerk neu scannen" \
        "󰩟  Status: $current" |
        wofi_pick "󰤨  Netzwerk"
)"

case "$choice" in
    *"WLAN verbinden"*)
        connect_wifi
        ;;
    *"WLAN ein/aus"*)
        if [ "$(wifi_status)" = "enabled" ]; then
            nmcli radio wifi off && notify "WLAN ausgeschaltet."
        else
            nmcli radio wifi on && notify "WLAN eingeschaltet."
        fi
        ;;
    *"Gespeicherte Verbindungen"*)
        saved_connections
        ;;
    *"Aktive Verbindung trennen"*)
        disconnect_network
        ;;
    *"Netzwerk neu scannen"*)
        nmcli device wifi rescan && notify "WLAN-Scan gestartet."
        ;;
    *"Status:"*)
        nmcli general status | wofi --dmenu --prompt "󰩟  Netzwerkstatus"
        ;;
esac
