#!/usr/bin/env bash

set -e

#########################
## gum style variables ##
#########################

GUM_SPIN_SPINNER="minidot"

sudo -v

####################
## Installing gum ##
####################

if ! command -v gum &>/dev/null; then
  echo "Installing gum"
  sudo pacman -S gum
fi

####################################
## Installing paru & dependencies ##
####################################

if ! command -v paru &>/dev/null; then
  sudo -v
  gum spin --title "Installing paru dependencies" -- \
    sudo pacman -S base-devel git --needed --noconfirm

  TEMP_DIR=$(mktemp -d)

  gum spin --title "Cloning paru-bin from AUR" -- \
    git clone https://aur.archlinux.org/paru-bin.git $TEMP_DIR/paru-bin
  cd $TEMP_DIR/paru-bin

  gum spin --title "Installing paru"
  sudo -v
  makepkg -si --noconfirm
fi

#############################
## Configuration Selection ##
#############################

mapfile -t desktop < <(
  gum choose --header="Choose desktop configuration:" \
    "Niri" \
    "KDE Plasma" \
    "None"
)

mapfile -t tools < <(
  gum choose --header="Choose tools to install:" --no-limit \
    "Terminal" \
    "Terminal Emulator" \
    "Gaming" \
    "VR Gaming"
)

mapfile -t extratools < <(
  gum choose --header="Choose extra tools to install:" --no-limit \
    "Godot" \
    "Btop" \
    "Fuzzel" \
    "Ncdu" \
    "Linja Pona [Font]" \
    "Vesktop" \
    "Nextcloud" \
    "Zen Browser" \
    "Tailscale"
)

######################
## Applying configs ##
######################

for choice in "${desktop[@]}"; do
  case "$choice" in
  "Niri")
    sudo -v
    gum spin --title "Installing niri alongside required packages" --show-error -- \
      paru -S --needed --noconfirm \
      niri \
      ly \
      nautilus \
      gnome-keyring \
      brightnessctl \
      cava \
      candy-icons-git \
      power-profiles-daemon \
      qt6ct-kde \
      xdg-desktop-portal-gnome

    stow niri qt6ct

    curl -fsSL https://install.danklinux.com | sh

    xdg-mime default org.gnome.nautilus.desktop inode/directory

    sudo systemctl enable ly@tty1
    ;;

  "KDE Plasma")
    sudo -v
    gum spin --title "Installing KDE Plasma" -- \
      pacman -S --needed --noconfirm plasma kde-applications
    sudo systemctl enable sddm
    ;;
  esac
done

for choice in "${tools[@]}"; do
  case "$choice" in
  "Terminal")
    sudo -v
    gum spin --title "Installing Terminal packages..." -- \
      paru -S --needed --noconfirm \
      fastfetch \
      hyfetch \
      neovim \
      zsh \
      eza \
      fzf \
      tealdeer \
      ttf-sourcecodepro-nerd \
      vim \
      wl-clipboard \
      zoxide \
      reflector
    stow fastfetch hyfetch nvim zsh
    gum spin --title "Updating Tealdeer database" -- \
      tldr --update
    ;;

  "Terminal Emulator")
    sudo -v
    gum spin --title "Installing Terminal Emulator..." -- \
      paru -S --needed --noconfirm \
      ghostty
    stow ghostty
    ;;

  "Gaming")
    sudo -v
    gum spin --title "Installing Gaming Packages..." -- \
      paru -S --needed --noconfirm \
      mangohud \
      gamemode \
      steam \
      prismlauncher \
      heroic-games-launcher-bin \
      arrpc
    stow mangohud

    sudo -v
    gum spin --spinner minidot --title "Applying arrpy systemd fix..." -- \
      sudo cp /usr/lib/systemd/user/arrpc.service /etc/systemd/system/arrpc.service &&
      sudo systemctl enable arrpc --now
    ;;

  "VR Gaming")
    sudo -v
    gum spin --title "Installing VR Gaming Packages..." -- \
      paru -S --needed --noconfirm \
      alvr-launcher-bin \
      wlx-overlay-s-bin
    ;;
  esac
done

extrapkgs=()

for choice in "${extratools[@]}"; do
  case "$choice" in
  "Godot")
    extrapkgs+=("godots-bin")
    stow godot
    ;;

  "Fuzzel")
    extrapkgs+=("fuzzel")
    stow fuzzel
    ;;

  "Btop") extrapkgs+=("btop") ;;
  "Ncdu") extrapkgs+=("ncdu") ;;
  "Linja Pona [Font]") extrapkgs+=("otf-linja-pona") ;;
  "Vesktop") extrapkgs+=("vesktop") ;;
  "Nextcloud") extrapkgs+=("nextcloud-client") ;;
  "Zen Browser") extrapkgs+=("zen-browser-bin") ;;
  "Tailscale") extrapkgs+=("tailscale") ;;
  esac
done

extpkgstr=$(
  IFS=" "
  echo "${extrapkgs[*]}"
)

sudo -v
gum spin --title "Installing extra packages" -- \
  paru -S --needed --noconfirm "$extpkgstr"
