#!/usr/bin/env bash

THEME="${THEME:-$HOME/.local/share/SubMiner/themes/subminer.rasi}"
FONTCONFIG_FILE=$HOME/.config/mpv/mpv-fonts.conf
COMMAND=mpv
VIDEO_EXTENSIONS="mkv|mp4|avi|webm|mov|flv|wmv|m4v|ts|m2ts"
TARGET_DIRECTORY=""

# Parse command-line options first
while getopts ":st:" opt; do
	case $opt in
		s)
			COMMAND="$COMMAND --profile=subminer"
			;;
		t)
			THEME="$OPTARG"
			;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			exit 1
			;;
		:)
			echo "Option -$OPTARG requires an argument." >&2
			exit 1
			;;
	esac
done
shift $((OPTIND - 1))

if [[ $# -gt 1 ]]; then
	echo "Usage: $0 [-s] [-t theme] [directory]" >&2
	exit 1
fi

if [[ $# -eq 1 ]]; then
	if [[ ! -d "$1" ]]; then
		echo "Invalid directory: $1" >&2
		exit 1
	fi
	TARGET_DIRECTORY="$(realpath "$1")"
fi

find_videos() {
	find "$PWD" -maxdepth 1 -type f -regextype posix-extended \
		-iregex ".*\.($VIDEO_EXTENSIONS)$" 2> /dev/null | sort -V
}

build_rofi_menu() {
	while IFS= read -r video; do
		[ -z "$video" ] && continue
		local display_name
		display_name=$(basename "$video")
		printf '%s\0icon\x1fthumbnail://%s\n' "$display_name" "$video"
	done < <(find_videos)
}

is_video_file() {
	local file="$1"
	local regex="\\.($VIDEO_EXTENSIONS)$"

	if [[ ! "${file,,}" =~ $regex ]]; then
		return 1
	fi

	if command -v file > /dev/null 2>&1; then
		local mime
		mime=$(file -b --mime-type "$file" 2>/dev/null || true)
		[[ "$mime" == video/* ]] && return 0
	fi

	return 0
}

select_video_via_rofi() {
	if [[ -n "$TARGET_DIRECTORY" ]]; then
		local selection=""
		local prompt="Choose Video "
		while true; do
			selection=$(rofi -show filebrowser -show-icons -theme "$THEME" \
				-theme-str 'configuration {font: "JetBrainsMono Nerd Font 10";} listview {columns: 1; lines: 15;} window {width: 88%;}' \
				-p "$prompt" \
				-filebrowser-directory "$TARGET_DIRECTORY" \
				-filebrowser-cancel-returns-1 true)

			[[ -z "$selection" ]] && return 1

			if [[ "$selection" != /* ]]; then
				selection="$TARGET_DIRECTORY/$selection"
			fi

			if [[ -d "$selection" ]]; then
				prompt="Choose Video (folders are not selectable)"
				continue
			fi

			if is_video_file "$selection"; then
				echo "$selection"
				return 0
			fi

			prompt="Choose Video (select a video file)"
		done
	fi

	build_rofi_menu | rofi -dmenu -i -show-icons -theme "$THEME" \
		-theme-str 'configuration {font: "JetBrainsMono Nerd Font 10";} listview {columns: 1; lines: 15;} window {width: 88%;}' -p "Choose Video "
}

selection=$(select_video_via_rofi)

get_video_thumbnail() {
	local video="$1"
	local thumb_dir="$HOME/.cache/thumbnails/large"
	local video_uri="file://$(realpath "$video")"
	local thumb_hash=$(echo -n "$video_uri" | md5sum | cut -d' ' -f1)
	local thumb_path="$thumb_dir/$thumb_hash.png"

	if [[ -f "$thumb_path" ]]; then
		echo "$thumb_path"
		return 0
	fi

	local tmp_thumb="/tmp/rmpv-thumb-$$.jpg"
	if command -v ffmpegthumbnailer &> /dev/null; then
		ffmpegthumbnailer -i "$video" -o "$tmp_thumb" -s 512 -q 5 2> /dev/null && echo "$tmp_thumb"
	elif command -v ffmpeg &> /dev/null; then
		ffmpeg -i "$video" -ss 00:00:30 -vframes 1 -vf "scale=512:-1" "$tmp_thumb" 2> /dev/null && echo "$tmp_thumb"
	fi
}

if [[ -z "$selection" ]]; then
	echo "No video selected."
	exit 1
fi

if [[ -n "$TARGET_DIRECTORY" ]]; then
	choice="$selection"
	if [[ "$choice" != /* ]]; then
		choice="$TARGET_DIRECTORY/$choice"
	fi
else
	choice="./$selection"
fi

if [[ ! -f "$choice" ]]; then
	echo "Selected item is not a valid file: $choice" >&2
	exit 1
fi

THUMBNAIL_PATH=$(get_video_thumbnail "$choice")
if [[ -n "$THUMBNAIL_PATH" && -f "$THUMBNAIL_PATH" ]]; then
	notify-send -i "$THUMBNAIL_PATH" "Playing Video" "$(basename "$choice")"
else
	notify-send "Playing Video" "$(basename "$choice")"
fi
$COMMAND "$choice" &

# vim: ft=sh
