package org.amc.game.chessserver;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.amc.game.chess.HumanPlayer;
import org.amc.game.chess.Player;
import org.amc.game.chessserver.ServerChessGame.ServerGameStatus;
import org.amc.game.chessserver.ServerChessGameFactory.GameType;
import org.amc.game.chessserver.observers.ObserverFactoryChain;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ServerChessGameSerilaiserTest {
	private GsonBuilder builder;
	private ServerChessGameFactory scgFactory;
	private final long gameUID = 2345L;
	Player whitePlayer;
	Player blackPlayer;
	
	@Before
	public void setUp() throws Exception {
		builder = new GsonBuilder();
		builder.registerTypeHierarchyAdapter(ServerChessGame.class, 
				new GameTableController.ServerChessGameSerialiser());
		builder.setPrettyPrinting();
		builder.serializeNulls();
		
		scgFactory = new ServerChessGameFactory();
		ObserverFactoryChain chain = mock(ObserverFactoryChain.class);
		scgFactory.setObserverFactoryChain(chain);
		
		whitePlayer = new HumanPlayer("Sarah");
		whitePlayer.setUserName("username_sarah");
		
		blackPlayer = new HumanPlayer("Robert");
		blackPlayer.setUserName("username_bobby");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() {
		ServerChessGame scgGame = scgFactory.getServerChessGame(GameType.LOCAL_GAME, gameUID, whitePlayer);
		scgGame.addOpponent(blackPlayer);
		Gson gson = builder.create();
		String output = gson.toJson(scgGame);
		ServerChessGameInfo info = gson.fromJson(output, ServerChessGameInfo.class);
		assertEquals(gameUID, info.getUid());
		assertEquals(whitePlayer.getUserName(), info.player);
		assertEquals(blackPlayer.getUserName(), info.opponent);
		assertEquals(scgGame.getCurrentStatus(), info.currentStatus);
	}
	
	@Test
	public void testNull() {
		ServerChessGame scgGame = scgFactory.getServerChessGame(GameType.LOCAL_GAME, gameUID, whitePlayer);
		Gson gson = builder.create();
		String output = gson.toJson(scgGame);
		ServerChessGameInfo info = gson.fromJson(output, ServerChessGameInfo.class);
		assertEquals(gameUID, info.getUid());
		//assertNull(whitePlayer.getUserName());
		assertNull(info.opponent);
		assertEquals(scgGame.getCurrentStatus(), info.currentStatus);
	}
	
	private static class ServerChessGameInfo {
		private long uid;
		private ServerGameStatus currentStatus;
		private String player;
		private String opponent;
		public long getUid() {
			return uid;
		}
		public void setUid(long uid) {
			this.uid = uid;
		}
		public ServerGameStatus getCurrentStatus() {
			return currentStatus;
		}
		public void setCurrentStatus(ServerGameStatus currentStatus) {
			this.currentStatus = currentStatus;
		}
		public String getPlayer() {
			return player;
		}
		public void setPlayer(String player) {
			this.player = player;
		}
		public String getOpponent() {
			return opponent;
		}
		public void setOpponent(String opponent) {
			this.opponent = opponent;
		}
		
	}

}

@@ -1770,22 +1770,27 @@
 ), info.
-p
+getP
 layer
+()
 );%0A%09%09ass
@@ -1827,32 +1827,37 @@
 ame(), info.
-o
+getO
 pponent
+()
 );%0A%09%09assertE
@@ -1887,33 +1887,36 @@
 tStatus(), info.
-c
+getC
 urrentStatus);%0A%09
@@ -1903,32 +1903,34 @@
 getCurrentStatus
+()
 );%0A%09%7D%0A%09%0A%09@Test%0A%09
@@ -2253,10 +2253,8 @@
 ;%0A%09%09
-//
 asse
@@ -2264,62 +2264,26 @@
 ull(
-whitePlayer.getUserName());%0A%09%09assertNull(info.o
+info.getO
 pponent
+()
 );%0A%09
@@ -2329,17 +2329,20 @@
 ), info.
-c
+getC
 urrentSt
@@ -2345,16 +2345,18 @@
 ntStatus
+()
 );%0A%09%7D%0A%09%0A
@@ -2513,16 +2513,19 @@
 ponent;%0A
+%09%09%0A
 %09%09public
@@ -2564,64 +2564,8 @@
 %09%09%7D%0A
-%09%09public void setUid(long uid) %7B%0A%09%09%09this.uid = uid;%0A%09%09%7D%0A
 %09%09pu
@@ -2640,374 +2640,117 @@
 %09%09%7D%0A
-%09%09public void setCurrentStatus(ServerGameStatus currentStatus) %7B%0A%09%09%09this.currentStatus = currentStatus;%0A%09%09%7D%0A%09%09public String getPlayer() %7B%0A%09%09%09return player;%0A%09%09%7D%0A%09%09public void setPlayer(String player) %7B%0A%09%09%09this.player = player;%0A%09%09%7D%0A%09%09public String getOpponent() %7B%0A%09%09%09return opponent;%0A%09%09%7D%0A%09%09public void setOpponent(String opponent) %7B%0A%09%09%09this.opponent = opponent;%0A%09%09%7D%0A%09%09
+%0A%09%09public String getPlayer() %7B%0A%09%09%09return player;%0A%09%09%7D%0A%0A%09%09public String getOpponent() %7B%0A%09%09%09return opponent;%0A%09%09%7D
 %0A%09%7D%0A

remove unused methods form ServerChessGameInfo class