#!/bin/bash

set -eu

background() {
    swaybg \
{% for monitor in hostvars[ansible_hostname]['monitors'] %}
        -o "{{ monitor['desc'] }}" -i ~/.config/background/{{ monitor['background']['file'] }} -m {{ monitor['background']['mode'] }} {%- if not loop.last %} \{% endif %}

{% endfor %}
}

idle() {
    swayidle -w \
        timeout 180 '~/.local/bin/hyprhelpr lock' \
        timeout 240 'hyprctl dispatch dpms off' \
        resume 'hyprctl dispatch dpms on'
}

lock() {
    if pgrep -x "swaylock" >/dev/null
    then
        echo "A instance of swaylock is already running."
        exit 1
    else
        swaylock -f -e -s fill --indicator-radius 150 \
{% for monitor in hostvars[ansible_hostname]['monitors'] %}
            -i {{ monitor['output'] }}:~/.config/background/lock-{{ monitor['background']['file'] }} {%- if not loop.last %} \{% endif %}

{% endfor %}
    fi
}

shutdown() {
    sudo systemctl poweroff
}

reboot() {
    sudo systemctl reboot
}

powermenu() {
    PM_ACTION=$(printf "Shutdown\nReboot\nLock\nLogoff" | wofi --show=dmenu -i --width 200 --height 150)
    if [ -n  "$PM_ACTION" ]; then
        case $PM_ACTION in
            Shutdown)
                shutdown
            ;;
            Reboot)
                reboot
            ;;
            Lock)
                lock
            ;;
            Logoff)
                hyprctl dispatch exit
            ;;
        esac
    fi
}

screenshot() {
    ACTION=$(printf "Area\nWindow\nScreen" | wofi --show=dmenu -i --width 200 --height 150)
    if [ -n  "$ACTION" ]; then
        case $ACTION in
            Area)
                grim -g "$(slurp)" - | swappy -f -
            ;;
            Window)
                grim -g "$(hyprctl clients -j | jq -r ".[] | select(.workspace.id | IN(["$(hyprctl monitors -j | jq '.[].activeWorkspace.id' | xargs | sed -e 's/ /,/g')"][]))" | jq -r ".at,.size" | jq -s "add" | jq '_nwise(4)' | jq -r '"\(.[0]),\(.[1]) \(.[2])x\(.[3])"' | slurp)" - | swappy -f -
            ;;
            Screen)
                grim -g "$(slurp -o)" - | swappy -f -
            ;;
        esac
    fi
}

ACTION=$1
case $ACTION in
    background)
        background
    ;;
    idle)
        idle
    ;;
    lock)
        lock
    ;;
    shutdown)
        shutdown
    ;;
    reboot)
        reboot
    ;;
    powermenu)
        powermenu
    ;;
    screenshot)
        screenshot
    ;;
    help | -h | --help)
        echo "Usage: hyprhelpr <action> [parameters]"
        echo "Available actions:"
        echo "    background   Start swaybg with the configured backgrounds."
        echo "    idle         Start swayidle with the configured actions."
        echo "    lock         Lock the current session using swaylock."
        echo "    shutdown     Shut down the system."
        echo "    reboot       Reboot the system."
        echo "    powermenu    Show wofi in dmenu mode with different power options"
        echo "    screenshot   Show wofi in dmenu mode with different screenshot options"
        echo "    help         Show this help text."
    ;;
    *)
        echo "Invalid action \"$ACTION\"."' Use "hyprhelpr help" to get help.'
        exit 1
esac