if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

bindkey -e
if [ -n "$EAT_SHELL_INTEGRATION_DIR" ]; then
    source "$EAT_SHELL_INTEGRATION_DIR/zsh";
fi

export ZSH_DATA_DIR="${XDG_CONFIG_HOME:-"$HOME/.config"}/zsh/data"
export ZSH_CONFIG_DIR="${XDG_CONFIG_HOME:-"$HOME/.config"}/zsh/cfg"

#Check if terminal is fullcolor
if zmodload zsh/terminfo && (( "$terminfo[colors]" >= "256" )); then 
    __MATERUS_ZSH_256COLORS="${__MATERUS_ZSH_256COLORS:-1}"; else
    __MATERUS_ZSH_256COLORS="${__MATERUS_ZSH_256COLORS:-0}"; 
fi

#region Plugins
# Clone plugins if not exists
[ -d $ZSH_DATA_DIR/plugins/powerlevel10k ] || git clone https://github.com/romkatv/powerlevel10k $ZSH_DATA_DIR/plugins/powerlevel10k
[ -d $ZSH_DATA_DIR/plugins/zsh-syntax-highlighting ] || git clone  https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_DATA_DIR/plugins/zsh-syntax-highlighting
[ -d $ZSH_DATA_DIR/plugins/zsh-history-substring-search ] || git clone  https://github.com/zsh-users/zsh-history-substring-search.git $ZSH_DATA_DIR/plugins/zsh-history-substring-search
[ -d $ZSH_DATA_DIR/plugins/zsh-autosuggestions ] || git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_DATA_DIR/plugins/zsh-autosuggestions

source ${ZSH_DATA_DIR}/plugins/powerlevel10k/powerlevel10k.zsh-theme
source ${ZSH_CONFIG_DIR}/extra/zinputrc
source ${ZSH_CONFIG_DIR}/extra/zshcompletion.zsh

source ${ZSH_DATA_DIR}/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source ${ZSH_DATA_DIR}/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
source ${ZSH_DATA_DIR}/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
#endregion

#region SUBSTRING SEARCH
history-substring-search-up-prefixed(){
    HISTORY_SUBSTRING_SEARCH_PREFIXED=1 history-substring-search-up
}
history-substring-search-down-prefixed(){
    HISTORY_SUBSTRING_SEARCH_PREFIXED=1 history-substring-search-down
}

zle -N history-substring-search-up-prefixed
zle -N history-substring-search-down-prefixed
#endregion

#region Init Variables
export SAVEHIST=100000
export HISTSIZE=$SAVEHIST
export ZSH_PRIVATE=${ZSH_PRIVATE:-0}

mkdir -p $ZSH_DATA_DIR
mkdir -p $ZSH_DATA_DIR/plugins

# Load config generated by nix
[[ -f "$ZSH_DATA_DIR/nix.sh" ]] && source "$ZSH_DATA_DIR/nix.sh" 

setopt HIST_SAVE_NO_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt EXTENDED_HISTORY

# Private history mode
if [[ $ZSH_PRIVATE == 1 ]] then 
    export HISTFILE="/dev/null"; 
    unsetopt SHARE_HISTORY;
else 
    export HISTFILE="$ZSH_DATA_DIR/zsh_history"; 
    setopt SHARE_HISTORY;
fi
#endregion


#region Bindings
bindkey -r "^["
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;5A" history-substring-search-up-prefixed
bindkey "^[[1;5B" history-substring-search-down-prefixed
bindkey "$key[Up]" history-substring-search-up
bindkey "$key[Down]" history-substring-search-down
bindkey '^[[3~' delete-char
bindkey '^[[3;2~' delete-word
#endregion

#region Completions
ZSH_COMPDUMP="${ZSH_COMPDUMP:-${XDG_CACHE_HOME:-${HOME}/.cache}/.zcompdump-${HOST}-${ZSH_VERSION}}"
autoload -U compinit && compinit -d $ZSH_COMPDUMP
#endregion

#region Functions
zsh-private() {
    ZSH_PRIVATE=1 zsh
}

myip() {
    wget -qO- https://wtfismyip.com/text
}

speedtest() {
    curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python3
}
#endregion

#region Aliases
if [[ "$TERM" = "xterm-kitty" ]]; then 
    alias ssh="kitty +kitten ssh"
fi
if [[ $(whence -p "nvim") ]]; then
    EDITOR="${EDITOR:-nvim}"
    alias vim="nvim"
    alias vi="nvim"
    alias vimdiff="nvim -d"
fi

#endregion

if [[ "$__MATERUS_ZSH_256COLORS" = "1" ]] ; then [[ ! -f ${ZSH_CONFIG_DIR}/p10kcfg/fullcolor.zsh ]] || source ${ZSH_CONFIG_DIR}/p10kcfg/fullcolor.zsh;
else [[ ! -f ${ZSH_CONFIG_DIR}/p10kcfg/compatibility.zsh ]]  || source ${ZSH_CONFIG_DIR}/p10kcfg/compatibility.zsh;
fi
