/**
 * Copyright 2014 Connor Anderson

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package game.objects;

import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Game {
    private static int numOfGames = 0;
    private final PlayerList playerList;
    private final Board board;
    private final GameState gameState;
    private final Phase phase;
    private long lastModified;
    private final String gameName;
    private final int gameID;

    public Game(String gameName) {
        this.gameName = gameName;
        this.playerList = new PlayerList();
        this.board = new Board();
        this.gameState = new GameState(board);
        this.phase = gameState.getPhase();
        this.lastModified = System.currentTimeMillis();
        this.gameID = numOfGames++;
    }

    public PlayerList getPlayerList() {
        return playerList;
    }

    public GameState getGameState() {
        return gameState;
    }

    public Board getBoard() {
        return board;
    }

    public void endPhase() {
        switch (phase.getPhase()) {
            case Phase.SETUP:
                gameState.startGame();
                break;
            case Phase.MOVE:
                endTurn();
                break;
            default:
                break;
        }
        phase.nextPhase();
    }

    public void endTurn() {
        nextPlayer();
        gameState.nextTurn();
    }

    public void nextPlayer() {
        gameState.setCurrentPlayer((gameState.getCurrentPlayer() + 1) % playerList.getNumberOfPlayers());
//        boolean loser = true;
//        HashMap<String, Country> countries = board.getAllCountries();
//        for (String key : countries.keySet()) {
//            if (countries.get(key).getOwner() == gameState.getCurrentPlayer()) {
//                loser = false;
//            }
//        }
//        if (loser) {
//            nextPlayer();
//        }
    }

    public Player getCurrentPlayerObject() {
        return (Player) playerList.getPlayerById(gameState.getCurrentPlayer());
    }

    public JSONObject getGameJSON() throws JSONException {
        JSONObject json = new JSONObject();
        json.put("Players", playerList.getPlayersJSON());
        json.put("GameState", gameState.getGameStateJSON());
        json.put("Board", board.getBoardJSON());
        return new JSONObject().put("Game", json);
    }

    public long getLastModified() {
        return lastModified;
    }

    public void pushChanges() {
        this.lastModified = System.currentTimeMillis();
    }
    
    public String getGameName() {
        return gameName;
    }
    
    public int getGameID() {
        return gameID;
    }
    
    public Set<HttpSession> getSessions() {
        Set<HttpSession> sessions = new HashSet<>();
        Map<String, Player> players = playerList.getPlayerHashMap();
        for (String key:players.keySet()) {
            sessions.add(players.get(key).getSession());
        }
        return sessions;
    }
    
    @Override
    public String toString() {
        return "Game ID: " + gameID + ", Name: \"" + gameName + "\" with " + playerList.getNumberOfPlayers() + " players.";
    }
}
@@ -31,17 +31,21 @@
 nderson%0A
-%0A
+ *%0A *
  License
@@ -100,17 +100,16 @@
 cense%22);
-%0A
  you may
@@ -112,16 +112,19 @@
  may not
+%0A *
  use thi
@@ -168,17 +168,16 @@
 License.
-%0A
  You may
@@ -193,16 +193,19 @@
  copy of
+%0A *
  the Lic
@@ -212,17 +212,21 @@
 ense at%0A
-%0A
+ *%0A *
  http://
@@ -261,17 +261,21 @@
 NSE-2.0%0A
-%0A
+ *%0A *
  Unless 
@@ -335,16 +335,18 @@
 oftware%0A
+ *
  distrib
@@ -407,17 +407,16 @@
 SIS,
-%0A
  WITHOUT
  WAR
@@ -411,16 +411,19 @@
  WITHOUT
+%0A *
  WARRANT
@@ -483,17 +483,16 @@
 ied.
-%0A
  See the
  Lic
@@ -483,24 +483,27 @@
 ied. See the
+%0A *
  License for
@@ -550,17 +550,16 @@
 ions and
-%0A
  limitat
@@ -564,24 +564,27 @@
 ations under
+%0A *
  the License
@@ -612,16 +612,64 @@
 jects;%0A%0A
+import game.objects.exceptions.TroopsException;%0A
 import o
@@ -828,16 +828,81 @@
 til.Set;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Apublic
@@ -915,16 +915,17 @@
  Game %7B%0A
+%0A
     priv
@@ -2282,16 +2282,256 @@
 ers());%0A
+        try %7B%0A            playerList.getPlayerById(gameState.getCurrentPlayer()).setNumberOfTroopsToDeploy(3);%0A        %7D catch (TroopsException ex) %7B%0A            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D%0A
 //      
@@ -3509,36 +3509,32 @@
 Millis();%0A    %7D%0A
-    
 %0A    public Stri
@@ -3575,36 +3575,32 @@
 gameName;%0A    %7D%0A
-    
 %0A    public int 
@@ -3638,28 +3638,24 @@
 meID;%0A    %7D%0A
-    
 %0A    public 
@@ -3832,17 +3832,19 @@
 ring key
-:
+ : 
 players.
@@ -3953,20 +3953,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov
@@ -4130,8 +4130,9 @@
 %0A    %7D%0A%7D
+%0A

Reset troops to deploy