##### Platform independent #####

# Files
alias ...="cd ../../"
alias ....="cd ../../../"

alias ll='ls -alhFG'
alias la='ls -AG'
alias l='ls -CFG'

alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

# CD-ing to some commonly used folders
alias og="cd ~/Git"
alias dotfiles="cd ~/Git/dotfiles"

# Editing files
alias raliases="source ~/.config/.aliases && source ~/.config/.aliases.local"
alias aliases="code ~/.config/.aliases"
alias laliases="code ~/.config/.aliases.local"
alias zshrc="code $ZDOTDIR/.zshrc"
alias lzshrc="code $ZDOTDIR/.zshrc.local"
alias vimrc="vim ~/.vimrc"

# NPM
alias npr="npm run"

# IP addresses & networking
alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"

# Misc
alias c="clear"
alias be="bundle exec"
alias serve="python -m http.server 9000"
alias yda="youtube-dl -x --audio-format mp3"

# tmux
if type tmux >/dev/null 2>&1; then
  alias tm="tmux -2 attach-session -t "default" || tmux -2 new-session -s \"default\""
  alias tmv="tmux -2 attach-session -t "vscode" || tmux -2 new-session -s \"vscode\""
  alias tmks="tmux kill-session -t"
  alias tmas="tmux attach-session -t"
  alias tmls="tmux list-sessions"
fi

# Docker
if type docker >/dev/null 2>&1; then
  alias dc="docker compose"
  alias de="docker exec"
  alias dr="docker run"
  alias db="docker build"
  alias ds="docker stop"
  alias drm="docker rm"
  alias drmi="docker rmi"
  alias dps="docker ps"
  alias dl="docker logs"
fi

# Git Aliases
if type git >/dev/null 2>&1; then
  alias g="git"
  alias ga="git add"
  alias gr="git rm"
  alias gc="git commit"
  alias gs="git status -s"
  alias gss="git status"
  alias gd="git diff"
  alias gds="git diff --staged"
  alias gco="git checkout"
  alias gcom="git checkout master"
  alias gpom="git pull origin master"
  alias gbd="git branch -D"
  alias gm="git mergetool"
  alias gmc="git merge --continue"
  alias grc="git rebase --continue"
  alias gsf="git sf"
  alias gaf="git af"
  alias gmf="git mf"
fi

# Github CLI Aliases
if type gh >/dev/null 2>&1; then
  alias gp="gh pr"
  alias gps="gh pr status"

  # my prs
  alias prso="gh pr list --author @me --state open"

  function prsob() {
    gh pr list --author $1 --state open
  }

  function prscb() {
    gh pr list --author $1 --state closed
  }
fi

if type systemctl >/dev/null 2>&1; then
  alias ssc="sudo systemctl"
  alias sc="systemctl"
  alias scs="systemctl status"
  alias scst="systemctl start"
fi

# eza aliases, if available
if type eza >/dev/null 2>&1; then
  eza_params=(
    '--git' '--icons' '--group' '--group-directories-first'
    '--time-style=relative'
  )
  alias ls='eza $eza_params -1'
  alias l='eza --git-ignore $eza_params'
  alias ll='eza --all --header --long $eza_params'
  alias llm='eza --all --header --long --sort=modified $eza_params'
  alias la='eza -lbhHigUmuSa'
  alias lx='eza -lbhHigUmuSa@'
  alias lt='eza --tree $eza_params'
  alias tree='eza --tree $eza_params'

  alias lA='ll --sort=acc'
  alias lC='ll --sort=cr'
  alias lM='ll --sort=mod'
  alias lS='ll --sort=size'
  alias lX='ll --sort=ext'
fi

set_alias_with_backup cat bat
set_alias_with_backup dig doggo
set_alias_with_backup df duf
set_alias_with_backup ping gping
set_alias_with_backup du dust --no-percent-bars

##### Mac OS Specific #####
if [ "$(uname)" = "Darwin" ]; then
  alias fl="open -a ForkLift"
  alias flushdns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder"
  alias copyssh="pbcopy < $HOME/.ssh/id_ed25519.pub"
  alias scr="open -b com.apple.ScreenSaver.Engine"
fi
