# Quick configs for apps
alias zshconf="nvim $ZDOTDIR/.zshrc"
alias tmuxconf="nvim $XDG_CONFIG_HOME/tmux/tmux.conf"
alias hyprconf="cd $XDG_CONFIG_HOME/hypr/"
alias nvimconf="cd $XDG_CONFIG_HOME/$NVIM_APPNAME/"

# Quick cd ..
alias ..="cd .."
alias ...="cd ../../"
alias ....="cd ../../../"
alias .....="cd ../../../../"
alias .4="cd ../../../../"
alias .5="cd ../../../../.."

# Always copy contents of directories (r)ecursively and explain (v) what was done
# alias cp="cp -irv"

# Interactive delete (and trash)
alias dl="trash-put -v"
alias del="rm -Iv"

# mkdir with parents and nocorrect
alias mkdir="nocorrect mkdir -pv"

# ls aliases
alias l="ls -lAhG"
alias lt="ls -AhG --sort=time"
alias ltr="lt --reverse"
alias llt="lt -l"
alias lltr="ltr -l"
alias l.="ls -dlAFG .*"
alias lv="ls -AhG --sort=version"
alias lvr="lv --reverse"
alias llv="lv -l"
alias llvr="llv --reverse"

# Check which package owns a file
function whohas() {
  if [ -z "$1" ]; then
    echo "Usage: $0 <file>"
    return 1
  fi

  if command -v "$1" >/dev/null; then
    pacman -Qo "$(whereis -b $1 | awk '{print $2}')" 
  elif test -e $1; then
    pacman -Qo "$1"
  else
    echo "File '$1' not found."
  fi
}

# Easy lazygit
alias lg="lazygit"
alias lz="lazygit"

# Grep auto colorize
alias grep="grep --color=auto"

# Always try to (c)ontinue getting a partially-downloaded file and use XDG_DATA_HOME for wget-hsts file
alias wget="wget -c --hsts-file=\"$XDG_DATA_HOME/wget-hsts\""

# thefuck utility configuration and aliases
# eval $(thefuck --alias)
# alias f="fuck"

# Alternate neovim profiles
alias notesvim="NVIM_APPNAME=nvim-notesvim nvim"
alias truenvim="NVIM_APPNAME=nvim nvim"

# Sudo with alias and nocorrect fix
function sudo()
{
  if [ "$1" = "nocorrect" ]; then
    shift
    nocorrect command sudo "$@"
  else
    command sudo "$@"
  fi
}

alias sudo="sudo "
alias esudo="sudo -E "

# Memes
alias pls="run0 "

# run0 with alias and nocorrect fix
function run0()
{
  if [ "$1" = "nocorrect" ]; then
    shift
    nocorrect command run0 "$@"
  else
    command run0 "$@"
  fi
}

# run0 or sudo
if [ -n "$WAYLAND_DISPLAY" ]; then
  alias ele="run0 "
  alias eledit="sudoedit "
else
  alias ele="sudo "
  alias eledit="sudoedit "
fi


# Rsync
rsyncflags="--recursive \
  --links \
  --times \
  --atimes \
  --devices \
  --specials \
  --info=PROGRESS2 \
  --human-readable \
  --partial \
  --mkpath \
  "

alias copy="rsync $rsyncflags --perms --executability"
alias extcopy="rsync $rsyncflags --info=NAME,STATS2 --verbose --inplace"
alias mov="copy --remove-source-files"

# Systemd inhibit suspend
function si() {
  systemd-inhibit --what=sleep --why="$1 - zsh alias" "$@"
}

# Rclone
export RCLONE_PASSWORD_COMMAND="secret-tool lookup service rclone"

# gocryptfs private folder
alias pmount="mkdir -p \"$HOME/mnt/private\" && gocryptfs -extpass \"secret-tool lookup service gocryptfs-private\" \"$HOME/Documents/Private\" \"$HOME/mnt/private\""
alias pumount="umount \"$HOME/mnt/private\""

# Quick Mount
function qmount() {
  device="$(basename "$1")"

  if test -d "/mnt/$device"; then
    sudo rmdir "/mnt/$device" || return 1
  fi

  if test -d "$HOME/mnt/$device"; then
    rmdir "$HOME/mnt/$device" || return 1
  fi

  sudo mkdir "/mnt/$device" || return 1
  mkdir "$HOME/mnt/$device" || return 1

  sudo mount "$1" /mnt/"$device" || return 1
  mkdir "$HOME/mnt/$device" || return 1

  sudo bindfs -u "$(whoami)" -g "$(whoami)" /mnt/"$device" "$HOME/mnt/$device" || return 1

}

function qumount() {
  device="$(basename "$1")"

  sudo umount "$HOME/mnt/$device" || return 1
  rmdir "$HOME/mnt/$device"

  sudo umount "/mnt/$device" || return 1
  sudo rmdir "/mnt/$device"
}

# Colorized ip info
alias ip="ip -c"
alias ipa="ip a"

# Create python venv
alias mkvenv="python -m venv .venv && source ./.venv/bin/activate"

# Alias to start libvirt service
alias virtmgr="systemctl start libvirtd && uwsm app -- virt-manager & disown"

# Flutter using fvm
f() { fvm flutter "$@"; }
compdef f=flutter 2>/dev/null

# Fun shutdown aliases
alias rebot="systemctl reboot"
alias byebye="systemctl poweroff"
alias goodbye="systemctl poweroff -i"
alias zzz="systemctl suspend"
alias resurrect="systemctl reboot"
alias jesus="systemctl reboot -i"
alias respawn="systemctl soft-reboot"

# systemctl aliases
alias scs="systemctl status -l"
alias scst="systemctl start"
alias scstp="systemctl stop"
alias scr="systemctl restart"
alias scen="systemctl enable"
alias scdis="systemctl disable"
alias scf="systemctl --failed"

# journalctl aliases
alias jf="journalctl -f"
alias jb="journalctl -b"
alias ju="journalctl -u"
alias jfb="journalctl -f -b"
alias jbf="jfb"
alias jfu="journalctl -f -u"
alias jbu="journalctl -b -u"
alias jbfu="journalctl -b -f -u"
alias jfbu="jbfu"

# systemctl --user aliases
alias scus="systemctl --user status"
alias scusl="scus -l "
alias scust="systemctl --user start"
alias scustp="systemctl --user stop"
alias scur="systemctl --user restart"
alias scuen="systemctl --user enable"
alias scudis="systemctl --user disable"
alias scuf="systemctl --user --failed"

# journalctl --user aliases
alias juf="journalctl --user -f"
alias jub="journalctl --user -b"
alias juu="journalctl --user -u"
alias jufb="journalctl --user -f -b"
alias jubf="jufb"
alias jufu="journalctl --user -f -u"
alias jubu="journalctl --user -b -u"
alias jubfu="journalctl --user -b -f -u"
alias jufbu="jubfu"

alias ino="arduino-cli"

function mpvp() {
  local link=""

  if [ -n "$1" ] && [ -f "$1" ]; then
    link="$1"
    shift
  elif wl-paste | grep -qE "^https?://"; then
    link="$(wl-paste)"
  else
    echo "Refusing to play. No link or file provided." >&2
    return 1
  fi

  mpv "$link" "$@" || mpv --ytdl-raw-options-add="cookies-from-browser=firefox" "$link" "$@"
}

function ytdl() {
  local link=""

  if [ -n "$1" ] && [ -f "$1" ]; then
    link="$1"
    shift
  elif wl-paste | grep -qE "^https?://"; then
    link="$(wl-paste)"
  else
    echo "Refusing to download. No link or file provided." >&2
    return 1
  fi

  yt-dlp "$link" "$@" || yt-dlp --cookies-from-browser=firefox "$link" "$@"
}

# ani-cli alias to download first 100 (almost all) episodes of an anime
# Anime name is the current directory name
alias anidl="ani-cli -d -e 1-100 \"\$(basename \"\$(realpath .)\")\""

# xdg-ninja
alias xdg-maid="xdg-ninja --skip-ok --skip-unsupported"

function aur-vote () {
  AUR_AUTO_VOTE_PASSWORD="$(secret-tool lookup service aur)" command aur-auto-vote --no-unvote "$(secret-tool search service aur 2>&1 | grep "attribute.UserName" | cut -d "=" -f 2 | xargs)"
}

# Yazi change cwd on exit
function y() {
  local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
  yazi "$@" --cwd-file="$tmp"
  if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
    builtin cd -- "$cwd"
  fi
  rm -f -- "$tmp"
}

function tmux() {
  local slice="tmux.slice"
  if uwsm check is-active >/dev/null 2>&1; then
    uwsm app -s "$slice" -- tmux "$@"
  else
    command tmux "$@"
  fi
}

# Hotspot control for android device via adb over ssh
function hotspot() {
  local DEFAULT_HOTSPOT_DEVICE="rootdevmobile"
  local device action

  # parse args
  if [[ $# -eq 0 ]]; then
    device="$DEFAULT_HOTSPOT_DEVICE"
    action="on"
  elif [[ $# -eq 1 ]]; then
    if [[ "$1" == "off" ]]; then
      device="$DEFAULT_HOTSPOT_DEVICE"
      action="off"
    elif [[ "$1" == "on" ]]; then
      device="$DEFAULT_HOTSPOT_DEVICE"
      action="on"
    else
      device="$1"
      action="on"
    fi
  elif [[ $# -eq 2 ]]; then
    device="$1"
    action="$2"
  else
    echo "usage: hotspot [device] [on|off]"
    return 1
  fi

  # validate
  if [[ "$action" != "on" && "$action" != "off" ]]; then
    echo "invalid action: $action"
    return 1
  fi

  # run command
  if [[ "$action" == "on" ]]; then
    ssh "$device" \
      "nohup sh -c 'am broadcast -a dev.shadoe.delta.action.START_SOFT_AP -n dev.shadoe.delta/.SoftApBroadcastReceiver &&
      svc wifi disable'"
  else
    ssh "$device" "am broadcast -a dev.shadoe.delta.action.STOP_SOFT_AP -n dev.shadoe.delta/.SoftApBroadcastReceiver"
  fi
}

function qadb() {
  local DEFAULT_ROOT_DEVICE="rootdevmobile"
  local DEFAULT_ADB_DEVICE="devmobile"

  ssh "$DEFAULT_ROOT_DEVICE" "nohup sh -c 'setprop service.adb.tcp.port 5555 &&
    stop adbd &&
    start adbd'"

  adb connect "$DEFAULT_ADB_DEVICE"
}

for f in "$XDG_CONFIG_HOME"/zsh/zshalias.d/**/*.zsh(N.); do
  source "$f"
done
