// Variables
// --DOCUMENT
const guessInput = $("#guessInput");
const answerText = $("#answer");
const guessText = $("#alreadyGuessed");
const feedText = $("#feedback");
const debug = false;
// --GLOBAL ( GFX&SYS )
var tries = 0;
// --GAME
var oWord = "";
var cWord = "";
var gesult = [];
var misses = [];
var wins = 0;
// --THREAD
var delay = null;

// Functions
function lerp(a,b,t){
    return a+t*(b-a);
}
function reset(){
    if(debug) {console.log("Resetting.");}
    if(delay != null) {window.clearInterval(delay); delay = null;}

    // Finding a random word
    cWord = all_words[Math.floor(Math.random() * all_words.length)];
    oWord = cWord;
    gesult = [];
    misses = [];
    var result = "";

    // For next game purposes
    wins = 0;
    tries = 0;

    // Making the answer text as long as we need it
    for(var i = 0; i < cWord.length; i++){
        result += "_ ";
        gesult.push("_ ");
    }

    answerText.text(result);
    guessText.text("");
    guessInput.attr("disabled", false);
    guessInput.focus();

    // Debug
    if(debug) {console.log(cWord);}
}
function shwMSG(msg){
    if(delay != null) {window.clearInterval(delay); feedText.text(""); delay = null; return;}
    feedText.text(msg);
    delay = window.setInterval(function(){
        shwMSG(gesult.join(""));
    }, 2500);
}
function checkGuess(){
    // Debug
    if(debug) {console.log("Word:", guessInput.val());}

    // checking letter against word
    let wordArray = cWord.split("");
    let wantWord = guessInput.val().toLowerCase();
    var success = false;
    
    // Misses
    for(var i = 0; i < misses.length; i++){
        if(misses[i] == wantWord){
            shwMSG("You've already used that letter.");
            return true;
        }
    }

    // Going through to find a match
    for(var i = 0; i < wordArray.length; i++){
        // Hmm, I think it matches?
        if(wordArray[i] == wantWord){
            success = true;
            wordArray[i] = '.';
            gesult[i] = wantWord;
            wins += 1;
        }
    }

    if(!success) {misses.push(wantWord);}

    // Displaying the correct word now
    // The answer text
    answerText.text(gesult.join(""));
    
    // The misses text
    var result = "";
    for(var i = 0; i < misses.length; i++) {result += misses[i] + ", ";}
    guessText.text(result);
    // Removing it from the correct word ( No doubles. )
    cWord = wordArray.join("");

    return success;
}
function endgame(text){
    if(delay != null) {return;}

    answerText.text(text);
    delay = window.setInterval(reset, 2500);

    guessInput.attr("disabled", true);
}

// Events
$("form").on("submit", function(event){
    // Prevent reload
    event.preventDefault();

    // Did the user submit nothing?
    if(guessInput.val().length <= 0) {return;}

    // Checking stuff
    if(!checkGuess()){
        if(tries < 7) {tries++;}
    }

    // Did we win?
    if(wins >= cWord.length)    {endgame("YOU WIN");}
    if(tries >= 7)              {endgame("YOU LOOSE! YOUR WORD WAS " + oWord);}

    // Reset input field
    guessInput.val("");
});
window.onload = reset;