# Oh My Posh
#########################################################
export PATH=$PATH:"$HOME/.local/bin"
OMP_EXECUTABLE="${XDG_DATA_HOME:-${HOME}/.local}/bin/oh-my-posh"
[ ! -f $OMP_EXECUTABLE ] && curl -s https://ohmyposh.dev/install.sh | bash -s
eval "$(oh-my-posh init zsh --config ~/.config/omp.toml)"

# Zinit
#########################################################
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
[ ! -d $ZINIT_HOME ] && mkdir -p "$(dirname $ZINIT_HOME)"
[ ! -d $ZINIT_HOME/.git ] && git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
source "${ZINIT_HOME}/zinit.zsh"

# Core Plugins
zinit snippet OMZP::command-not-found
zinit snippet OMZP::colored-man-pages
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma-continuum/fast-syntax-highlighting

autoload -Uz compinit
compinit

# Basic ZSH
#########################################################

# History
HISTFILE=~/.histfile
HISTSIZE=100000
SAVEHIST=100000

# Disable Bell
unsetopt beep

# Keybinding
bindkey '\e[1;5C' forward-word
bindkey '\e[1;5D' backward-word
WORDCHARS="_-" # Define These Characters as Part of "Words"

# Application Envs
#########################################################
export EDITOR=nvim

# GPG
if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then
  export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
fi
export GPG_TTY=$(tty)
gpg-connect-agent updatestartuptty /bye >/dev/null

zinit snippet OMZP::git
zinit snippet OMZP::docker-compose
zinit snippet OMZP::podman
zinit snippet OMZP::toolbox
zinit snippet OMZP::rust
zinit snippet OMZP::composer
zinit snippet OMZP::symfony6

source $HOME/.profile

# Install Dependencies
[ ! -d $VOLTA_HOME ] && curl https://get.volta.sh | bash -s -- --skip-setup && volta install node@lts
command -v go &>/dev/null || sudo dnf install go
[ ! -d $RUSTUP_HOME ] && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path -y
command -v composer &>/dev/null || sudo dnf install composer

# Install Scripts
$HOME/.config/install.sh

# Aliases
#########################################################
alias edit-nvim-conf="nvim ~/.config/nvim"
alias edit-zsh-conf="nvim ~/.zshrc"
alias hist="eval \$(cat $HISTFILE | fzf)"
alias nano="nvim"
alias neofetch="fastfetch"
alias notes="glow $HOME/Dokumente/Notes --all"
ws() {
	local -A path_icons=(
        ["$HOME/Workspace/Learning"]='3,󰑴 '
        ["$HOME/Workspace/School"]='3, '
        ["$HOME/Workspace/AdventOfCode"]='3, '
        ["$HOME/Workspace/Userscripts"]='3,󰯁'
        ["$HOME/Workspace/TowerDefence"]='3,'
        ["$HOME/Workspace/Engine"]='3,󱎓'
        ["$HOME/Workspace/Contribute"]='3,󰮮'
        ["$HOME/Workspace/Contribute/Forgejo"]='4,'
        ["$HOME/Workspace"]='2, '
	)

    typeset -A display_to_path
    projects=()
	find "$HOME/Workspace" -maxdepth 4 -type d \( -name ".git" -o -name "go.mod" -o -name "Cargo.toml" -o -name "compose.yaml" -o -name "package.json" \) -exec dirname {} \; | sort -u | \
    while IFS= read -r line; do
    	display_line="$line"
        best_prio=0
        for path_target entry in "${(@kv)path_icons}"; do
    		if [[ $line == $path_target* ]]; then
                IFS=, read -r prio icon <<< "$entry"
                if [[ $prio -gt $best_prio ]]; then
      				display_line="$icon${line//$path_target/}"
                    best_prio=$prio
                fi
    		fi
  		done
        display_to_path[$display_line]=$line
        projects+=($display_line)
  	done

    selected=$display_to_path[$(print -l $projects | fzf)]
	if [[ -n $selected ]]; then
        cd $selected
    fi
}

