#!/bin/sh

launch() {
	app=$(printf "Notes
	Files
	Editor
	Terminal
	Multiplexer
	LBRY
	Chat
	Music
	Browser
	Email
	Processes" | tr '\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) brave-browser;;
	Email) brave-browser mail.protonmail.com/login;;
	Processes) st -e htop;;
esac
}

action() {
	action=$(printf "Play Music
	Pause Music
	Toggle Music
	Command
	Command to clipboard
	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;;
	'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
}

ddg() {
	search_string="$(printf "" | dmenu -p 'ddg')"
	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")"
		# printf "this is newstring %s\n" "$new_string"
		i=$(expr $i + 1)
	done
	search="https://duckduckgo.com/?q=$new_string"
	brave-browser "$search"
}

rawurlencode() {
	case "$1" in
		[-_.~a-zA-Z0-9] ) result=$1; printf $result;;
			* ) result=$(printf '%%%02x' "'$1"); printf %$result;;
	esac
}

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;;
	*) printf "Invalid argument";;
esac