/** 
This activity uses conditionals to provide instructions based on current state of the game. Loops are used to iterate through tiles.
The player must roll the dice and select the tiles to reduce the total down to zero. The game is won when the total is zero
and there are no tiles left. 
 */

//***********************************Scene - Initialize When Scene Starts Event*********************************************//
// Tile creation
if($this.scene.state() == "PLAY") {
    // create an array to store available tiles
    var availableTiles = [];
    // start with a target amount of 0
    var targetAmount = 0;
    // create the cloned tiles
    for (var t = 1; t < 10; t++) {
        var clonedTile = numberedTile.clone();
        var tileNumber = clonedTile.findName("number_" + t);
        // update the text on the clone's label object
        tileNumber.text(t);
        // assign the number to the tile
        clonedTile.number = t;
        // add the tile to the availableTiles array
        availableTiles.push(clonedTile); 
        // place the cloned tile in the scene by setting its x value.
        clonedTile.x(20 + 85 * (t - 1));
    }
    // remove the original tile
    numberedTile.remove();
}

// run this function when a dice is clicked
$this.rollDice = function()  {
    // only allow rolls if targetAmount = 0
    if(targetAmount === 0) {
        // randomly roll the dic
        var die1Value = random(1,6);
        var die2Value = random(1,6);
        // update the sprite frames
        die1.frameIndex(die1Value);
        die2.frameIndex(die2Value);
        // set the target amount and update UI
        targetAmount = die1Value + die2Value;
        totalText.text(targetAmount);
        // update message text
        message.text("Click the tiles to subtract from the total.");
        // check for valid moves after every roll
        $this.checkForValidMoves();
        
    }
};

// run this function when a tile is clicked
$this.processTile = function(clickedTile) {
    // make sure the tile's number is smaller than targetAmount
    if(clickedTile.number <= targetAmount) {
        // subtract clicked number from targetAmount
        targetAmount -= clickedTile.number;
        // update the text
        totalText.text(targetAmount);
        // hide clicked number tile
        clickedTile.visible(false);
        // loop through the availableTiles array
        for (var i = 0; i < availableTiles.length; i++) {
            // find the clickedTile in the availableTiles array
            if (availableTiles[i] == clickedTile) {
                // and remove it with splice function
                availableTiles.splice(i, 1);
            }
        }
        if (availableTiles.length === 0) {
            $this.checkForEndOfGame();
        } else {
            // if there are tiles, check for valid moves 
            // check for valid moves after every clicked tile
            $this.checkForValidMoves();
        }
       
    }
};

$this.checkForValidMoves = function() {
   if(targetAmount > 0) {
       // find the smallest available tile
       var smallestTile = availableTiles[0];
       if (smallestTile.number > targetAmount) {
           message.text("There are no valid moves. Play again!");
       }
   } else { 
       message.text("Click the dice to roll again!")
   } 
};

$this.checkForEndOfGame = function() {
    if (targetAmount === 0) {
        message.text("Good job! You win!");
    } else {
        message.text("Game over. Try again!");
    }
}

//*********************************die 1 and die 2 Objects - Mouse Click Event*********************************************//
$this.scene.rollDice();

//****************************************numberedTile Object - Mouse Click***********************************************//
$this.scene.processTile($this)

