# Pre-requisite: zi (https://github.com/z-shell/zi)
# sh -c "$(curl -fsSL get.zshell.dev)" -- -i skip -b main
typeset -A ZI
ZI[BIN_DIR]="${HOME}/.zi/bin"
source "${ZI[BIN_DIR]}/zi.zsh"
autoload -Uz _zi
(( ${+_comps} )) && _comps[zi]=_zi
# end of zi init

# init completion
autoload -Uz compinit && compinit

# Vi mode
export KEYTIMEOUT=1
bindkey -v

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:$HOME/.local/bin:$PATH

if [[ -f "$HOME/.zshrc.local" ]]; then
  source "$HOME/.zshrc.local"
fi

zi snippet OMZL::history.zsh
zi snippet OMZL::directories.zsh

# apply workaround for refresh: https://github.com/spaceship-prompt/spaceship-prompt/issues/1193
zi light spaceship-prompt/spaceship-prompt

zi light ptavares/zsh-sdkman
zi light zsh-users/zsh-syntax-highlighting

zi is-snippet wait lucid for \
  OMZP::fzf \
    has'nodenv' \
  OMZP::nodenv

export FAVORITE_COMMAND1="{{ .tmux_favorite1 }}"
export FAVORITE_COMMAND2="{{ .tmux_favorite2 }}"
export FAVORITE_COMMAND3="{{ .tmux_favorite3 }}"
export FAVORITE_COMMAND4="{{ .tmux_favorite4 }}"

alias cdd='cd {{ .deploydir }}'
export cdd={{ .deploydir }}

alias rm='rm --interactive --verbose'
alias mv='mv --interactive --verbose'
alias cp='cp --verbose --interactive'
alias dmesg='dmesg -T'
alias grep='grep --color'

# ---------------------------------------------------------------------------
#  History
# ---------------------------------------------------------------------------

setopt APPEND_HISTORY       # Append commands to the history file when a session ends
unsetopt INC_APPEND_HISTORY # Avoid appending commands immediately after execution
unsetopt SHARE_HISTORY      # Keep history isolated to each session during runtime

# ---------------------------------------------------------------------------
#  Git
# ---------------------------------------------------------------------------

if command -v tig &>/dev/null; then
  alias tiga='tig --all'
fi

# Function to run after every 'cd' command
chpwd() {
  # Check if the current directory is a Git repository
  if git rev-parse --is-inside-work-tree 2>/dev/null; then
    echo "Fetching latest changes from remote..."
    git fetch
  fi
}

# ---------------------------------------------------------------------------
#  Editor
# ---------------------------------------------------------------------------

if hash nvim 2>/dev/null; then
  export EDITOR=nvim
elif hash vim 2>/dev/null; then
  export EDITOR=vim
elif hash vi 2>/dev/null; then 
  export EDITOR=vi
else
  export EDITOR=nano
fi

export SVN_EDITOR=$EDITOR
export GIT_EDITOR=$EDITOR
export VISUAL=$EDITOR
alias vi=$EDITOR

# ---------------------------------------------------------------------------
#  Tmux
# ---------------------------------------------------------------------------

if command -v tmux &>/dev/null; then
  alias ta='tmux attach'
  
  alias tkill="for s in \$(tmux list-sessions | awk -F ':' '{print \$1}' | fzf); do
    tmux kill-session -t \$s;
  done"
fi


# Function to set tmux window title
function set_tmux_title {
    if [[ -n "$TMUX" ]]; then
        tmux rename-window "$1"
    fi
}

# SSH wrapper to change the tmux title locally
function ssh() {
    if [[ -n "$TMUX" ]]; then
        # Save the original tmux window title
        original_title=$(tmux display-message -p '#W')
    fi

    # Change tmux title before connecting
    set_tmux_title "SSH $1"

    # Run the actual ssh command with all arguments
    command ssh "$@"

    # Restore the original tmux window title after disconnecting
    if [[ -n "$TMUX" ]]; then
        set_tmux_title "$original_title"
        tmux set-window-option automatic-rename on
    fi
}

export ZSH_TMUX_AUTOSTART=false
export ZSH_TMUX_AUTOSTART_ONCE=false
export ZSH_TMUX_AUTOCONNECT=true
export ZSH_TMUX_CONFIG=$HOME/.tmux.conf
export ZSH_TMUX_DEFAULT_SESSION_NAME=build

zi snippet OMZP::tmux

# ---------------------------------------------------------------------------
#  Fzf
# ---------------------------------------------------------------------------

if command -v fzf &>/dev/null; then

  change_path_and_find_project() {        
      cd {{ .projectdir }}      
      zle fzf-cd-widget
      zle reset-prompt
  }

  zle -N change_path_and_find_project
  bindkey '^P' change_path_and_find_project
fi


# ---------------------------------------------------------------------------
#  Sdkman
# ---------------------------------------------------------------------------

export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"


# ---------------------------------------------------------------------------
#  Maven
# ---------------------------------------------------------------------------

if command -v mvn &>/dev/null; then
    if [ -f "{{ .java_truststore_file }}" ]; then
        alias m='mvn -Djavax.net.ssl.trustStore={{ .java_truststore_file }} -Djavax.net.ssl.trustStorePassword={{ .java_truststore_password }}'
    else
        alias m='mvn'
    fi
    alias mvnp='m -Dmaven.test.skip -Dmaven.javadoc.skip=true -DskipMunitTests clean package'
    alias mvnd='m -Dmaven.test.skip -Dmaven.javadoc.skip=true deploy -P profile-nexus'
    alias mvni='m -Dmaven.test.skip -Dmaven.javadoc.skip=true clean install'
    
    function mbump() {
        if [ $# -eq 0 ]; then
            echo "Usage: mbump <version> (means -SNAPSHOT) or mbump <version> <kind> (kind is R or S)"
        else
            if [ $# -gt 1 ] && [ "$2" = "R" ]; then
                kind="RELEASE"
            else
                kind="SNAPSHOT"
            fi
            mvn versions:set -DgenerateBackupPoms=false -DnewVersion="$1-$kind"
        fi
    }

    # copy maven artifact
    alias d="python3 ~/.local/bin/deploy.py '$cdd'"
fi

# ---------------------------------------------------------------------------
#  Nix
# ---------------------------------------------------------------------------

if [ -e $HOME/.nix-profile/etc/profile.d/nix.sh ]; then 
  . $HOME/.nix-profile/etc/profile.d/nix.sh;

  alias nix-update='nix-channel --update && nix-env -u'

  nix-search() {
    nix-env -qa --description ".*$1.*"
  }
fi