#!/usr/bin/env bash
words=($(egrep '^[a-z|ñ]{5}$' $PWD/words.es | tr '[A-Z]' '[a-z]'))
actual=${words[$[$RANDOM % ${#words[@]}]]} end=false guess_count=0 max_guess=6
if [[ $1 == "unlimit" ]]; then
    max_guess=999999
fi
while [[ $end != true ]]; do
    guess_count=$(( $guess_count + 1 ))
    if [[ $guess_count -le $max_guess ]]; then
        echo "[Introduce una palabra de cinco letras:] ($guess_count / $max_guess):"
        read guess
        guess=$(echo $guess | tr '[A-Z]' '[a-z]')
        if [[ " ${words[*]} " =~ " $guess " ]]; then
            output="" remaining=""
            if [[ $actual == $guess ]]; then
                echo "¡Acertaste!"
                for ((i = 0; i < ${#actual}; i++)); do
                    output+="\033[30;102m ${guess:$i:1} \033[0m"
                done
                printf "$output\n"
                end=true
            else
                for ((i = 0; i < ${#actual}; i++)); do
                    if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
                        remaining+=${actual:$i:1}
                    fi
                done
                for ((i = 0; i < ${#actual}; i++)); do
                    if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
                        if [[ "$remaining" == *"${guess:$i:1}"* ]]; then
                            output+="\033[30;103m ${guess:$i:1} \033[0m"
                            remaining=${remaining/"${guess:$i:1}"/}
                        else
                            output+="\033[30;107m ${guess:$i:1} \033[0m"
                        fi
                    else
                        output+="\033[30;102m ${guess:$i:1} \033[0m"
                    fi
                done
                printf "$output\n"
            fi
        else
            echo "Palabra no válida.";
            guess_count=$(( $guess_count - 1 ))
        fi
    else
        echo "¡Has perdido! la palabra a buscar era:"
        echo $actual
        end=true
    fi
done
