#!/usr/bin/env bash

# One stable command boundary for launcher actions. Vicinae scripts stay tiny,
# while the actual mapping to Quickshell IPC remains testable in one place.

set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
action="${1:-}"

if command -v panama-osd >/dev/null 2>&1; then
    osd_command=(panama-osd)
else
    osd_command=("$script_dir/panama-osd")
fi

if command -v panama-prism-gallery >/dev/null 2>&1; then
    gallery_command=(panama-prism-gallery)
else
    gallery_command=("$script_dir/panama-prism-gallery")
fi

report_failure() {
    local status=$?
    trap - EXIT
    if (( status != 0 )) && command -v notify-send >/dev/null 2>&1; then
        notify-send -a Panama -i dialog-error-symbolic \
            "Panama action failed" \
            "The “${action//-/ }” action could not be completed." || true
    fi
    exit "$status"
}
trap report_failure EXIT

show_state() {
    local state="$1" on_icon="$2" off_icon="$3" label="$4"
    case "$state" in
        true) "${osd_command[@]}" message "$on_icon" "$label On" ;;
        false) "${osd_command[@]}" message "$off_icon" "$label Off" ;;
        *)
            printf 'Panama action returned an invalid state: %s\n' "$state" >&2
            return 1
            ;;
    esac
}

toggle_state() {
    local target="$1" on_icon="$2" off_icon="$3" label="$4" state
    state="$(qs ipc call "$target" toggle)"
    show_state "$state" "$on_icon" "$off_icon" "$label"
}

case "$action" in
    control-center) qs ipc call quicksettings open ;;
    notifications) qs ipc call notifications open ;;
    calendar) qs ipc call calendar-agenda open ;;
    clipboard) qs ipc call clipboard open ;;
    overview) qs ipc call overview open ;;
    settings) qs ipc call settings open ;;

    dnd)
        state="$(qs ipc call notifications dnd)"
        show_state "$state" notifications-disabled-symbolic notifications-symbolic "Do Not Disturb"
        ;;
    caffeine)
        toggle_state caffeine weather-clear-symbolic weather-clear-night-symbolic Caffeine
        ;;
    night-light)
        toggle_state night-light night-light-symbolic night-light-disabled-symbolic "Night Light"
        ;;
    focus-start)
        qs ipc call focus start
        "${osd_command[@]}" message preferences-system-time-symbolic "Focus Session Started"
        ;;
    focus-end)
        qs ipc call focus end
        "${osd_command[@]}" message media-playback-stop-symbolic "Focus Session Ended"
        ;;

    capture) qs ipc call capture open ;;
    intelligence) qs ipc call screen-intelligence open ;;
    screenshot) qs ipc call capture screenNow ;;
    microphone) "${osd_command[@]}" microphone toggle ;;

    gallery) "${gallery_command[@]}" ;;
    restart-shell)
        qs kill
        quickshell --daemonize
        ;;
    *)
        printf 'Usage: panama-action {%s}\n' \
            'control-center|notifications|calendar|clipboard|overview|settings|dnd|caffeine|night-light|focus-start|focus-end|capture|intelligence|screenshot|microphone|gallery|restart-shell' >&2
        exit 2
        ;;
esac
