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

notify() {
    notify-send "󰍹 Display" "$1"
}

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

connected_monitors() {
    hyprctl monitors all 2>/dev/null | awk '
        /^Monitor / {
            name=$2
            disabled=0
        }
        /disabled: true/ {
            disabled=1
        }
        /^$/ && name != "" {
            if (!disabled) print name
            name=""
        }
        END {
            if (name != "" && !disabled) print name
        }
    '
}

all_monitors() {
    hyprctl monitors all 2>/dev/null | awk '/^Monitor / {print $2}'
}

first_monitor() {
    connected_monitors | head -n 1
}

second_monitor() {
    connected_monitors | sed -n '2p'
}

show_status() {
    hyprctl monitors all | wofi --dmenu --prompt "󰩟  Displaystatus"
}

choose_monitor() {
    local monitors
    monitors="$(all_monitors)"

    if [ -z "$monitors" ]; then
        notify "Keine Monitore ueber hyprctl gefunden."
        exit 0
    fi

    printf '%s\n' "$monitors" | awk '{print "󰍹  " $0}' | wofi_pick "$1"
}

disable_monitor() {
    local selection monitor
    selection="$(choose_monitor "󰍹  Monitor deaktivieren")"
    [ -n "$selection" ] || exit 0
    monitor="${selection#*  }"
    hyprctl keyword monitor "$monitor,disable" && notify "$monitor deaktiviert."
}

enable_preferred() {
    local selection monitor
    selection="$(choose_monitor "󰍹  Monitor aktivieren")"
    [ -n "$selection" ] || exit 0
    monitor="${selection#*  }"
    hyprctl keyword monitor "$monitor,preferred,auto,1" && notify "$monitor aktiviert."
}

extend_right() {
    local primary secondary
    primary="$(first_monitor)"
    secondary="$(second_monitor)"

    if [ -z "$primary" ] || [ -z "$secondary" ]; then
        notify "Dafuer muessen mindestens zwei aktive Monitore vorhanden sein."
        exit 0
    fi

    hyprctl keyword monitor "$primary,preferred,0x0,1"
    hyprctl keyword monitor "$secondary,preferred,auto-right,1"
    notify "Displays erweitert."
}

mirror_displays() {
    local primary secondary
    primary="$(first_monitor)"
    secondary="$(second_monitor)"

    if [ -z "$primary" ] || [ -z "$secondary" ]; then
        notify "Dafuer muessen mindestens zwei aktive Monitore vorhanden sein."
        exit 0
    fi

    hyprctl keyword monitor "$primary,preferred,0x0,1"
    hyprctl keyword monitor "$secondary,preferred,0x0,1,mirror,$primary"
    notify "Displays gespiegelt."
}

choice="$(
    printf '%s\n' \
        "󰍹  Status anzeigen" \
        "󰑓  Display-Konfig neu laden" \
        "󰹑  Monitor aktivieren" \
        "󰶐  Monitor deaktivieren" \
        "󰹑  Displays erweitern" \
        "󰹑  Displays spiegeln" \
        "󰍹  Grafisches Display-Tool" |
        wofi_pick "󰍹  Display"
)"

case "$choice" in
    *"Status anzeigen"*)
        show_status
        ;;
    *"Display-Konfig neu laden"*)
        hyprctl reload
        ;;
    *"Monitor aktivieren"*)
        enable_preferred
        ;;
    *"Monitor deaktivieren"*)
        disable_monitor
        ;;
    *"Displays erweitern"*)
        extend_right
        ;;
    *"Displays spiegeln"*)
        mirror_displays
        ;;
    *"Grafisches Display-Tool"*)
        if command -v nwg-displays >/dev/null 2>&1; then
            nwg-displays
        elif command -v wdisplays >/dev/null 2>&1; then
            wdisplays
        else
            notify "Installiere nwg-displays oder wdisplays fuer ein grafisches Display-Tool."
        fi
        ;;
esac
