#!/usr/bin/zsh

# Create associative array
local -A QEFILES
# Add key+value pairs to array
QEFILES=( 
	[sway_config]="nvim $HOME/.config/sway/config"
	[sway_keys]="nvim $HOME/.config/sway/keys"
	[zsh_config]="nvim $HOME/.config/zsh/.zshrc && source $HOME/.config/zsh/.zshrc"
	[zsh_aliases]="nvim $HOME/.config/zsh/aliasrc && source $HOME/.config/zsh/aliasrc"
	[zsh_functions]="nvim $HOME/.config/zsh/functions && source $HOME/.config/zsh/functions"
	[waybar_config]="nvim $HOME/.config/waybar/config"
	[waybar_style]="nvim $HOME/.config/waybar/style.css"
	[waybar_module]="nvim $HOME/.config/waybar/modules.json"
	[nvim_config]="nvim $HOME/.config/nvim/init.vim"
	[kitty_config]="nvim $HOME/.config/kitty/kitty.conf"
	[qutebrowser_config]="nvim $HOME/.config/qutebrowser/config.py"
	[qutebrowser_redirect]="nvim $HOME/.config/qutebrowser/pyconfig/new_redirectors.py"
	[qemu_hooks]="sudo -E nvim /etc/libvirt/hooks/qemu"
	[mako_config]="nvim $HOME/.config/mako/config"
	[win10-pt]="virsh edit win10-pt"
)
# Open files in nvim
qe() { 
	eval "$QEFILES[$1]"
}
# Set completion with associative array keys
function _qe { compadd ${(@k)QEFILES} }
# Assign completion to function
compdef _qe qe

local -A JDIR
JDIR=( 
	[bin]="$HOME/.local/bin"
	[fuzzel]="$HOME/.local/bin/fuzzel"
	[bar]="$HOME/.local/bin/bar"
	[python]="$HOME/Development/Python"
	[pio]="$HOME/Development/Platformio"
)
jump() { 
	cd "$JDIR[$1]"
}
function _jump { compadd ${(@k)JDIR} }
compdef _jump jump

# Extract archives with ex
ex () {
  if [ -f "$1" ] ; then
    case $1 in
      *.tar.bz2)   tar xjf "$1"   ;;
      *.tar.gz)    tar xzf "$1"   ;;
      *.bz2)       bunzip2 "$1"   ;;
      *.rar)       unrar x "$1"   ;;
      *.gz)        gunzip "$1"    ;;
      *.tar)       tar xf "$1"    ;;
      *.tbz2)      tar xjf "$1"   ;;
      *.tgz)       tar xzf "$1"   ;;
      *.zip)       unzip "$1" -d "${1%.*}";;
      *.Z)         uncompress "$1";;
      *.7z)        7z x "$1"      ;;
      *.deb)       ar x "$1"      ;;
      *.tar.xz)    tar xf "$1"    ;;
      *.tar.zst)   unzstd "$1"    ;;
      *.img.xz )   xz --decompress "$1" ;;
      *)           echo "$1 cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

pw-restart() {
	systemctl --user restart pipewire pipewire-pulse
	systemctl --user restart xdg-desktop-portal xdg-desktop-portal-gtk xdg-desktop-portal-wlr
}

# Move a file to a new location and put a symlink where it used to be
mv-ln () {
    mv "$1" "$2"
    ln -s "$2" "$1"
}

rm-ln(){
    sed -i "/\b\($1\)\b/d" "$2"
}

history-run(){
    $(tac ~/.cache/zsh_history | fzf --no-sort)
}

zip-dir() {
    zip -rj ${1}.zip $1/*
}

zip-all(){
    for d in ./*; do
        if [ -d "$d" ]; then
            7z a -tzip ${d}.zip -w ${d}/.
        fi
    done
}

# Use fzf to select usb device and show info
usb-info () {
    if ! which fzf >/dev/null 2>&1; then
        echo "Please install fzf"
        exit
    fi
    lsusb | fzf | awk '{print $6}' | xargs -I {} lsusb -v -d "{}"
}

hide-desktop() {
	# Create local applications directory if it doesn't exist
	[ ! -d "$HOME/.local/share/applications" ] && mkdir -p ~/.local/share/applications/
	# Copy the desktop file to the local directory
	cp /usr/share/applications/$1 ~/.local/share/applications/
	# Append nodisplay to the file to hide it from launchers
	echo "NoDisplay=true" >> ~/.local/share/applications/$1	
}

http-to-ssh () {
    REMOTE=$(git remote -v | grep fetch | awk '{print $2}')
    USER=$(echo "$REMOTE" | awk -F/ '{print $4}')
    REPO=$(echo "$REMOTE" | awk -F/ '{print $5}')

    echo "Current remote URL is: $REMOTE"

    # Switch to ssh, otherwise do noting
    case $REMOTE in
        *"https"*)
            # Echo relevant info to user
            echo "USER=$USER"
            echo "REPO=$REPO"
            echo "New URL is git@github.com:$USER/$REPO"
            git remote set-url origin "git@github.com:$USER/$REPO"
            echo "Changed to SSH."
            ;;
        *)
            echo "No change made, already using SSH."
            ;;
    esac
}

# This function will:
# 1) auto-cd if given a directory instead of a file
# 2) Automatically request root permissions if the user isn't able to modify the file
# It's a small QOL improvement since I always use vim on a directory or root file.
vim () {
    # Cd to directory if it doesn't exist
    if [ -d "$1" ]; then
        cd "$1" || exit
    # If it's a file
    else
        # open normally if user has privileges
        if groups | grep -q "$(stat -c "%G" "$1" 2>/dev/null)"; then
            nvim "$1"
        # otherwise open with root
        else
            sudo -E nvim "$1"
        fi
    fi
}

get-ip() {
    ping -q -c 1 -t 1 "$1" | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"
}

rm-kh() {
    sed -i "/\b\($1\)\b/d" ~/.ssh/known_hosts
}

lsiommu() {
	shopt -s nullglob
	for d in /sys/kernel/iommu_groups/*/devices/*; do
		n=${d#*/iommu_groups/*}; n=${n%%/*}
		printf 'IOMMU Group %s ' "$n"
		lspci -nns "${d##*/}"
	done;
}
