#!/usr/bin/env bash

set -e

DOTSDIR=$(pwd)

sudo -v || exit 1

#####################
## Updating System ##
#####################

sudo pacman -Syyu --noconfirm

#############################
## Installing dependencies ##
#############################

sudo pacman -S --noconfirm --needed gum stow base-devel git

###################################
## Installing yay & dependencies ##
###################################

if ! command -v yay &>/dev/null; then
  TEMP_DIR=$(mktemp -d)

  git clone https://aur.archlinux.org/yay-bin.git "$TEMP_DIR"/yay
  cd "$TEMP_DIR"/yay

  sudo -v || exit 1
  makepkg -si --noconfirm
fi

cd "$DOTSDIR"

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

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

mapfile -t tools < <(
  gum choose --header="Choose tools to install:" --no-limit \
    "Terminal" \
    "Terminal Emulator" \
    "Gaming" \
    "VR Gaming" \
    "Hamr Sticker Picker" \
    "CUPS Printing Support" \
    "Audio" \
    "Bluetooth"
)

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

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

env_to_set=()

for choice in "${desktop[@]}"; do
  case "$choice" in
  "Niri")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      niri \
      thunar \
      tumbler \
      ffmpegthumbnailer \
      gnome-keyring \
      brightnessctl \
      cava \
      candy-icons-git \
      power-profiles-daemon \
      qt6ct-kde \
      xdg-desktop-portal-gnome \
      xdg-desktop-portal-gtk \
      xdg-desktop-portal-wlr \
      quickshell-git \
      cliphist \
      wl-clipboard \
      dgop \
      matugen-bin \
      qt6-multimedia \
      dms-shell-bin \
      xwayland-satellite \
      ly
    # greetd-dms-greeter-git

    stow niri qt6ct --adopt

    # xdg-mime default org.gnome.nautilus.desktop inode/directory
    # xdg-mime default org.kde.dolphin.desktop inode/directory
    xdg-mime default thunar.desktop inode/directory

    mkdir -p "$HOME"/.config/environment.d
    # echo "QT_QPA_PLATFORMTHEME=qt6ct" >>"$HOME"/.config/environment.d/90-qt.conf
    env_to_set+=("QT_QPA_PLATFORMTHEME=qt6ct")
    systemctl --user daemon-reload

    systemctl --user enable dms
    systemctl --user add-wants niri.service dms
    systemctl --user add-wants niri.service gnome-keyring-daemon.service

    # dms greeter enable
    # dms greeter sync
    sudo systemctl enable ly@tty1
    ;;
  "Hyprland")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      hyprland \
      uwsm \
      thunar \
      tumbler \
      ffmpegthumbnailer \
      gnome-keyring \
      brightnessctl \
      cava \
      candy-icons-git \
      power-profiles-daemon \
      qt6ct-kde \
      xdg-desktop-portal-gnome \
      xdg-desktop-portal-gtk \
      xdg-desktop-portal-wlr \
      quickshell-git \
      cliphist \
      wl-clipboard \
      dgop \
      matugen-bin \
      qt6-multimedia \
      dms-shell-bin \
      xwayland-satellite \
      ly

    stow hyprland qt6ct --adopt

    # xdg-mime default org.gnome.nautilus.desktop inode/directory
    # xdg-mime default org.kde.dolphin.desktop inode/directory
    xdg-mime default thunar.desktop inode/directory

    mkdir -p "$HOME"/.config/environment.d
    # echo "QT_QPA_PLATFORMTHEME=qt6ct" >>"$HOME"/.config/environment.d/90-qt.conf
    env_to_set+=("QT_QPA_PLATFORMTHEME=qt6ct")
    systemctl --user daemon-reload

    systemctl --user enable dms
    systemctl --user add-wants hyprland-session.target dms
    systemctl --user add-wants hyprland-session.target gnome-keyring-daemon.service

    # dms greeter enable
    # dms greeter sync
    sudo systemctl enable ly@tty1

    ;;
  "KDE Plasma")
    sudo -v || exit 1
    pacman -S --needed --noconfirm plasma kde-applications
    sudo systemctl enable sddm
    ;;
  esac
done

for choice in "${tools[@]}"; do
  case "$choice" in
  "Terminal")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      fastfetch \
      hyfetch \
      neovim \
      zsh \
      eza \
      fzf \
      tealdeer \
      ttf-sourcecodepro-nerd \
      vim \
      wl-clipboard \
      zoxide \
      reflector \
      inetutils \
      imagemagick \
      tree

    stow fastfetch hyfetch nvim zsh --adopt

    tldr --update

    chsh -s /bin/zsh "$USER"
    ;;

  "Terminal Emulator")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      ghostty
    stow ghostty --adopt
    ;;

  "Gaming")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      mangohud \
      gamemode \
      steam \
      prismlauncher \
      heroic-games-launcher-bin \
      arrpc \
      umu-launcher \
      wine \
      protontricks \
      protonup-qt \
      protonplus \
      goverlay \
      vkbasalt \
      vulkan-tools

    stow mangohud --adopt

    sudo -v || exit 1

    sudo cp /usr/lib/systemd/user/arrpc.service /etc/systemd/system/arrpc.service
    sudo systemctl enable arrpc --now
    ;;

  "VR Gaming")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      alvr-launcher-bin \
      wayvr
    ;;

  "Hamr Sticker Picker")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      hamr

    stow hamr-sticker-picker --adopt

    systemctl --user enable hamr.service
    systemctl --user add-wants niri.service hamr.service
    systemctl --user start hamr.service
    ;;
  "CUPS Printing Support")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      cups

    sudo systemctl enable cups
    ;;
  "Audio")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      pipewire \
      pipewire-pulse \
      wireplumber

    systemctl --user enable --now pipewire pipewire-pulse wireplumber
    ;;
  "Bluetooth")
    sudo -v || exit 1
    yay -S --needed --noconfirm \
      bluez

    sudo systemctl enable bluetooth --now
    ;;
  esac
done

extrapkgs=()

for choice in "${extratools[@]}"; do
  case "$choice" in
  "General Fonts")
    extrapkgs+=("noto-fonts" "noto-fonts-cjk" "noto-fonts-emoji")
    ;;
  "Godot")
    extrapkgs+=("godots-bin")
    stow godot --adopt
    ;;

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

  "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") ;;
  "Global git config")
    git config --global init.defaultBranch main

    if [ "$USER" = "vivian" ]; then
      echo "-=[ WARNING ]=-"
      echo "Because your username equals \"vivian\", your git user.name and user.email will be set to mine, please revert this change if you are not me."
      echo "Your old values will be written to ~/.dots-gitbackup"

      {
        echo "user.name"
        git config --global user.name
        echo ""
        echo "user.email"
        git config --global user.email
      } >"$HOME"/.dots-gitbackup

      git remote remove origin
      git remote add origin ssh://git@gitea.sparkle.gay:9173/vivian/.dotfiles.git
      git config --global user.name "Vivian Elisabeth H"
      git config --global user.email "36878099+SparkleDEV@users.noreply.github.com"
    fi
    ;;
  "MPV")
    extrapkgs+=("mpv" "yt-dlp")
    stow mpv --adopt
    ;;
  esac
done

mkdir -p "$HOME"/.config/environment.d
echo "$(
  IFS=$'\n'
  printf "%s\n" "${env_to_set[@]}" | sort -u
)" >"$HOME"/.config/environment.d/90-dots.conf

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

sudo -v || exit 1
yay -S --needed --noconfirm $extpkgstr

git add .
git reset --hard HEAD
git submodule update --recursive --init

echo "-=[ DOTFILES INSTALLED ]=-"
echo "You probably want to reboot"
