#!/bin/bash

# omarchy: Main TUI menu for Omarchy tools
# Usage: omarchy

RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
BIN_DIR="$(dirname "$0")"
OMARCHY_ASCII_SHOWN=0

show_ascii_art() {
  cat <<'EOF'
 ▄██████▄    ▄▄▄▄███▄▄▄▄      ▄████████    ▄████████  ▄████████    ▄█    █▄    ▄██   ▄  
███    ███ ▄██▀▀▀███▀▀▀██▄   ███    ███   ███    ███ ███    ███   ███    ███   ███   ██▄
███    ███ ███   ███   ███   ███    ███   ███    ███ ███    █▀    ███    ███   ███▄▄▄███
███    ███ ███   ███   ███   ███    ███  ▄███▄▄▄▄██▀ ███         ▄███▄▄▄▄███▄▄ ▀▀▀▀▀▀███
███    ███ ███   ███   ███ ▀███████████ ▀▀███▀▀▀▀▀   ███        ▀▀███▀▀▀▀███▀  ▄██   ███
███    ███ ███   ███   ███   ███    ███ ▀███████████ ███    █▄    ███    ███   ███   ███
███    ███ ███   ███   ███   ███    ███   ███    ███ ███    ███   ███    ███   ███   ███
 ▀██████▀   ▀█   ███   █▀    ███    █▀    ███    ███ ████████▀    ███    █▀     ▀█████▀ 
                                          ███    ███                                    
EOF
}

show_help() {
  cat <<EOF
Omarchy CLI - Usage:

  omarchy [command]

Available commands:
  omarchy                       Launch the Omarchy TUI menu
  -v, --version, version        Show Omarchy version
  -h, --help, help              Show this help menu

  update                        Update Omarchy
  refresh-waybar                Refresh waybar
  theme-install <git-url>       Install a theme from a git repo
  theme-remove <theme-name>     Remove a theme by name
EOF
}

show_version() {
  BLUE='\033[0;34m'
  NC='\033[0m'
  (
    cd ~/.local/share/omarchy 2>/dev/null
    tag=$(git describe --tags --abbrev=0 2>/dev/null)
    echo -e "${BLUE}Omarchy Version:${NC} $tag"
  )
}

install_theme_prompt() {
  local url
  url=$(gum input --placeholder="Git repo URL for theme" --header="Enter theme git repo URL:") || theme_menu
  if [[ -n "$url" ]]; then
    "$BIN_DIR/omarchy-theme-install" "$url"
  fi
  theme_menu
}

remove_theme_prompt() {
  local theme
  theme=$(gum input --placeholder="Theme name" --header="Enter the theme name to remove:") || theme_menu
  if [[ -n "$theme" ]]; then
    "$BIN_DIR/omarchy-theme-remove" "$theme"
  fi
  theme_menu
}

# Menu functions
show_tui_menu() {
  local columns choice
  columns=$(tput cols 2>/dev/null || echo 80)
  
  if [[ $OMARCHY_ASCII_SHOWN -eq 0 ]]; then
    if [[ $columns -ge 100 ]]; then
      echo 
        show_ascii_art
    else
      echo "Omarchy"
    fi
    echo
    OMARCHY_ASCII_SHOWN=1
  fi

  local main_menu=("System" "Theme" "Tools" "Exit")
  choice=$(printf "%s\n" "${main_menu[@]}" | gum choose --header="Select a category:") || exit 0
  case "$choice" in
    System) system_menu ;;
    Theme)  theme_menu  ;;
    Tools)  tools_menu  ;;
    Exit)   exit 0      ;;
  esac
}

system_menu() {
  local menu=("Version" "Update" "Refresh Waybar" "Back")
  local commands=(
    "show_version"
    "$BIN_DIR/omarchy-update"
    "$BIN_DIR/omarchy-refresh-waybar"
    "show_tui_menu"
  )
  local choice
  choice=$(printf "%s\n" "${menu[@]}" | gum choose --header="System:") || show_tui_menu
  for i in "${!menu[@]}"; do
    if [[ "${menu[$i]}" == "$choice" ]]; then
      if [[ "$choice" == "Back" ]]; then
        show_tui_menu
      else
        if [[ "${commands[$i]}" == "show_version" ]]; then
          show_version
        else
          eval "${commands[$i]}"
        fi
      fi
      break
    fi
  done
}

theme_menu() {
  local menu=("Switch Theme" "Install Theme" "Remove Theme" "Back")
  local commands=(
    "$BIN_DIR/omarchy-theme-menu"
    "install_theme_prompt"
    "remove_theme_prompt"
    "show_tui_menu"
  )
  local choice
  choice=$(printf "%s\n" "${menu[@]}" | gum choose --header="Theme:") || show_tui_menu
  for i in "${!menu[@]}"; do
    if [[ "${menu[$i]}" == "$choice" ]]; then
      if [[ "$choice" == "Back" ]]; then
        show_tui_menu
      else
        eval "${commands[$i]}"
      fi
      break
    fi
  done
}

tools_menu() {
  local menu=("Setup Fingerprint" "Sync Applications" "Back")
  local commands=(
    "$BIN_DIR/omarchy-fingerprint-setup"
    "$BIN_DIR/omarchy-sync-applications"
    "show_tui_menu"
  )
  local choice
  choice=$(printf "%s\n" "${menu[@]}" | gum choose --header="Tools:") || show_tui_menu
  for i in "${!menu[@]}"; do
    if [[ "${menu[$i]}" == "$choice" ]]; then
      if [[ "$choice" == "Back" ]]; then
        show_tui_menu
      else
        eval "${commands[$i]}"
      fi
      break
    fi
  done
}

if [[ $# -eq 0 ]]; then
  show_tui_menu
  exit 0
fi

case "$1" in
  -v|--version|version)
    show_version
    ;;
  -h|--help|help)
    show_help
    ;;
  update)
    echo -e "${BLUE}🔄 Updating Omarchy...${NC}"
    "$BIN_DIR/omarchy-update"
    ;;
  refresh-waybar)
    echo -e "${BLUE}🔄 Refreshing waybar...${NC}"
    "$BIN_DIR/omarchy-refresh-waybar"
    ;;
  theme-install)
    "$BIN_DIR/omarchy-theme-install" "${@:2}"
    ;;
  theme-remove)
    "$BIN_DIR/omarchy-theme-remove" "${@:2}"
    ;;
  *)
    echo -e "${RED}Unknown command: $1${NC}"
    show_help
    exit 1
    ;;
esac 