#!/usr/bin/env bash
# rofi-based Omarchy menu. Replaces Omarchy v3's walker-driven omarchy-menu.
# Usage: omarchy-menu [system|theme|background|capture|keybindings]
set -euo pipefail

OMARCHY_PATH="${OMARCHY_PATH:-$HOME/.local/share/omarchy}"
CURRENT="$HOME/.config/omarchy/current"
PICK=(rofi -dmenu -i -p "" -theme-str 'window { width: 22%; } listview { lines: 8; }')

menu_main() {
  local choice
  choice="$(printf '%s\n' Apps Themes Backgrounds Capture System Keybindings | "${PICK[@]}")" || exit 0
  case "$choice" in
    Apps) rofi -show drun -show-icons ;;
    Themes) menu_theme ;;
    Backgrounds) omarchy-theme-bg-next ;;
    Capture) menu_capture ;;
    System) menu_system ;;
    Keybindings) menu_keybindings ;;
  esac
}

menu_system() {
  local choice
  choice="$(printf '%s\n' Lock Logout Suspend Reboot Shutdown | "${PICK[@]}")" || exit 0
  case "$choice" in
    Lock) omarchy-system-lock ;;
    Logout) hyprctl dispatch exit ;;
    Suspend) systemctl suspend ;;
    Reboot) systemctl reboot ;;
    Shutdown) systemctl poweroff ;;
  esac
}

menu_theme() {
  local choice
  choice="$(find "$OMARCHY_PATH/themes" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort | "${PICK[@]}")" || exit 0
  [ -n "$choice" ] && omarchy-theme-set "$choice"
}

menu_capture() {
  local choice
  choice="$(printf '%s\n' Region Window Output | "${PICK[@]}")" || exit 0
  case "$choice" in
    Region) omarchy-capture-screenshot region ;;
    Window) omarchy-capture-screenshot window ;;
    Output) omarchy-capture-screenshot output ;;
  esac
}

menu_keybindings() {
  alacritty -e less ~/.config/hypr/bindings.conf
}

case "${1:-}" in
  system) menu_system ;;
  theme) menu_theme ;;
  background) omarchy-theme-bg-next ;;
  capture) menu_capture ;;
  keybindings) menu_keybindings ;;
  "" | *) menu_main ;;
esac
