// Variables
// --Constant
const retryDelay = 2500;
var allWords = ["no","words"];
// --Items
let usrField = $("form");
let usrButton = $("#finalButton");
let skpButton = $("#skipButton");
let hinButton = $("#hintButton");
let scrText = $("#scrambled");
let gusText = $("#guess");
let hinText = $("#hint");
// --Preset
var usrTries = 0;
var usrIndex = 0;
var usrScrambled = "";
// --Thread
var delay = null;

// Functions
function getWords(){
    retrieveWords(allWords);
}
function checkWord(){
    // Set the word to a lowercase value
    let word = gusText.val().toLowerCase();

    // Disable the input to the input field
    gusText.attr("disabled", true);
    hinText.attr("hidden", false);

    gusText.attr("class", "hideBox");

    // Checking if the word is equal to what we want
    if(word == allWords[usrIndex]){
        // Telling the user we won! And restarting..
        hinText.text("Correct! You win!");
        delay = window.setInterval(onStart, retryDelay);
    }
    // Ehh, Looks like you'll need to try again
    else{
        switch(usrTries){
        // This is the first mess up
        case 0:
            hinText.text("Incorrect! You have one more try.");
            delay = window.setInterval(updateDisplay, retryDelay);
            break;
        // This is the second mess up
        case 1:
            hinText.text("Sorry, your word was " + allWords[usrIndex]);
            delay = window.setInterval(onStart, 2500);
            break;
        }
    }
}
function updateDisplay(){
    // Resetting all values to defaults
    gusText.val("");
    gusText.attr("disabled", false);
    hinText.html("");
    hinText.attr("hidden", true);
    usrButton.attr("disabled", false);
    gusText.attr("class", "");
    if(delay != null){
        window.clearInterval(delay);
        delay = null;
    }
}
function checkBoldWord(wLetter, matches){
    var counts = 0;

    for(var i = 0; i < matches.length; i++){
        if(matches[i] == wLetter){counts += 1; break;}
    }

    return counts > 0;
}

function onStart(){
    // Getting words
    getWords();

    if(delay != null) {
        window.clearInterval(delay); 
        delay = null;
    }

    // Updating the new word and stuff
    hinButton.attr("disabled", false);
    usrIndex = Math.floor(Math.random() * allWords.length);
    scrText.text(scramble(allWords[usrIndex]));
    usrScrambled = scrText.text();
    usrButton.attr("disabled", false);

    // IMPORTANT FOR RESTARTING CURRENT GAME!
    // Change the amount of guesses back to !!ZERO!!
    usrTries = 0;

    // Updating display
    updateDisplay();
}
function onType(event){
    var typedWord = event.target.value.toLowerCase().split("");
    var wantedWord = allWords[usrIndex].toLowerCase().split("");
    var matches = [];

    for(var a = 0; a < typedWord.length; a++){
        for(var b = 0; b < wantedWord.length; b++){
            if(typedWord[a] == wantedWord[b]){
                matches.push(typedWord[a]);
            }
        }
    }

    var result = "";
    let wantedTag = "u";
    
    for(var i = 0; i < usrScrambled.length; i++){
        if(checkBoldWord(usrScrambled[i], matches)){
            result = result + "<" + wantedTag + ">" + usrScrambled[i] + "</" + wantedTag + ">";
            //matches.splice(result.d, 1);
        }
        else{
            result = result + usrScrambled[i];
        }
    }

    scrText.html(result);
}

// Events
window.addEventListener("load", function(){
    // The background
    let _bg = document.getElementById("bkgImg");

    // The rest of the stuff
    onStart();
});
usrField.on("submit", function(event){
    // Stop page from reloading on submit
    // REALLY REALLY ANNOYING..
    event.preventDefault();

    // Disable the button so the player can see
    // or learn what they got right or wrong.
    usrButton.attr("disabled", true);
    checkWord();

    // Add some tries!
    usrTries += 1;
});
gusText.on("input", onType);
hinButton.on("click", function(event){
    // Displaying a hint
    if(delay != null) {return;}

    // This is something cool!
    hinText.html("The first two letters of your word are <strong>" + allWords[usrIndex][0] + allWords[usrIndex][1] + "</strong>");
    hinText.attr("hidden", false);
    hinButton.attr("disabled", true);

    delay = window.setInterval(updateDisplay, retryDelay);
});
skpButton.on("click", function(){
    onStart();
})