package orwell.proxy;

import com.google.protobuf.InvalidProtocolBufferException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import orwell.messages.ServerGame;
import orwell.proxy.robot.RobotGameStateVisitor;

/**
 * Created by Michaël Ludmann on 6/4/15.
 */
public class GameState {
    private final static Logger logback = LoggerFactory.getLogger(GameState.class);
    private ServerGame.GameState serverGameGameState;

    public GameState(final byte[] gameStateMessage) {
        try {
            this.serverGameGameState = ServerGame.GameState.parseFrom(gameStateMessage);
        } catch (final InvalidProtocolBufferException e) {
            logback.error("setGameState protobuf exception: " + e.getMessage());
        }
    }

    public EnumGameState getEnumGameState() {
        if (hasParseError())
            return EnumGameState.UNDEFINED;
        if (isGameOnGoing())
            return EnumGameState.PLAYING;
        if (isGameFinished())
            return EnumGameState.FINISHED;
        return EnumGameState.WAITING_TO_START;
    }

    private boolean hasParseError() {
        return null == serverGameGameState;
    }

    protected boolean isGameOnGoing() {
        return (null != serverGameGameState &&
                serverGameGameState.getPlaying() &&
                0 < serverGameGameState.getSeconds());
    }

    protected boolean isGameFinished() {
        return (null != serverGameGameState &&
                0 == serverGameGameState.getSeconds() && !isGameOnGoing());
    }

    protected String getWinningTeam() {
        if (!isGameFinished()) {
            return null;
        }
        return serverGameGameState.getWinner();
    }

    public RobotGameStateVisitor getRobotGameStateVisitor() {
        if (null == serverGameGameState) {
            return null;
        }
        return new RobotGameStateVisitor(getEnumGameState(), getWinningTeam());
    }
}

@@ -587,16 +587,207 @@
 ssage);%0A
+            if (null != serverGameGameState && serverGameGameState.hasWinner()) %7B%0A                logback.debug(%22%3E%3E%3E%3E%3E%3E%3E We have a winner: %22 + serverGameGameState.getWinner());%0A            %7D%0A
         
@@ -985,81 +985,8 @@
 ) %7B%0A
-        if (hasParseError())%0A            return EnumGameState.UNDEFINED;%0A
     
@@ -1125,16 +1125,56 @@
 NISHED;%0A
+        if (isGameWaitingToStart())%0A    
         
@@ -1220,22 +1220,158 @@
     
-%7D%0A%0A    priva
+    else // Parsing error for instance%0A            return EnumGameState.UNDEFINED;%0A    %7D%0A%0A    /**%0A     * @return playing == true%0A     */%0A    protec
 te
+d
  boo
@@ -1379,21 +1379,21 @@
 ean 
-hasParseError
+isGameOnGoing
 () %7B
@@ -1408,22 +1408,23 @@
  return 
+(
 null 
-=
+!
 = server
@@ -1436,24 +1436,139 @@
 ameState
-;%0A    %7D%0A
+ &&%0A                serverGameGameState.getPlaying());%0A    %7D%0A%0A    /**%0A     * @return playing == false && has winner%0A     */
 %0A    pro
@@ -1580,39 +1580,40 @@
 d boolean isGame
-OnGoing
+Finished
 () %7B%0A        ret
@@ -1656,32 +1656,33 @@
                 
+!
 serverGameGameSt
@@ -1720,12 +1720,8 @@
     
- 0 %3C
  ser
@@ -1737,26 +1737,25 @@
 meState.
-getSeconds
+hasWinner
 ());%0A   
@@ -1758,33 +1758,96 @@
 %0A    %7D%0A%0A    
-protec
+/**%0A     * @return playing == false && has no winner%0A     */%0A    priva
 te
-d
  boolean isG
@@ -1845,32 +1845,38 @@
 olean isGame
-Finished
+WaitingToStart
 () %7B%0A       
@@ -1935,13 +1935,9 @@
     
-0 == 
+!
 serv
@@ -1959,35 +1959,67 @@
 .get
-Seconds() && !isGameOnGoing
+Playing() &&%0A                !serverGameGameState.hasWinner
 ());

Simplify game states PLAYING, FINISHED, WAITING_TO_START