//***************** Scene Object - Initialize When Scene Starts Event *************//
// this function takes an array
// shuffles it randomly and returns it
$this.shuffle = function(arr){
    arr.sort(() => random(-10, 5));
    return arr;
};

$this.words = [
  "analog",
  "array",
  "binary",
  "browser",
  "computer",
  "condition",
  "conditional",
  "database",
  "download",
  "function",
  "group",
  "hardware",
  "interface",
  "javascript",
  "keywords",
  "layers",
  "megabyte",
  "network",
  "object",
  "program",
  "random",
  "routine",
  "scene",
  "script",
  "software",
  "sprite",
  "string",
  "syntax",
  "toggle",
  "variable",
  "visible",
  "website",
  "window",
];

$this.getNewWord = function() {
    // clear out the guess word
    guess.text("");
    message.text("Unscramble the word: Click here for a new one.");
    
    // select a word at a random
    $this.selectedWord = $this.words[random($this.words.length - 1)];
    
    // create an empty array for the ordered letters
    var orderedLetters = [];
    
    // put each letter in the array
    for (var l = 0; l < $this.selectedWord.length; l++) {
        orderedLetters.push($this.selectedWord[l]);
    }
    
    // shuffle the letters in a new array
    var shuffledLetters = $this.shuffle(orderedLetters);
    // updated the shuffledLabel text
    shuffledLabel.text(shuffledLetters);
};

$this.checkGuess = function() {
    // only check if the guess is the correct length
    var originalWord = $this.selectedWord;
    var guessedWord = guess.text();
    if(originalWord.length === guessedWord.length) {
        message.text("Good job! Click here to play again!");
        // loop through letter in the guess
        for (var l = 0; l < originalWord.length; l++) {
            if (originalWord[l] != guessedWord[l]) {
                message.text("Wrong! Click here to try again!");
            }
        }
    }
}

if ($this.scene.state() == "PLAY") {
    $this.getNewWord();
}

//********************* resetButton Object - Mouse Click Event ********************//
guess.text("");

//******************** submitButton Object - Mouse Click Event ********************//
$this.scene.checkGuess();

//******************** message Object - Mouse Click Event ************************//
$this.scene.getNewWord();