#!/bin/sh

#shellcheck disable=SC2086,SC2068

menu_droid() {
	fzf --prompt="$1" $2
}

menu_linux() {
	bemenu --fn 'IBM Plex Sans 15' -i -c -W "0.${3:-6}" -B 3 -l 25 -p "$1" --bdr="#$GLOBAL_ACCENT" --tf="#$GLOBAL_ACCENT" --hf="#$GLOBAL_ACCENT" -R 20
}

search_droid() {
	printf  "\n\033[1;33m%s\033[0m" "$1" && read -r query
}

search_linux() {
	query=$(: | menu_linux "$1 ")
	export query
}

torrent_linux() {
	setsid -f "$TERMINAL" -e "${0%/*}/torrent" $@
}

torrent_droid() {
	"${0%/*}/torrent" $@
}

info_droid() {
	printf "\n\r\033[1;36m%s\033[0m" "$1"
}

info_linux() {
	notify-send -e "$1" -r '10' -i "$2" $3
}

error_droid() {
	printf "\33[2K\r\033[1;31m%s\033[0m" "$1"
}

error_linux() {
	notify-send -e -u "critical" "$1" -r '10'
}

player_droid() {
	url=$1
	title=$2
	shift 2

	dir="/sdcard/mpv"
	file="$dir/mpv.config.mp4"
	if [ -w "${dir%*/.mpv}" ]; then
	    [ -d "${dir}" ] || mkdir "${dir}"
	    printf "%s" "$@" | sed 's| --|\n|g; s|^--||g' > "$file"
	    sleep 10 && : > "$file" &
	else
	    printf "\n\033[1;31mCannot write\033[0m → %s\n" "$file"
	    printf "\033[1;36mRun:\033[0m termux-setup-storage\n\n"
	    printf "\033[1;36mThen:\033[0m\n"
	    printf "> allow storage permission\n"
	    printf "> add → include='%s'\n" "$file"
	    printf "  to MPV → Settings > Advanced > mpv.conf\n"
	    printf "  Make sure to press enter after pasting the content above for MPV to recognise it.."
	fi
	am start --user 0 -a android.intent.action.VIEW -d "$url" -n is.xyz.mpv/.MPVActivity -e "title" "$title" >/dev/null 2>&1
	#am start --user 0 -a android.intent.action.VIEW -d "$1" -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -e "title" "$2" >/dev/null 2>&1
}

player_linux() {
	url=$1
	title=$2
	shift 2

	setsid -f mpv "$url" --force-media-title="$title" $@
}
