package org.amc.dao;

import static org.junit.Assert.*;

import org.amc.DAOException;
import org.amc.game.chess.ChessBoard;
import org.amc.game.chess.ChessBoardUtilities;
import org.amc.game.chess.ChessGame;
import org.amc.game.chess.ChessGameFixture;
import org.amc.game.chess.ChessGamePlayer;
import org.amc.game.chess.Colour;
import org.amc.game.chess.ComparePlayers;
import org.amc.game.chess.HumanPlayer;
import org.amc.game.chess.Move;
import org.amc.game.chess.Player;
import org.amc.game.chess.SimpleInputParser;
import org.amc.game.chessserver.DatabaseSignUpFixture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.text.ParseException;

public class ChessGameDAOTest {

    private DatabaseSignUpFixture signUpfixture = new DatabaseSignUpFixture();
    private DAO<ChessGame> chessGameDAO;
    private ChessGameFixture cgFixture;
    private Player laura;
    private Player nobby;
    private DAO<Player> playerDAO;
    private ChessGame game;
    private SimpleInputParser mlParser;
    private int id;
    private static final String[] MOVES = {"A2A3", "A7A6"};
 
    @Before
    public void setUp() throws Exception {
        this.signUpfixture.setUp();
        this.mlParser = new SimpleInputParser();
        
        chessGameDAO = new DAO<>(ChessGame.class);
        cgFixture = new ChessGameFixture();
        
        playerDAO = new DAO<Player>(HumanPlayer.class);
        laura = playerDAO.findEntities("userName", "laura").get(0);
        nobby = playerDAO.findEntities("userName", "nobby").get(0);
        game = new ChessGame(cgFixture.getBoard(), new ChessGamePlayer(nobby, Colour.WHITE),
                        new ChessGamePlayer(laura, Colour.BLACK));
        cgFixture.getBoard().initialise();
        chessGameDAO.addEntity(game);
        id = game.getId();
    }

    @After
    public void tearDown() throws Exception {
        this.signUpfixture.tearDown();
    }

    @Test
    public void test() throws Exception {
        ChessGame game = chessGameDAO.getEntity(id);
        game.changePlayer();
        assertTrue(ComparePlayers.comparePlayers(game.getCurrentPlayer(), laura));
        assertTrue(ComparePlayers.comparePlayers(game.getBlackPlayer(), laura));
        assertTrue(ComparePlayers.comparePlayers(game.getWhitePlayer(), nobby));
        
    }
    
    @Test
    public void testChessBoardSaved() throws Exception {
        ChessGame game = chessGameDAO.getEntity(id);
        ChessBoard newBoard = new ChessBoard();
        newBoard.initialise();
        
        ChessBoardUtilities.compareBoards(newBoard, game.getChessBoard());
        
        for(String move : MOVES) {
            Move gameMove = mlParser.parseMoveString(move);
            game.move(game.getCurrentPlayer(), gameMove );
            game.changePlayer();
            newBoard.move(gameMove);
        }
        
        ChessBoardUtilities.compareBoards(newBoard, game.getChessBoard());
    }
}

@@ -54,37 +54,8 @@
 *;%0A%0A
-import org.amc.DAOException;%0A
 impo
@@ -88,16 +88,16 @@
 sBoard;%0A
+
 import o
@@ -176,129 +176,9 @@
 me;%0A
-import org.amc.game.chess.ChessGameFixture;%0Aimport org.amc.game.chess.ChessGamePlayer;%0Aimport org.amc.game.chess.Colour;
 %0A
+
 impo
@@ -219,47 +219,8 @@
 rs;%0A
-import org.amc.game.chess.HumanPlayer;%0A
 impo
@@ -458,42 +458,8 @@
 t;%0A%0A
-import java.text.ParseException;%0A%0A
 publ
@@ -611,48 +611,9 @@
 AO;%0A
-    private ChessGameFixture cgFixture;
 %0A
+
     
@@ -664,43 +664,8 @@
 by;%0A
-    private DAO%3CPlayer%3E playerDAO;%0A
     
@@ -962,71 +962,59 @@
     
-%0A        chessGameDAO = new DAO%3C%3E(ChessGame.class);%0A        cgF
+ServerChessGameTestDatabaseEntity serverChessGamesf
 ixtu
@@ -1014,32 +1014,38 @@
 esfixture = new 
+Server
 ChessGameFixture
@@ -1041,217 +1041,143 @@
 Game
-Fixture();%0A        %0A        playerDAO = new DAO%3CPlayer%3E(HumanPlayer.class);%0A        laura = playerDAO.findEntities(%22userName%22, %22laura%22).get(0);%0A        nobby = playerDAO.findEntities(%22userName%22, %22nobby%22).get(0
+TestDatabaseEntity();%0A        laura = serverChessGamesfixture.getBlackPlayer();%0A        nobby = serverChessGamesfixture.getWhitePlayer(
 );%0A 
@@ -1190,20 +1190,22 @@
  game = 
-new 
+server
 ChessGam
@@ -1209,12 +1209,10 @@
 Game
-(cgF
+sf
 ixtu
@@ -1221,206 +1221,82 @@
 .get
-Board(), new ChessGamePlayer(nobby, Colour.WHITE),%0A                        new ChessGamePlayer(laura, Colour.BLACK));%0A        cgFixture.getBoard().initialise();%0A        chessGameDAO.addEntity(game);
+ChessGame();%0A        chessGameDAO = new DAO%3C%3E(ChessGame.class);  %0A        
 %0A   

remove common elements into external fixture