#!/bin/sh

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

launch() {
	app=$(printf "Notes
	Files
	Edit
	Editor
	Open
	Terminal
	Multiplexer
	LBRY
	Chat
	Music
	Browser
	Hidden browser
	Email
	Page
	Go page
	Package info
	Play clipboard
	Play downloads
	Processes" | tr -d '\t' |
		dmenu -i -p "Launcher")

	case $app in 
		Notes) st -t "Notes" -e vim "+cd ~/Notes/text" "+CtrlP";;
		Files) st -t "Files" -e sh -lc nnn;;
		Edit) editor;;
		Editor) st -t "Editor" -e vim "+CtrlPMRUFiles";;
		Open) opener;; 
		Terminal) st -t "Terminal";;
		Page) pages pager "$(printf "1\n2\n3\n" | dmenu -p 'page')";;
		Multiplexer) st -t "Multiplexer" -e tmux attach-session -t 0 || st -t "Multiplexer" -e tmux new-session -s 0;;
		LBRY) lbry;;
		'Go page') num=$(go_page); pages pager $num;;
		Chat) element-desktop;;
		Music) st -t "Music" -e ncmpcpp;;
		Browser) $browser_cmd;;
		'Hidden browser') $browser_cmd --incognito;;
		Email) $browser_cmd mail.protonmail.com/login;;
	'Play clipboard') mpv "$(xclip -o -selection clipboard)";;
	'Play downloads') mpv "$HOME/Downloads/tmp/$(ls ~/Downloads/tmp/ | dmenu -i -l 10 -p 'play')";;
		Processes) st -t "Processes" -e htop;;
	esac
}

action() {
	action=$(printf "Toggle Music
	Pause Music
	Play Music
	Command
	Command to clipboard
	Choose
	Select VPN
	Disable VPN
	Enable VPN
	VPN Status
	Rebind Keys
	Show calender
	Disable Bar
	Password
	Username
	Alternate password
	Download
	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;;
		'Command') cmd;;
		'Choose') chooser;;
		'Command to clipboard') cmd_clip;;
		'Username') username;;
		'Password') password;;
		'Alternate password') other_password;;
		'Show calender') notify-send 'Calender' "\n\n$(cal)";;
		#This should check for an error code and confirm that download has started
		'Download') youtube-dl --no-progress -o "$HOME/Downloads/tmp/%(title)s.%(ext)s" "$(xclip -selection clipboard -o)"; notify-send -u low -t 3000 "Download complete";;
	esac
}

do_search() {
	type=$(printf "Manual
		DDG
		Definition
		Godocs
		Mojeek
		Package
		Github" | tr -d '\t' | dmenu -i -p "Search")
	
	case "$type" in
		Manual) p=$(printf '' | dmenu -p 'man'); if [ -z $p ]; then exit; fi; st -t "Manual $p" -e man "$p";;
		Github) github;;
		Godocs) godoc;;
		DDG) ddg;;
		Package) package;;
		Definition) word=$(printf '' | dmenu -p 'word'); if [ -z "$word" ]; then exit; fi; st -e sh -lc "dict \"$word\" | less";;
		esac
}

package() {
	type=$(printf "search\ninfo" | dmenu -p 'package')
	if [ -z "$type" ]; then
		exit
	elif [ "$type" = "info" ]; then
		s=$(dmenu_path | dmenu -i -p 'package info')
		st -e sh -lc "apt-cache show $s"
	else
		s=$(dmenu -i -p 'package search')
		st -e sh -lc "apt search \"$s\""
	fi
}

editor() {
	#handle spacing in filenames bug
	file=$(mru list | dmenu -i -l 10 -p 'edit what?')
	if [ -z "$file" ]; then exit; fi
	first=$(printf "$file" | head -n1 -)
	d=$(dirname "$first")
	files=$(printf "$file" | tr '\n' ' ')
	printf "files: $files"
	st -t 'Editor' -e vim "+cd $d" $files
}

opener() {
	mru list | dmenu -i -l 20 -p 'open' | while read f; do mru insert "$f" & xdg-open $f; done
}

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";
}

go_page() {
	name=$(printf "1\n2\n3\n4" | dmenu -p "page name")
	go doc "$(printf "" | dmenu -p 'go doc search')" > /tmp/pages-$name
	printf "$name"
}

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

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

	output=$( $c )
	notify-send -u low 'command output' "$output"
}

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
}

username() {
	account=$(find ~/.password-store/[!\.]* -type f |
		sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
		dmenu -p 'account')
	if [ -z "$account" ]; then exit; fi
	name=$(pass show "$account" | sed -n -e "s/^username: //p" |
		dmenu -p 'which username' | xclip -f -selection clipboard)
	if [ -z "$name" ]; then exit; fi
	notify-send -u low -t 2000 "username copied" "$account: $name"
}

password() {
	account=$(find ~/.password-store/[!\.]* -type f |
		sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
		dmenu -p 'which service')
	pass -c $account
	notify-send -u low -t 1000 'password copied' "Copied $account"

}

other_password() {
	account=$(find ~/.password-store/[!\.]* -type f |
		sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
		dmenu -p 'service')
	if [ -z "$account" ]; then exit; fi
	name=$(pass show "$account" | sed -n -e "s/^username: //p" |
		dmenu -p 'which user password')
	if [ -z "$name" ]; then exit; fi
	notify-send -u low -t 2000 "password copied" "$account: $name"

	#figure out range selections
	sed -n -e ""
}

edit_password() {
	account=$(find ~/.password-store/[!\.]* -type f |
		sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
		dmenu -p 'which service')
	if [ -z "$account" ]; then exit; fi
	error=$(st -e pass edit "$account" 2>&1)
	if [ -n "$error" ]; then notify-send -u low -t 3000 "password edit error" "$error"; fi
}

chooser() {
	f=$(mru list | dmenu -i -l 20 -p 'choose file')
	if [ -z "$f" ]; then exit; fi
	c=$(dmenu_path | dmenu -i -p 'operation')
	if [ -z $c ]; then exit; fi
	$c "$f"
}

case $1 in
	launch) launch;;
	action) action;;
	cmd) cmd;;
	cmd_clip) cmd_clip;;
	username) username;;
	password) password;;
	other_password) other_password;;
	edit_password) edit_password;;
	search) do_search;;
	open) opener;;
	choose) chooser;;
	*) printf "Invalid argument";;
esac