#!/bin/sh

browser_cmd="brave-browser"
browser_new_cmd="brave-browser --new-window"

launch() {
	app=$(printf "Notes
	Files
	Editor
	Terminal
	Multiplexer
	LBRY
	Chat
	Music
	Browser
	Email
	Processes" | tr -d '\t' |
		dmenu -i -p "Launcher")

case $app in 
	Notes) st -e vim "+cd ~/Notes/text" "+CtrlP";;
	Files) st -e nnn;;
	Editor) st -e vim;;
	Terminal) st;;
	Multiplexer) st -e tmux $*;;
	LBRY) lbry;;
	Chat) element-desktop;;
	Music) st -e ncmpcpp;;
	Browser) $browser_cmd;;
	Email) $browser_cmd mail.protonmail.com/login;;
	Processes) st -e htop;;
esac
}

action() {
	action=$(printf "Play Music
	Pause Music
	Toggle Music
	Command
	Command to clipboard
	Search github
	Search ddg
	Search godoc
	Select VPN
	Disable VPN
	Enable VPN
	VPN Status
	Rebind Keys
	Disable Bar
	Enable Bar" | tr -d '\t' |
		dmenu -i -p "Actions")

case $action in
	'Play Music') mpc play ;;
	'Pause Music') mpc pause ;;
	'Toggle Music') mpc toggle ;;
	'Rebind Keys') setup-xbindkeys;;
	'Disable Bar') tmux set -g status off;;
	'Enable Bar') tmux set -g status on;;
	'Search github') github;;
	'Search godoc') godoc;;
	'Search ddg') ddg;;
	'Command') cmd;;
	'Command to clipboard') cmd_clip;;
esac
}

screenshot() {
	method=$(printf "clipboard
file
both" |
	dmenu -i -p "Screenshot")

	if [ $method = "file" ]; then
		scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c 255,0,0,255 -t 'Hi'"\
			-s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png 
	fi

	if [ $method = "clipboard" ]; then
		scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c 255,0,0,255 -t 'Hi'"\
			-s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png\
			-e "xclip $f; rm $f"
	fi

	if [ $method = "both" ]; then
		scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c 255,0,0,255 -t 'Hi'"\
			-s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png\
			-e "xclip $f;"
	fi
}

search_type() {
	search_string="$(printf "New window\nBookmarks\nHistory" | dmenu -p "$1")"
	search_mode='tab'

	case "$search_string" in
		'New window') search_mode='window'; search_string=$(printf '' | dmenu -p "new $1");;
		'Bookmarks');;
		'History') ;;
		# *) printf "Invalid argument";;
	esac
	printf "$search_string|$search_mode"
}

brave_search() {
	search_prefix="$1"
	search_info="$(search_type "$2")"
	search_string=$(printf "$search_info" | cut -d'|' -f1)
	search_mode=$(printf "$search_info" | cut -d'|' -f2)
	if [ -z "$search_string" ]; then exit; fi
	new_string=$(rawurlencode "$search_string")
	search="$search_prefix$new_string"
	case "$search_mode" in 
		tab) $browser_cmd "$search";;
		window) $browser_new_cmd "$search";;
	esac
}

rawurlencode() {
	#Takes the first argument and encodes it
	search_string="$1"
	string_length=$(expr length "$search_string")
	char=''; new_string=''; 

	i=1
	while [ $i -le $string_length ]
	do
		char=$(expr substr "$search_string" $i 1)
		# new_string="$new_string$(rawurlencode "$char")"
		case "$char" in
			[-_.~a-zA-Z0-9] ) new_string="$new_string$char";;
				* ) new_string="$new_string%$(printf '%%%02x' "'$char")";;
		esac
		i=$(expr $i + 1)
	done
	printf "$new_string"
}

ddg() {
	brave_search "https://duckduckgo.com/?q=" "ddg"
}

github() {
	brave_search "https://github.com/search?q=" "github"
}

godoc() {
	brave_search "https://golang.org/pkg/" "godoc"
}

manual() {
	search=$(printf '' | dmenu -p 'manual')
	result=$(man "$search")
	printf "$result" | enscript -p - | ps2pdf - | zathura -
}

goinfo() {
	#Maybe this can also show recent searches
	search=$(printf "Window\nNotify" | dmenu -p 'goinfo')
	case $search in
		Window) search=$(printf '' | dmenu -p 'goinfo'); result=$(go doc "$search"); infowindow "$result"; exit;;
		Notify) search=$(printf '' | dmenu -p 'goinfo'); result=$(go doc "$search"); notify-send -u low -t 0 "Go documentation" "$result"; exit;;
	esac
	result=$(go doc "$search"); notify-send -u low -t 0 "Go documentation" "$result";
}

infowindow() {
	printf "$1" | enscript -p - | ps2pdf - | zathura -
}

cmd() {
	sh -c "$(printf '' | dmenu -i -p 'cmd')"
}

cmd_clip() {
	value=$(cmd)
	show_value="$(expr substr "$value" 1 200)\n..."
	notify-send -u low -t 2000 "Items cliped" "$show_value"
	printf "%s" "$value" | xclip -selection clipboard
}

case $1 in
	launch) launch;;
	action) action;;
	cmd) action;;
	cmd_clip) cmd_clip;;
	ddg) ddg;;
	github) github;;
	godoc) godoc;;
	goinfo) goinfo;;
	manual) manual;;
	*) printf "Invalid argument";;
esac