#!/bin/bash

OMARCHY_BIN_PATH=~/.local/share/omarchy/bin

menu() {
  local prompt="$1"
  local options="$2"
  local extra="$3"
  local preselect="$4"

  read -r -a args <<<"$extra"

  if [[ -n "$preselect" ]]; then
    local index
    index=$(echo -e "$options" | grep -nxF "$preselect" | cut -d: -f1)
    if [[ -n "$index" ]]; then
      args+=("-a" "$index")
    fi
  fi

  echo -e "$options" | walker --dmenu --theme dmenu_250 -p "$prompt…" "${args[@]}"
}

terminal() {
  alacritty --class Omarchy -e $1
}

present_terminal() {
  alacritty --class Omarchy -e bash -c "~/.local/share/omarchy/bin/omarchy-show-logo; eval \"$1\"; ~/.local/share/omarchy/bin/omarchy-show-done;"
}

edit_in_nvim() {
  notify-send "Editing config file" "$1"
  alacritty -e nvim "$1"
}

open_web() {
  setsid chromium --new-window --app="$1" &
}

install() {
  present_terminal "echo 'Installing $1...'; yay -Sy --noconfirm $2"
}

install_and_launch() {
  present_terminal "echo 'Installing $1...'; yay -Sy --noconfirm $2 && setsid gtk-launch $3"
}

install_font() {
  present_terminal "echo 'Installing $1...'; yay -Sy --noconfirm --needed $2 && sleep 2 && ~/.local/share/omarchy/bin/omarchy-font-set '$3'"
}

show_learn_menu() {
  case $(menu "Learn" "  Keybindings\n  Omarchy\n  Hyprland\n󰣇  Arch\n  Neovim\n󱆃  Bash") in
  *Keybindings*) $OMARCHY_BIN_PATH/omarchy-menu-keybindings ;;
  *Omarchy*) open_web "https://manuals.omamix.org/2/the-omarchy-manual" ;;
  *Hyprland*) open_web "https://wiki.hypr.land/" ;;
  *Arch*) open_web "https://wiki.archlinux.org/title/Main_page" ;;
  *Bash*) open_web "https://devhints.io/bash" ;;
  *Neovim*) open_web "https://www.lazyvim.org/keymaps" ;;
  *) show_main_menu ;;
  esac
}

show_style_menu() {
  case $(menu "Style" "󰸌  Theme\n  Font\n  Background") in
  *Theme*) show_theme_menu ;;
  *Font*) show_font_menu ;;
  *Background*) $OMARCHY_BIN_PATH/omarchy-theme-bg-next ;;
  *) show_main_menu ;;
  esac
}

show_theme_menu() {
  theme=$(menu "Theme" "$($OMARCHY_BIN_PATH/omarchy-theme-list)" "" "$($OMARCHY_BIN_PATH/omarchy-theme-current)")
  if [[ "$theme" == "CNCLD" || -z "$theme" ]]; then
    show_main_menu
  else
    $OMARCHY_BIN_PATH/omarchy-theme-set "$theme"
  fi
}

show_font_menu() {
  theme=$(menu "Font" "$($OMARCHY_BIN_PATH/omarchy-font-list)" "-w 350" "$($OMARCHY_BIN_PATH/omarchy-font-current)")
  if [[ "$theme" == "CNCLD" || -z "$theme" ]]; then
    show_main_menu
  else
    $OMARCHY_BIN_PATH/omarchy-font-set "$theme"
  fi
}

show_capture_menu() {
  case $(menu "Capture" "  Screenshot\n  Screenrecord\n󰃉  Color") in
  *Screenshot*) show_screenshot_menu ;;
  *Screenrecord*) show_screenrecord_menu ;;
  *Color*) pkill hyprpicker || hyprpicker -a ;;
  *) show_main_menu ;;
  esac
}

show_screenshot_menu() {
  case $(menu "Screenshot" "  Region\n  Window\n  Display") in
  *Region*) $OMARCHY_BIN_PATH/omarchy-cmd-screenshot ;;
  *Window*) $OMARCHY_BIN_PATH/omarchy-cmd-screenshot window ;;
  *Display*) $OMARCHY_BIN_PATH/omarchy-cmd-screenshot output ;;
  *) show_capture_menu ;;
  esac
}

show_screenrecord_menu() {
  case $(menu "Screenrecord" "  Region\n  Display") in
  *Region*) $OMARCHY_BIN_PATH/omarchy-cmd-screenrecord ;;
  *Display*) $OMARCHY_BIN_PATH/omarchy-cmd-screenrecord output ;;
  *) show_capture_menu ;;
  esac
}

show_toggle_menu() {
  case $(menu "Toggle" "󱄄  Screensaver\n󰔎  Nightlight\n󱫖  Idle Lock\n󰍜  Top Bar") in
  *Screensaver*) $OMARCHY_BIN_PATH/omarchy-launch-screensaver ;;
  *Nightlight*) $OMARCHY_BIN_PATH/omarchy-toggle-nightlight ;;
  *Idle*) $OMARCHY_BIN_PATH/omarchy-toggle-idle ;;
  *Bar*) pkill -SIGUSR1 waybar ;;
  *) show_main_menu ;;
  esac
}

show_setup_menu() {
  local options="  Audio\n  Wifi\n󰂯  Bluetooth\n󱐋  Power Profile\n󰍹  Monitors"
  [ -f ~/.config/hypr/bindings.conf ] && options="$options\n  Keybindings"
  [ -f ~/.config/hypr/input.conf ] && options="$options\n  Input"
  options="$options\n  Config\n󰈷  Fingerprint\n  Fido2"

  case $(menu "Setup" "$options") in
  *Audio*) alacritty --class=Wiremix -e wiremix ;;
  *Wifi*) alacritty --class=Impala -e impala ;;
  *Bluetooth*) blueberry ;;
  *Power*) show_setup_power_menu ;;
  *Monitors*) edit_in_nvim ~/.config/hypr/monitors.conf ;;
  *Keybindings*) edit_in_nvim ~/.config/hypr/bindings.conf ;;
  *Input*) edit_in_nvim ~/.config/hypr/input.conf ;;
  *Config*) show_setup_config_menu ;;
  *Fingerprint*) present_terminal $OMARCHY_BIN_PATH/omarchy-setup-fingerprint ;;
  *Fido2*) present_terminal $OMARCHY_BIN_PATH/omarchy-setup-fido2 ;;
  *) show_main_menu ;;
  esac
}

show_setup_power_menu() {
  profile=$(menu "Power Profile" "$($OMARCHY_BIN_PATH/omarchy-powerprofiles-list)" "" "$(powerprofilesctl get)")

  if [[ "$profile" == "CNCLD" || -z "$profile" ]]; then
    show_main_menu
  else
    powerprofilesctl set "$profile"
  fi
}

show_setup_config_menu() {
  case $(menu "Setup" "  Hyprland\n  Hypridle\n  Hyprlock\n  Hyprsunset\n  Swayosd\n󰌧  Walker\n󰍜  Waybar\n󰞅  XCompose") in
  *Hyprland*) edit_in_nvim ~/.config/hypr/hyprland.conf ;;
  *Hypridle*) edit_in_nvim ~/.config/hypr/hypridle.conf && ~/.local/share/omarchy/bin/omarchy-restart-hypridle ;;
  *Hyprlock*) edit_in_nvim ~/.config/hypr/hyprlock.conf ;;
  *Hyprsunset*) edit_in_nvim ~/.config/hypr/hyprsunset.conf && ~/.local/share/omarchy/bin/omarchy-restart-hyprsunset ;;
  *Swayosd*) edit_in_nvim ~/.config/swayosd/config.toml && ~/.local/share/omarchy/bin/omarchy-restart-swayosd ;;
  *Walker*) edit_in_nvim ~/.config/walker/config.toml && ~/.local/share/omarchy/bin/omarchy-restart-walker ;;
  *Waybar*) edit_in_nvim ~/.config/waybar/config.jsonc && ~/.local/share/omarchy/bin/omarchy-restart-waybar ;;
  *XCompose*) edit_in_nvim ~/.XCompose && ~/.local/share/omarchy/bin/omarchy-restart-xcompose ;;
  *) show_main_menu ;;
  esac
}

show_install_menu() {
  case $(menu "Install" "󰣇  Package\n  Web App\n  Service\n  Style\n󰵮  Development\n  Editor\n󱚤  AI") in
  *Package*) terminal $OMARCHY_BIN_PATH/omarchy-pkg-install ;;
  *Web*) present_terminal $OMARCHY_BIN_PATH/omarchy-webapp-install ;;
  *Service*) show_install_service_menu ;;
  *Style*) show_install_style_menu ;;
  *Development*) show_install_development_menu ;;
  *Editor*) show_install_editor_menu ;;
  *AI*) show_install_ai_menu ;;
  *) show_main_menu ;;
  esac
}

show_install_service_menu() {
  case $(menu "Install" "  Dropbox\n  Steam\n  Tailscale") in
  *Dropbox*) present_terminal $OMARCHY_BIN_PATH/omarchy-install-dropbox ;;
  *Steam*) present_terminal $OMARCHY_BIN_PATH/omarchy-install-steam ;;
  *Tailscale*) present_terminal $OMARCHY_BIN_PATH/omarchy-install-tailscale ;;
  *) show_install_menu ;;
  esac
}

show_install_editor_menu() {
  case $(menu "Install" "  VSCode\n  Cursor\n  Zed\n  Sublime Text\n  Helix") in
  *VSCode*) install_and_launch "VSCode" "visual-studio-code-bin" "code" ;;
  *Cursor*) install_and_launch "Cursor" "cursor-bin" "cursor-cursor" ;;
  *Zed*) install_and_launch "Zed" "zed" "dev.zed.Zed" ;;
  *Sublime*) install_and_launch "Sublime Text" "sublime-text-4" "sublime_text" ;;
  *Helix*) install_and_launch "Helix" "helix" "helix" ;;
  *) show_install_menu ;;
  esac
}

show_install_ai_menu() {
  case $(menu "Install" "󱚤  Claude Code\n󱚤  Gemini\n󱚤  LM Studio\n󱚤  Ollama\n󱚤  Crush\n󱚤  opencode") in
  *Claude*) install "Claude Code" "claude-code" ;;
  *Gemini*) install "Gemini" "gemini-cli-bin" ;;
  *Studio*) install "LM Studio" "lmstudio" ;;
  *Ollama*) install "Ollama" "ollama" ;;
  *Crush*) install "Crush" "crush-bin" ;;
  *opencode*) install "opencode" "opencode-bin" ;;
  *) show_install_menu ;;
  esac
}

show_install_style_menu() {
  case $(menu "Install" "󰸌  Theme\n  Background\n  Font") in
  *Theme*) present_terminal $OMARCHY_BIN_PATH/omarchy-theme-install ;;
  *Background*) nautilus ~/.config/omarchy/current/theme/backgrounds ;;
  *Font*) show_install_font_menu ;;
  *) show_install_menu ;;
  esac
}

show_install_font_menu() {
  case $(menu "Install" "  Meslo LG Mono\n  Fira Code\n  Victor Code\n  Bistream Vera Mono" "-w 350") in
  *Meslo*) install_font "Meslo LG Mono" "ttf-meslo-nerd" "MesloLGL Nerd Font" ;;
  *Fira*) install_font "Fira Code" "ttf-firacode-nerd" "FiraCode Nerd Font" ;;
  *Victor*) install_font "Victor Code" "ttf-victor-mono-nerd" "VictorMono Nerd Font" ;;
  *Bistream*) install_font "Bistream Vera Code" "ttf-bitstream-vera-mono-nerd" "BitstromWera Nerd Font" ;;
  *) show_install_menu ;;
  esac
}

show_install_development_menu() {
  case $(menu "Install" "󰫏  Ruby on Rails\n  Docker DB\n  Node.js\n  Go\n  Laravel\n  Python\n  Elixir") in
  *Rails*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env ruby" ;;
  *Docker*) present_terminal $OMARCHY_BIN_PATH/omarchy-install-docker-dbs ;;
  *Node*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env node" ;;
  *Go*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env go" ;;
  *Laravel*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env laravel" ;;
  *Python*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env python" ;;
  *Elixir*) present_terminal "$OMARCHY_BIN_PATH/omarchy-install-dev-env elixir" ;;
  *) show_install_menu ;;
  esac
}

show_remove_menu() {
  case $(menu "Remove" "󰣇  Package\n  Web App\n󰸌  Theme\n󰈷  Fingerprint\n  Fido2") in
  *Package*) terminal $OMARCHY_BIN_PATH/omarchy-pkg-remove ;;
  *Web*) present_terminal $OMARCHY_BIN_PATH/omarchy-webapp-remove ;;
  *Theme*) present_terminal $OMARCHY_BIN_PATH/omarchy-theme-remove ;;
  *Fingerprint*) present_terminal "$OMARCHY_BIN_PATH/omarchy-setup-fingerprint --remove" ;;
  *Fido2*) present_terminal "$OMARCHY_BIN_PATH/omarchy-setup-fido2 --remove" ;;
  *) show_main_menu ;;
  esac
}

show_update_menu() {
  case $(menu "Update" "󰣇  Omarchy\n  Config\n  Process\n󰸌  Themes\n  Timezone") in
  *Omarchy*) present_terminal $OMARCHY_BIN_PATH/omarchy-update ;;
  *Config*) show_update_config_menu ;;
  *Process*) show_update_process_menu ;;
  *Themes*) present_terminal $OMARCHY_BIN_PATH/omarchy-theme-update ;;
  *Timezone*) $OMARCHY_BIN_PATH/omarchy-cmd-tzupdate ;;
  *) show_main_menu ;;
  esac
}

show_update_process_menu() {
  case $(menu "Restart" "  Hypridle\n  Hyprsunset\n  Swayosd\n󰌧  Walker\n󰍜  Waybar") in
  *Hypridle*) $OMARCHY_BIN_PATH/omarchy-restart-hypridle ;;
  *Hyprsunset*) $OMARCHY_BIN_PATH/omarchy-restart-hyprsunset ;;
  *Swayosd*) $OMARCHY_BIN_PATH/omarchy-restart-swayosd ;;
  *Walker*) $OMARCHY_BIN_PATH/omarchy-restart-walker ;;
  *Waybar*) $OMARCHY_BIN_PATH/omarchy-restart-waybar ;;
  *) show_main_menu ;;
  esac
}

show_update_config_menu() {
  case $(menu "Use default config" "  Hyprland\n  Hypridle\n  Hyprlock\n  Hyprsunset\n󱣴  Plymouth\n  Swayosd\n󰌧  Walker\n󰍜  Waybar") in
  *Hyprland*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-hyprland ;;
  *Hypridle*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-hypridle ;;
  *Hyprlock*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-hyprlock ;;
  *Hyprsunset*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-hyprsunset ;;
  *Plymouth*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-plymouth ;;
  *Swayosd*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-swayosd ;;
  *Walker*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-walker ;;
  *Waybar*) present_terminal $OMARCHY_BIN_PATH/omarchy-refresh-waybar ;;
  *) show_main_menu ;;
  esac
}

show_system_menu() {
  case $(menu "System" "  Lock\n󰤄  Suspend\n  Relaunch\n󰜉  Restart\n󰐥  Shutdown") in
  *Lock*) $OMARCHY_BIN_PATH/omarchy-lock-screen ;;
  *Suspend*) systemctl suspend ;;
  *Relaunch*) uwsm stop ;;
  *Restart*) systemctl reboot ;;
  *Shutdown*) systemctl poweroff ;;
  *) show_main_menu ;;
  esac
}

show_main_menu() {
  go_to_menu "$(menu "Go" "󰀻  Apps\n󰧑  Learn\n  Capture\n󰔎  Toggle\n  Style\n  Setup\n󰉉  Install\n󰭌  Remove\n  Update\n  About\n  System")"
}

go_to_menu() {
  case "${1,,}" in
  *apps*) walker -p "Launch…" ;;
  *learn*) show_learn_menu ;;
  *style*) show_style_menu ;;
  *theme*) show_theme_menu ;;
  *capture*) show_capture_menu ;;
  *screenshot*) show_screenshot_menu ;;
  *screenrecord*) show_screenrecord_menu ;;
  *toggle*) show_toggle_menu ;;
  *setup*) show_setup_menu ;;
  *install*) show_install_menu ;;
  *remove*) show_remove_menu ;;
  *update*) show_update_menu ;;
  *system*) show_system_menu ;;
  *about*) gtk-launch About.desktop ;;
  esac
}

if [[ -n "$1" ]]; then
  go_to_menu "$1"
else
  show_main_menu
fi
