#!/bin/bash

### Assign first arg as pokemon
POKEMON="$1"

### Set temp files to hold raw data
DATADIR="/home/ez/Git/fuzzy-pokedex/pokeData"

### GET various pokeAPI endpoint URLs
endpoint_POKEMON="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON")"
endpoint_SPECIES="$(curl -s "https://pokeapi.co/api/v2/pokemon-species/$POKEMON")"
endpoint_ENCOUNTER="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON/encounters")"
endpoint_CHAIN="$(curl -s "$(jq -r '.evolution_chain["url"]' <<< "$endpoint_SPECIES")")"

### Parse JSON for attributes and assign to vars
IFS=$'\t' read -r NAME HEIGHT WEIGHT ID BASEX < <( jq -r '[.name, .height, .weight, .id, .base_experience] | @tsv' <<< "$endpoint_POKEMON")
IFS=$'\t' read -r BASEHAPPY CAPTURE GENUS < <( jq -r '[.base_happiness, .capture_rate, .genera[7].genus] | @tsv' <<< "$endpoint_SPECIES")
mapfile -t TYPE < <(jq -r '.types[].type["name"]' <<< "$endpoint_POKEMON"); TYPE_ITEM="${TYPE[0]}"
mapfile -t STAT < <(jq -r '.stats[].stat["name"]' <<< "$endpoint_POKEMON")
mapfile -t VAL < <(jq -r '.stats[].base_stat' <<< "$endpoint_POKEMON")
mapfile -t FLAVOR_ARRAY < <( jq '.flavor_text_entries[] | select(.language["name"] == "en") | .flavor_text'  <<< "$endpoint_SPECIES")
FLAVOR="$(tr -d '\"' < <(sed -e 's/\\./ /g' -e 's/\./& /g' -e 's/.\{28\}\s/&\n  /g' -e "s/this POKéMON/${NAME^}/g" \
	-e "s/This POKéMON/${NAME^}/g" -e 's/POKéMON/Pokémon/g' <<< "${FLAVOR_ARRAY[0]}"))"
mapfile -t CHAIN1 < <(jq -r '.chain.species["name"]' <<< "$endpoint_CHAIN")
mapfile -t CHAIN2 < <(jq -r '.chain.evolves_to[].species.name' <<< "$endpoint_CHAIN")
mapfile -t CHAIN3 < <(jq -r '.chain.evolves_to[].evolves_to[].species.name' <<< "$endpoint_CHAIN")
mapfile -t ABILITIES < <(jq -r '.abilities[].ability | [.name, .url] | @tsv' <<< "$endpoint_POKEMON")
mapfile -t LOCATE < <(jq -r '.[].location_area["name"]' <<< "$endpoint_ENCOUNTER")

### Print formatted data
{ printf "\n  %s    %s Type Pokémon\n" \
	"${NAME^}" "${TYPE_ITEM^}";
printf "\n  %s\n" "$FLAVOR";
if [[ -n "${CHAIN3[0]}" ]] ;
	then
		printf "\t   %s\n  %s %s %s %s %s\n\n" \
		"Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}" "►" "${CHAIN3^}";
	elif [[ -z "${CHAIN2[0]}" ]] ;
	then
		printf "  %s\n\n" "Single Stage Evolution Pokémon";
	else
		printf "\t%s\n      %s %s %s\n\n" \
	"Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}";
fi;
if [[ ${#CHAIN1[@]} -gt 1 ]] ;
	then
		printf "%s:\n" "Stage 1 Forms";
		for ((i=0; i<${#CHAIN1[@]}; i++)); do
			printf "  %s\n" "${CHAIN1[i]}";
		done;
	printf "\n";
fi;
if [[ ${#CHAIN2[@]} -gt 1 ]] ;
	then
		printf " %s:\n" "Stage 2 Forms";
		for ((i=0; i<${#CHAIN2[@]}; i++)); do
			printf "  %s\n" "${CHAIN2[i]}";
		done;
	printf "\n";
fi;
if [[ ${#CHAIN3[@]} -gt 1 ]] ;
	then
		printf " %s:\n" "Stage 3 Forms";
		for ((i=0; i<${#CHAIN3[@]}; i++)); do
			printf "  %s\n" "${CHAIN3[i]}";
		done;
	printf "\n";
fi;
printf "   ↞--------| STATS |--------↠ \n";
for ((i=0; i<${#STAT[@]}; i++)); do
	printf " %15s: %10s\n" "${STAT[i]}" "${VAL[i]}";
done;
printf "\n Genus: %s" "${GENUS^}";
printf "\n Happiness: %s\tPokedex ID: %s\n Base Exp: %s\tCapture Rate: %s\n Height:    %s\tWeight: %s\n" \
	"$BASEHAPPY" "$ID" "$BASEX" "$CAPTURE" "$HEIGHT" "$WEIGHT";
printf "\n Types:\n"
for ((i=0; i<${#TYPE[@]}; i++)); do
	TYPE_LIST_ITEM="${TYPE[i]}"
	printf "   %s\n" "${TYPE_LIST_ITEM^}"
done
printf "\n%s\n\n" " ↞---------------|ABILITIES|---------------↠"
for ((i=0; i<${#ABILITIES[@]}; i++)); do
	ABILITY_NAME="$(awk '{print $1}' <<< "${ABILITIES[i]}")"
	EFFECT_ENTRY="$(sed -e 's/\s\s/ /g' -e 's/^\w/  &/g' -e 's/.\{28\}\s/&\n  /g' \
		< <(jq -r '.effect_entries[] | select(.language["name"] == "en") | .effect' \
		< <(curl -s "$(awk '{print $2}' <<< "${ABILITIES[i]}")")))"
	printf "  %s:\n\n" "${ABILITY_NAME^}"
	printf "  %s\n\n" "$EFFECT_ENTRY"
done
printf "\n Location Areas:\n";
for ((i=0; i<${#LOCATE[@]}; i++)); do
	LOCATION_ITEM="$(sed 's/-/ /g' <<< "${LOCATE[i]}")";
	printf "   %s\n" "${LOCATION_ITEM^}";
done; } > "$DATADIR/$POKEMON"

