# display a log message if verbose mode is enabled
lg() {
	((VERBOSE)) && printf "\033[1;35m%s\033[0m\n" "$*" >&2
}

# display error message and exit
die() {
	((!SILENT)) && printf "\033[1;31m%s\033[0m\n" "$*" >&2
	exit 1
}

# gets the list of directories and playable files from the passed in directory
# sets the $watched list to the list of watched files
get_directory_data() {
	search_dir="$1"
	inputlist=""
	watched=""
	cnt=1
	[ "$search_dir" = "/" ] && cnt=0 # account for no ../ on /
	for directory in "$1"/*; do
		directory="${directory##*/}"
		[ ! -d "$search_dir/$directory" ] && continue
		[ -z "$inputlist" ] && inputlist="$directory" || inputlist="$inputlist|$directory"
		if ! check_db "directory" "$search_dir/$directory"; then
			lg "$search_dir/$directory opened before... adding $cnt to list" 1> /dev/stderr
			[ -z "$watched" ] && watched="$cnt" || watched="$watched, $cnt"
		fi
		((++cnt))
	done
	shopt -s nullglob   # set nullglob to avoid printing output if no files with extension exist
	shopt -s nocaseglob # case insensitive globbing
	for filename in "$search_dir"/*.{mp4,mkv,ts,mp3,webm}; do
		filename="${filename##*/}"
		[ -z "$inputlist" ] && inputlist="$filename" || inputlist="$inputlist|$filename"
		if ! check_db "file" "$search_dir" "$filename"; then
			lg "$filename watched before... adding $cnt to list" 1> /dev/stderr
			[ -z "$watched" ] && watched="$cnt" || watched="$watched, $cnt"
		fi
		((++cnt))
	done
	shopt -u nullglob
	shopt -u nocaseglob
	if [[ -n "$inputlist" && "$search_dir" != / ]]; then
		inputlist="../|$inputlist|Back|Quit"
	elif [[ -z "$inputlist" && "$search_dir" != / ]]; then
		inputlist="../|Back|Quit"
	elif [[ "$search_dir" = / ]]; then
		inputlist="$inputlist|Quit"
	else
		inputlist="Quit"
	fi
	lg "INPUT LIST: $inputlist" 1> /dev/stderr
	lg "WATCHED LIST: $watched" 1> /dev/stderr
}

# recursive function for finding path to video file given a starting directory
find_media() {
	inp="$1"
	[ -z "$inp" ] && die "No directory"
	lg "BEGIN find_media() on $inp" 1> /dev/stderr

	# base case hit when a file is found
	if [ -f "$inp" ]; then
		printf "%s\n" "$inp"
		return 0
	fi

	get_directory_data "$inp"
	selection="$(rofi -dpi "$DPI" -dmenu -no-custom -async-pre-read 33 -config "$ROFI_CFG" \
		-l 15 -i -sep '|' -mesg "$(generate_span "Current directory: $inp")" -a "$watched" \
		-p "Enter selection" -window-title 'aniwrapper' <<< "$inputlist")"
	[ -z "$selection" ] && return 1
	case "$selection" in
		Back | ../)
			dotdotslash="${inp%/*}"
			[ -z "$dotdotslash" ] && dotdotslash="/"
			insert_history "directory" "$dotdotslash"
			find_media "$dotdotslash"
			;;
		Quit)
			return 1
			;;
		*)
			if [ -d "$inp/$selection" ]; then
				insert_history "directory" "$inp/$selection"
				if [ "$inp" = "/" ]; then
					find_media "/$selection"
				else
					find_media "$inp/$selection"
				fi
			else
				find_media "$inp/$selection"
			fi
			;;
	esac
}

# opens the passed in file with $PLAYER_FN
play_file() {
	lg "Checking if file is playable"
	if [[ "$1" =~ (\.mp4|\.mkv|\.ts|\.webm)$ ]]; then
		filename="${1##*/}"
		directory="${1%/*}"
		insert_history "file" "$directory" "$filename"
		notification "Playing $1"
		case "$PLAYER_FN" in
			mpv)
				nohup "$PLAYER_FN" --force-media-title="aniwrapper: play-from-file - $1" "$1" > /dev/null 2>&1 &
				;;
			*)
				nohup "$PLAYER_FN" "$1" > /dev/null 2>&1 &
				;;
		esac
		return $?
	else
		die "File: $1 is not playable... Quitting"
	fi
}


# vim: ft=sh
