#!/bin/bash

OMARCHY_VERSION=$(git -C ~/.local/share/omarchy describe --tags --abbrev=0 2>/dev/null)
PATH="$PATH:$HOME/.local/share/omarchy/bin"

show_ascii_art() {
  clear
  tte -i ~/.local/share/omarchy/logo.txt --frame-rate 640 --no-color expand
  echo "                                                                          $OMARCHY_VERSION"
}

main_menu() {
  show_ascii_art

  local options=("Theme" "Font" "Setup" "Update" "Manual" "Exit")
  choice=$(printf "%s\n" "${options[@]}" | gum choose --header "") || exit 0
  case "$choice" in
  Theme) theme_menu ;;
  Font)
    omarchy-font-menu
    ack_command
    main_menu
    ;;
  Update) update_menu ;;
  Setup) setup_menu ;;
  Manual) open_manual ;;
  Exit) clear && exit 0 ;;
  esac
}

update_menu() {
  show_ascii_art
  local menu=("Omarchy" "Waybar" "Walker" "Plymouth" "SwayOSD" "Desktop apps" "Back")
  local commands=(
    "omarchy-update"
    "omarchy-refresh-waybar"
    "omarchy-refresh-walker"
    "omarchy-refresh-plymouth"
    "omarchy-refresh-swayosd"
    "omarchy-refresh-applications"
    "main_menu"
  )
  local choice
  choice=$(printf "%s\n" "${menu[@]}" | gum choose --header="Update") || main_menu
  for i in "${!menu[@]}"; do
    if [[ "${menu[$i]}" == "$choice" ]]; then
      if [[ "$choice" == "Back" ]]; then
        main_menu
      else
        eval "${commands[$i]}"
        ack_command
        main_menu
      fi
      break
    fi
  done
}

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

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

remove_theme_prompt() {
  local theme
  theme=$(gum input --placeholder="Theme name" --header="")
  if [[ -n "$theme" ]]; then
    omarchy-theme-remove "$theme"
  fi
  theme_menu
}

setup_menu() {
  show_ascii_art
  local menu=("Dropbox" "Steam" "Docker DBs" "Fingerprint sensor" "Fido2 device" "Back")
  local commands=(
    "omarchy-setup-dropbox"
    "omarchy-setup-steam"
    "setup_docker_dbs"
    "omarchy-setup-fingerprint"
    "omarchy-setup-fido2"
    "main_menu"
  )
  local choice
  choice=$(printf "%s\n" "${menu[@]}" | gum choose --header="Setup") || main_menu
  for i in "${!menu[@]}"; do
    if [[ "${menu[$i]}" == "$choice" ]]; then
      if [[ "$choice" == "Back" ]]; then
        main_menu
      else
        eval "${commands[$i]}"
        ack_command
        main_menu
      fi
      break
    fi
  done
}

setup_docker_dbs() {
  options=("MariaDB" "MySQL" "Redis" "PostgreSQL")
  choices=$(printf "%s\n" "${options[@]}" | gum choose --no-limit --header "Select databases (space to select, return to install, esc to cancel)") || main_menu

  if [[ -n "$choices" ]]; then
    for db in $choices; do
      case $db in
      MySQL) sudo docker run -d --restart unless-stopped -p "127.0.0.1:3306:3306" --name=mysql8 -e MYSQL_ROOT_PASSWORD= -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:8.4 ;;
      PostgreSQL) sudo docker run -d --restart unless-stopped -p "127.0.0.1:5432:5432" --name=postgres16 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:16 ;;
      MariaDB) sudo docker run -d --restart unless-stopped -p "127.0.0.1:3306:3306" --name=mariadb11 -e MARIADB_ROOT_PASSWORD= -e MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=true mariadb:11.8 ;;
      Redis) sudo docker run -d --restart unless-stopped -p "127.0.0.1:6379:6379" --name=redis redis:7 ;;
      esac
    done
  fi

  main_menu
}

open_manual() {
  setsid chromium --new-window --ozone-platform=wayland --app="https://manuals.omamix.org/2/the-omarchy-manual" >/dev/null 2>&1 &
  clear
}

ack_command() {
  gum spin --spinner "globe" --title "Done!" -- sleep 1
}

main_menu
