unsetopt HIST_SAVE_NO_DUPS                               #HIST DUPS SHOULD BE ALSO SAVED

GREEN='\033[0;32m'
RED='\033[0;31m'
WHITE='\033[0;37m'
RESET='\033[0m'

autoload -U compinit && compinit                         #COMPLETION
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'   #CASE-INSENSITIVE COMPLETION
_comp_options+=(globdots)                                #HIDEN FILES

setopt AUTO_PUSHD                                        #PUSH THE CURRENT DIRECTORY TO THE VISITED STACK
setopt PUSHD_IGNORE_DUPS                                 #PUSH ONLY NEW DIRS
setopt PUSHD_SILENT                                      #DO NOT PRINT ANYTHING

stty stop undef                                          #DISABLE TERMINAL FREEZE

unsetopt BEEP                                            #DISABLE BEEP

function zsh_add_file() {
	[ -f "$ZDOTDIR/$1" ] && source "$ZDOTDIR/$1"
}

function zsh_add_plugin() {
	PLUGIN_NAME=$(echo $1 | cut -d "/" -f 2)

	if [ -d "$ZDOTDIR/plugins/$PLUGIN_NAME" ]; then
		zsh_add_file "plugins/$PLUGIN_NAME/$PLUGIN_NAME.plugin.zsh" || \
		zsh_add_file "plugins/$PLUGIN_NAME/$PLUGIN_NAME.zsh"

		echo "${RED}Plugged in ${GREEN}$PLUGIN_NAME${RED}!${RESET}\n"
	else
		GIT_SITE=$2
		[ -v $GIT_SITE ] && GIT_SITE="https://github.com/"

		echo "${RED}Downloading ${GREEN}$PLUGIN_NAME ${RED}plugin...${RESET}"

		git clone "$GIT_SITE/$1.git" "$ZDOTDIR/plugins/$PLUGIN_NAME" > /dev/null 2>&1 && echo "${RED}Successfully downloaded ${GREEN}$PLUGIN_NAME ${RED}plugin!${RESET}" && zsh_add_plugin $1
	fi
}

function zsh_add_theme() {
	THEME_NAME=$(echo $1 | cut -d "/" -f 2)

	if [ -d "$ZDOTDIR/themes/$THEME_NAME" ]; then
		zsh_add_file "themes/$THEME_NAME/$THEME_NAME.zsh-theme"

		echo "${RED}Themed a ${GREEN}$THEME_NAME ${RED}theme!${RESET}\n"
	else
		GIT_SITE=$2
		[ -v $GIT_SITE ] && GIT_SITE="https://github.com"

		echo "${RED}Downloading ${GREEN}$THEME_NAME ${RED}theme...${RESET}"

		git clone "$GIT_SITE/$1.git" "$ZDOTDIR/themes/$THEME_NAME" > /dev/null 2>&1 && echo "${RED}Successfully downloaded ${GREEN}$THEME_NAME ${RED}theme!${RESET}" && zsh_add_theme $1
	fi
}

function zsh_add_completion() {
	COMPLETION_NAME=$(echo $1 | cut -d "/" -f 2)

	if [ -d "$ZDOTDIR/completions/$COMPLETION_NAME" ]; then
		completion_file_path=$(ls $ZDOTDIR/completions/$COMPLETION_NAME/_*)
		
		fpath+="$(dirname "${completion_file_path}")"

		zsh_add_file "completions/$COMPLETION_NAME/$COMPLETION_NAME/.plugin.zsh"
		
		echo "${RED}Inserted a completion: ${GREEN}$COMPLETION_NAME${RED}!${RESET}\n"
	else
		GIT_SITE=$3
		[ -v $GIT_SITE ] && GIT_SITE="https://github.com"
		
		echo "${RED}Downloading ${GREEN}$COMPLETION_NAME ${RED}completion...${RESET}"
		
		git clone "$GIT_SITE/$1.git" "$ZDOTDIR/completions/$COMPLETION_NAME" > /dev/null 2>&1 && echo "${RED}Successfully downloaded ${GREEN}$COMPLETION_NAME ${RED}completion!${RESET}"
		
		fpath+=$(ls $ZDOTDIR/completions/$COMPLETION_NAME/_*)
		
		zsh_add_completion $1
		
		return
	fi
	
	completion_file="$(basename "${completion_file_path}")"
	
	if [ "$2" = true ] && compinit "${completion_file:1}"
}

function zsh-update() {
	return_dir=$(pwd)

	zsh_update_git_subfolders "plugins"
	zsh_update_git_subfolders "themes"
	zsh_update_git_subfolders "completions"

	cd $return_dir
}

function zsh_update_git_subfolders() {
	directory="$ZDOTDIR/$1"

	echo "${RED}Updating ${GREEN}$1...${RESET}\n"

	cd $directory

	for D in *; do
		if [ -d "${D}" ]; then
			cd $D
			echo "${RED}Updating ${GREEN}$(echo $D | cut -d "/" -f 2)${RED}..."
			git pull
			cd $directory
		fi
	done

	echo "${RESET}"

	return
}

zsh_add_file ".zaliases"                                 #IMPORT ALIASES

zsh_add_plugin "zsh-users/zsh-autosuggestions"
zsh_add_plugin "zsh-users/zsh-syntax-highlighting"

zsh_add_theme "jackharrisonsherlock/common"

zsh_add_completion "esc/conda-zsh-completion" false

clear

autoload -U promptinit
promptinit