# vim: set ft=zsh nowrap:
# shellcheck disable=SC1090

_here="$(dirname -- "$(readlink -f -- "${HOME}/.zshrc")")"
_cache_dir="${XDG_CACHE_HOME:-"${HOME}/.cache"}/zsh"

if [[ ! -d ${_cache_dir} ]]; then
    mkdir -p "${_cache_dir}"
fi

###########
# Options #
###########
# Ref: https://zsh.sourceforge.io/Doc/Release/Options.html

setopt CORRECT
setopt INTERACTIVE_COMMENTS
unsetopt BEEP
unsetopt NOMATCH
unsetopt FLOW_CONTROL

# History
setopt BANG_HIST
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_NO_STORE
setopt HIST_REDUCE_BLANKS
setopt HIST_VERIFY
setopt INC_APPEND_HISTORY_TIME

# Emacs mode
bindkey -e

############
# Keybinds #
############

# History search keybindings
bindkey '^P' history-beginning-search-backward
bindkey '^N' history-beginning-search-forward

# Load history search widgets
autoload -U history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end

# Fix for buggy behaviour of history search
# See https://github.com/zsh-users/zsh-autosuggestions/issues/619#issuecomment-904193190
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-beginning-search-backward-end history-beginning-search-forward-end)

if ! infocmp "$TERM" >/dev/null; then
    echo "TERM $TERM not supported on your system!" >&2
    echo "Some keybindings might not work properly." >&2
fi

if [[ -n $terminfo ]]; then
    # create a zkbd compatible hash;
    # to add other keys to this hash, see: man 5 terminfo
    typeset -g -A key

    key[Home]=${terminfo[khome]}
    key[End]=${terminfo[kend]}
    key[Insert]=${terminfo[kich1]}
    key[Delete]=${terminfo[kdch1]}
    key[Up]=${terminfo[kcuu1]}
    key[Down]=${terminfo[kcud1]}
    key[Left]=${terminfo[kcub1]}
    key[Right]=${terminfo[kcuf1]}
    key[PageUp]=${terminfo[kpp]}
    key[PageDown]=${terminfo[knp]}
    key[CtrlLeft]=${terminfo[kLFT5]}
    key[CtrlRight]=${terminfo[kRIT5]}

    # Function mapping
    [[ -n "${key[Home]}"        ]]  && bindkey  "${key[Home]}"       beginning-of-line
    [[ -n "${key[End]}"         ]]  && bindkey  "${key[End]}"        end-of-line
    [[ -n "${key[Insert]}"      ]]  && bindkey  "${key[Insert]}"     overwrite-mode
    [[ -n "${key[Delete]}"      ]]  && bindkey  "${key[Delete]}"     delete-char
    [[ -n "${key[Up]}"          ]]  && bindkey  "${key[Up]}"         history-beginning-search-backward-end
    [[ -n "${key[Down]}"        ]]  && bindkey  "${key[Down]}"       history-beginning-search-forward-end
    [[ -n "${key[Left]}"        ]]  && bindkey  "${key[Left]}"       backward-char
    [[ -n "${key[Right]}"       ]]  && bindkey  "${key[Right]}"      forward-char
    [[ -n "${key[PageUp]}"      ]]  && bindkey  "${key[PageUp]}"     beginning-of-history
    [[ -n "${key[PageDown]}"    ]]  && bindkey  "${key[PageDown]}"   end-of-history
    [[ -n "${key[CtrlLeft]}"    ]]  && bindkey  "${key[CtrlLeft]}"   backward-word
    [[ -n "${key[CtrlRight]}"   ]]  && bindkey  "${key[CtrlRight]}"  forward-word

    bindkey "^R" history-incremental-search-backward

    # Enable terminal application mode during zle editing.
    # This ensures special keys (arrows, Home, End, etc.) send escape sequences
    # that match the terminfo database, making our keybindings work correctly.
    if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
            autoload -Uz add-zle-hook-widget
            function zle_application_mode_start { echoti smkx; }
            function zle_application_mode_stop { echoti rmkx; }
            add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
            add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
    fi
fi

###################
# Shell Variables #
###################
# Ref: https://zsh.sourceforge.io/Doc/Release/Parameters.html

path=("${HOME}/.local/bin" "${HOME}/go/bin" "${HOME}/.cargo/bin" "${path[@]}")
export MAIL="/var/spool/mail/$USER"
export MAILCHECK=60
export HISTFILE="${HOME}/.zsh_history"
export HISTSIZE=100100
export SAVEHIST=100000
export LISTMAX=0

###################
# Other Variables #
###################

export VISUAL="nvim"
export EDITOR="nvim"
export DIFFPROG="nvim -d"
export NNN_TRASH=2
export NNN_PLUG=''
export MESA_WHICH_LLVM=1
export CC=gcc

#############
# LS_COLORS #
#############

_dircolors="${_here}/dircolors"
eval "$(dircolors -b "$_dircolors")"

unset _dircolors

##########
# Prompt #
##########

source "${_here}/prompt"

##############
# Completion #
##############

source "${_here}/completion"

###########
# Plugins #
###########

export ZSH_AUTOSUGGEST_STRATEGY=(history completion)
export ZSH_AUTOSUGGEST_MANUAL_REBIND=true

export ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
typeset -A ZSH_HIGHLIGHT_STYLES
export ZSH_HIGHLIGHT_STYLES[path]='none'

_antidote="${_cache_dir}/antidote"
_plugins="${_here}/plugins"
_plugins_cache="${_cache_dir}/plugins"

if [[ ! -d $_antidote ]]; then
    git clone --depth=1 https://github.com/mattmc3/antidote.git "$_antidote"
fi

fpath=("${_antidote}/functions" "${fpath[@]}")
autoload -Uz antidote

if [[ ! ${_plugins_cache} -nt ${_plugins} ]]; then
    antidote bundle <"${_plugins}" >"${_plugins_cache}"
fi

source "${_plugins_cache}"

unset _antidote _plugins _plugins_cache

#############
# Functions #
#############
# Ref: https://zsh.sourceforge.io/Doc/Release/Functions.html#Functions

function set_terminal_title() {
    local title cwd host extra
    local -a parts

    extra="$1"

    parts=()
    if [ -n "$SSH_CONNECTION" ] && [ -z "$TMUX" ]; then
        local -a tmp
        tmp=("${=SSH_CONNECTION}")
        host=${tmp[3]}
        parts+=("$host")
    fi

    if [ "$PWD" = "$HOME" ]; then
        cwd="~"
    else
        cwd="${PWD##*/}"
    fi
    parts+=("$cwd")

    if [ -n "$extra" ]; then
        parts+=("$extra")
    fi

    # shellcheck disable=SC2296
    title="${(j|:|)parts}"

    echo -ne "\033]2;$title\033\\"
}

function preexec() {
    local cmd
    local -a parts

    parts=("${=1}")
    if [ "${parts[1]}" = "sudo" ]; then
        cmd="${parts[2]}"
    else
        cmd="${parts[1]}"
    fi

    set_terminal_title "$cmd"
}

function precmd() {
    set_terminal_title
}

function telnet_with_title() {
    local host port
    local skip_next="false"

    for arg in "$@"; do
        case "$arg" in
        --bind=*) ;&
        --escape=*) ;&
        --user=*) ;&
        --trace=*) ;&
        -[beln]?*)
            # Option with argument immediately after, like -blocalhost
            skip_next="false"
            ;;
        --bind) ;&
        --escape) ;&
        --user) ;&
        --trace) ;&
        -[beln])
            # Option with argument on next iteration
            skip_next="true"
            ;;
        -*)
            # Any other option, assumed not to take an argument
            skip_next="false"
            ;;
        *)
            if [ "$skip_next" = "true" ]; then
                skip_next="false"
            elif [ -z "$host" ]; then
                host="$arg"
            else
                port="$arg"
            fi
            ;;
        esac
    done

    echo -ne "\033]2;${host}:${port}\033\\"
    telnet "$@"
    set_terminal_title
}

function ssh_with_title() {
    local host=""
    local skip_next="false"

    for arg in "$@"; do
        case "$arg" in
        -[bcDeFIiLlmOopRSWw]?*)
            # Option with argument immediately after, like -p22
            skip_next="false"
            ;;
        -[bcDeFIiLlmOopRSWw])
            # Option with argument on next iteration
            skip_next="true"
            ;;
        -*)
            # Any other option, assumed not to take an argument
            skip_next="false"
            ;;
        *)
            if [ "$skip_next" = "true" ]; then
                skip_next="false"
            else
                host="${arg#*@}"
            fi
            ;;
        esac
    done

    echo -ne "\033]2;${host}\033\\"
    ssh "$@"
    set_terminal_title
}

###########
# Aliases #
###########

alias cp='cp -i'
alias ln='ln -fi'
alias ls='ls --color=auto --group-directories-first -vh'
alias grep='grep --color=auto'
alias mv='mv -i'
alias n='nnn -dHerU'
alias rm='rm -I'
alias ssh='ssh_with_title'
alias telnet='telnet_with_title'

###########
# Cleanup #
###########

unset _here _cache_dir
