#!/bin/bash

### Help 
help() {
    printf "\n%s\n%s\n\n" "Use fzf to search Pokemon stats" "Can optionally search by name"
	printf "%s\n\t%s\n\n%s" "EXAMPLE" "pokedex [pokemon name]"
	printf "%s\n\t%s\n\t\t%s\n\t%s\n\t\t%s\n\t\t%s\n\t\t%s\n\t\t%s\n\t%s\n\t\t%s\n\n" "OPTIONS" "-q, --quick [pokemon]" "Prints single pokedex entry to terminal" "-u, --update [N/+N/-N/N%]" "Scrape web for updated Pokemon stats" "WARNING: update function is resource heavy" "See Parallel job control (-j) for options" "Default is 200%" "-h, --help" "Print this help screen"
	printf "%s\n\t%s\t%s\n\t%s\t%s\n\t%s\t%s\n\t%s\t%s\n\t%s\t%s\n\t%s\t%s\n" "KEYBINDS" "space" "Reads the Pokedex entry" "ctrl-space" "Stops reading the Pokedex entry" "ctrl-n" "Shows small sprite version" "ctrl-b" "Shows large sprite version" "ctrl-s" "Shows shiny sprite version" "ctrl-h" "Shows this help screen in preview window"
}

### Set pokeData location
POKEDATA="/usr/share/fuzzy-pokedex/pokeData"

### Pokedex viewer using pokemon-colorscripts and fzf
pokedex() {
	pokemon-colorscripts -l |
	fzf -q "$1" +m -s -i \
	--cycle \
	--reverse \
	--prompt=' ' \
	--pointer='󰐝 ' \
	--border=rounded \
	--border-label="╢ Fuzzy-Pokedex ╟" \
	--margin=4% \
	--padding=4% \
	--header="Choose Pokemon to view info" \
	--info=inline:'  󰨉 ' \
  	--color='fg+:15,fg:42,preview-fg:15,label:9,preview-label:15,hl+:134,hl:123,query:134,gutter:0,border:9,prompt:15,pointer:15,marker:15' \
	--tiebreak=begin,chunk,length \
	--preview="echo; pokemon-colorscripts -n {1} --no-title; cat $POKEDATA/{1}" \
	--preview-window=68%:wrap:border-rounded \
	--bind=focus:transform-preview-label:'echo [ {1} ] ' \
	--bind=ctrl-h:preview:"echo; pokedex -h;" \
	--bind=space:preview:"echo; pokemon-colorscripts -n {1} --no-title; cat $POKEDATA/{1}; espeak-ng -f $POKEDATA/{1} -g 2 -p 45 -s 160 -l 150 -ven+m3 &" \
	--bind=ctrl-space:preview:"echo; pokemon-colorscripts -n {1} --no-title; cat $POKEDATA/{1};" \
	--bind=ctrl-b:preview:"echo; pokemon-colorscripts -b -n {1} --no-title; cat $POKEDATA/{1};" \
	--bind=ctrl-s:preview:"echo; pokemon-colorscripts -s -n {1} --no-title; cat $POKEDATA/{1};" \
	--bind=ctrl-n:preview:"echo; pokemon-colorscripts -n {1} --no-title; cat $POKEDATA/{1};" |
	parallel pokemon-colorscripts --no-title -n {} "&&" cat "$POKEDATA/{}" 
}

### Quick view returns single Pokemon stat instead of opening pokedex 
quick_view() {
	parallel pokemon-colorscripts --no-title -n {} "&&" cat "$POKEDATA/{}" <<<"$1"
}

### Update Pokemon stats file set
update_pokeData() {
	printf "\n\t\t%s\n\t%s\n" "WARNING!!!" "This WILL take a long time"
	pokemon-colorscripts -l | sort | parallel --bar --color --retry-failed -j "${1:-200%}" pokeInfo {} ">" "$POKEDATA/{}" 2>&1
}

if [[ ! "$1" =~ ^- ]]; then
	pokedex "$1"
else
	while (( $# )); do
    	arg=$1
		shift
    	case "$arg" in
			-q|--quick)
				quick_view "$1"
				;;
    		-u|--update)
				update_pokeData "$1"
				;;
    		-h|--help)
    			help
   				;;       			
    		-*) 
    			echo "Invalid Usage"
   				help
   				;;
    	esac
	done
fi