package org.academiadecodigo.bootcamp11.guessinggame;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */
public class Game {
    private int maxNumber;
    private Player[] players;

    public Game(int max, Player[] players) {
        this.maxNumber = max;
        this.players = players;
    }

    public int chooseNumber(int max) {
        return Utils.giveNumber(max);
    }

    public void start() {
        // First, we need a magic number which we'll compare
        // with the players' guesses.
        int magicNumber = chooseNumber(this.maxNumber);

        // Next, we initialize the room where the players are playing.
        Room room = new Room();

        boolean didSomeoneWin = false;
        // In case no one wins, the game is the default winner
        Player whoWon = new Player("the Game");

        // Next we start the guessing loop.
        System.out.println("Gaaaaaaaaaaame START!");

        System.out.println("(GM number is " + magicNumber + ", btw)");

        // (we're keeping track of how many rounds we go through)
        int round = 1;

        // this will be reevaluated in each loop
        // if everyone reached the max number of tries, game over.
        boolean canAnyoneStillPlay = canAnyoneStillPlay();

        while (!didSomeoneWin && canAnyoneStillPlay) {
            System.out.println("\nRound " + round + ": ");
            for (Player player : this.players) {
                if (!player.canPlay()) {
                    continue;
                }

                int playerGuess = player.guessNumber(this.maxNumber, room);
                if (playerGuess == magicNumber) {
                    System.out.println("\tGM: DING DING DING");
                    didSomeoneWin = true;
                    whoWon = player;
                    break;
                } else {
                    System.out.println("\tGM: Nope.");
                }
            }
            if (!didSomeoneWin) {
                System.out.println("Everyone failed round " + round + ".");
                System.out.println("Numbers tried so far:");
                System.out.println(room.toString());
            }
            round++;
            canAnyoneStillPlay = canAnyoneStillPlay();
            if (!canAnyoneStillPlay) {
                System.out.println("\nNo one can play anymore :(");
            }
        }

        System.out.println("\nThe grand winner was " +
                whoWon.getName() + "!!!");

        shameLosers(whoWon);
    }

    private boolean canAnyoneStillPlay() {
        for (Player player : this.players) {
            if (player.canPlay()) {
                return true;
            }
        }
        return false;
    }

    private void shameLosers(Player winner) {
        System.out.println("\nHall of Shame: ");

        for (Player loser : this.players) {
            String loserName = loser.getName();
            if (loserName.equals(winner.getName())) {
                continue;
            }
            System.out.println(loserName);
        }
    }
}
