#!/bin/sh
#
# source: https://github.com/Speyll/dotfiles/blob/main/.local/bin/fuzz-launcher

# shellcheck disable=SC2250

# Global Settings
FONT="monospace:size=14"
ALIGN="center"
FUZZEL_CMD="fuzzel -d -f $FONT -a $ALIGN"

# Function to display a menu and return the selected choice
show_menu() {
  echo "$1" | $FUZZEL_CMD -w "$2" -l "$3"
}

# Function to launch an application if available
launch_app() {
  if command -v "$1" >/dev/null 2>&1; then
    exec "$@"
  else
    echo "$1 not found" >&2
    exit 1
  fi
}

# Function to handle utilities menu
handle_utils_menu() {
  choice=$(show_menu "$utils_menu" 13 5)
  case "$choice" in
    "⚙️ Run") exec_fuzzel_run ;;
    "💻 Streaming") exec_fuzzel_streaming ;;
    "🔢 Calc") exec_fuzzel_calc ;;
    "📑 Todo") exec_fuzzel_todo ;;
    "⌨️ Keyboard") toggle_keyboard ;;
    *)
      echo "Invalid choice: $choice" >&2
      exit 1
      ;;
  esac
}

# Function to handle power menu
handle_power_menu() {
  choice=$(show_menu "$power_menu" 11 4)
  case "$choice" in
    " Shutdown") sudo poweroff ;;
    " Suspend") sudo halt ;;
    " Reboot") sudo reboot ;;
    " Lock") swaylock -c 000000 ;;
    *)
      echo "Invalid choice: $choice" >&2
      exit 1
      ;;
  esac
}

# Function to handle streaming menu
exec_fuzzel_streaming() {
  platform_choice=$(show_menu "$streaming_menu" 9 2)
  case "$platform_choice" in
    " Media") input_link=$(wl-paste) ;;
    " Twitch")
      channel_name=$(echo "" | fuzzel -d -p "type twitch name: " -f "$FONT" -w 40 -l 0 -a "$ALIGN")
      input_link="https://twitch.tv/$channel_name"
      ;;
    *)
      echo "Invalid choice: $platform_choice" >&2
      exit 1
      ;;
  esac
  media_choice=$(show_menu "$media_type_menu" 9 2)
  case "$media_choice" in
    " Video") mpv --pause=no "$input_link" ;;
    " Audio") foot -T "MPV Terminal" -e mpv --pause=no --no-video --volume=100 "$input_link" ;;
    *)
      echo "Invalid choice: $media_choice" >&2
      exit 1
      ;;
  esac
}

# Function to toggle virtual keyboard
toggle_keyboard() {
  if pgrep -x "wvkbd-mobintl" >/dev/null; then
    pkill wvkbd-mobintl
  else
    wvkbd-mobintl -H 50 -R 5 --bg "#282828" --fg "#ffffff" --fn "monospace 12" --alpha 200 &
  fi
}

# Function for calculator using fuzzel
exec_fuzzel_calc() {
  LAST=""
  while true; do
    SPACE=" "
    NEXT="$(fuzzel -l 0 --dmenu -p "${LAST}${SPACE}")"
    [ -z "$NEXT" ] && exit 1
    [ "$NEXT" = "y" ] && wl-copy "$LAST" && exit 0
    LAST="$(echo "$LAST$NEXT" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')"
  done
}

# Function for to-do manager using fuzzel
exec_fuzzel_todo() {
  cache_file="$HOME/.cache/todo.cache"
  cache_file_2="$HOME/.cache/todo2.cache"
  cache_contents="$(cat "$cache_file")"
  cache_lines="$(echo "$cache_contents" | wc -l)"
  [ -s "$cache_file" ] || cache_lines=0
  selection="$(echo "$cache_contents" | fuzzel --dmenu -l "$cache_lines" 2>/dev/null)"
  [ -z "$selection" ] && exit 1
  if grep -q "$selection" "$cache_file"; then
    grep -v "^${selection}$" "$cache_file" >"$cache_file_2"
    mv "$cache_file_2" "$cache_file"
  else
    echo "$selection" >>"$cache_file"
  fi
}

# Function for application runner
exec_fuzzel_run() {
  # shellcheck disable=SC2312
  exec_list=$(printf "%s\n" $(echo "$PATH" | tr ':' '\n' | xargs -I{} find {} -maxdepth 1 -executable -type f -printf "%f\n" 2>/dev/null) | sort -u)
  selected=$(echo "$exec_list" | fuzzel --dmenu)
  [ -n "$selected" ] && exec "$selected"
}

# Menu definitions
main_menu="🖥️ Terminal\n🗃️ Explorer\n🌍 Browser\n📦 Apps\n⚙️ Utils\n🔌 Power"
utils_menu="📑 Todo\n💻 Streaming\n🔢 Calc\n⚙️ Run\n⌨️ Keyboard"
power_menu=" Shutdown\n Suspend\n Reboot\n Lock"
streaming_menu=" Media\n Twitch"
media_type_menu=" Video\n Audio"

# Main Menu Logic
choice=$(show_menu "$main_menu" 12 6)
case "$choice" in
  "🖥️ Terminal") launch_app foot ;;
  "🗃️ Explorer") launch_app pcmanfm-qt ;;
  "🌍 Browser") launch_app flatpak run io.gitlab.librewolf-community ;;
  "📦 Apps") launch_app fuzzel ;;
  "⚙️ Utils") handle_utils_menu ;;
  "🔌 Power") handle_power_menu ;;
  *)
    echo "Invalid choice: $choice" >&2
    exit 1
    ;;
esac
