package org.amc.game.chessserver;

import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.SESSION_ATTRIBUTES;

import org.amc.DAOException;
import org.amc.game.chess.ChessGamePlayer;
import org.amc.game.chess.ComparePlayers;
import org.amc.game.chess.IllegalMoveException;
import org.amc.game.chess.Move;
import org.amc.game.chess.Player;
import org.amc.game.chessserver.AbstractServerChessGame.ServerGameStatus;
import org.apache.log4j.Logger;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.util.Map;

@Controller
public class GameMoveStompController extends StompController {
    
    private static final Logger logger = Logger.getLogger(GameMoveStompController.class);
    
    static final String ERROR_MSG_GAME_OVER = "Error:Move on game(%d) which has finished";
    
    static final String ERROR_MSG_NOT_ENOUGH_PLAYERS = "Error:Move on game(%d) which hasn't got two players";

    @MessageMapping("/move/{gameUUID}")
    @SendToUser(value = "/queue/updates", broadcast = false)
    public void registerMove(Principal user,
                    @Header(SESSION_ATTRIBUTES) Map<String, Object> wsSession,
                    @DestinationVariable long gameUUID, @Payload String moveString) {

        logger.debug(String.format("USER(%s)'s move received for game:%d", user.getName(), gameUUID));

        Player player = (Player) wsSession.get("PLAYER");

        logger.debug("PLAYER:" + player);

        AbstractServerChessGame game = getServerChessGame(gameUUID);
        
        try {
            getServerChessGameDAO().getServerChessGame(gameUUID);
        } catch(DAOException de) {
            logger.error(de);
        }

        String message = "";

        if (AbstractServerChessGame.ServerGameStatus.IN_PROGRESS.equals(game.getCurrentStatus())) {
            message = moveChessPiece(game, player, moveString);
        } else if (AbstractServerChessGame.ServerGameStatus.AWAITING_PLAYER.equals(game.getCurrentStatus())) {
            message = String.format(ERROR_MSG_NOT_ENOUGH_PLAYERS, gameUUID);
        } else {
            message = String.format(ERROR_MSG_GAME_OVER, gameUUID);
        }

        logger.error(message);

        MessageType type = "".equals(message) ? MessageType.INFO : MessageType.ERROR;
        sendMessageToUser(user, message, type);
    }
    
    private String moveChessPiece(AbstractServerChessGame game, Player player, String moveString) {
        String message = "";

        try {
            ChessGamePlayer gamePlayer = ComparePlayers.comparePlayers(game.getPlayer(), player) ? game.getPlayer() : game.getOpponent();
            game.move(gamePlayer, new Move(moveString));
        } catch (IllegalMoveException | IllegalArgumentException e) {
            message = "Error:" + e.getMessage();
        }

        return message;
    }
    
    @MessageMapping("/oneViewMove/{gameUUID}")
    @SendToUser(value = "/queue/updates", broadcast = false)
    public void registerOneViewMoveMove(Principal user,
                    @DestinationVariable long gameUUID, @Payload String moveString) {

        AbstractServerChessGame game = getServerChessGame(gameUUID);
        
        if(!(game instanceof OneViewServerChessGame)) {
            logger.error("Can only move Chess Piece on an One View Chess game");
            return;
        }

        String message = "";
       
        if (ServerGameStatus.IN_PROGRESS.equals(game.getCurrentStatus())) {
            Player player = game.getChessGame().getCurrentPlayer();
            message = moveChessPiece(game, player, moveString);
           } else if (ServerGameStatus.AWAITING_PLAYER.equals(game.getCurrentStatus())) {
               message = String.format(ERROR_MSG_NOT_ENOUGH_PLAYERS, gameUUID);
               logger.error(message);
           } else {
               message = String.format(ERROR_MSG_GAME_OVER, gameUUID);
               logger.error(message);
           }
        MessageType type = "".equals(message) ? MessageType.INFO : MessageType.ERROR;
        sendMessageToUser(user, message, type);
         
    }
}

@@ -128,37 +128,8 @@
 S;%0A%0A
-import org.amc.DAOException;%0A
 impo
@@ -1850,172 +1850,8 @@
 ID);
-%0A        %0A        try %7B%0A            getServerChessGameDAO().getServerChessGame(gameUUID);%0A        %7D catch(DAOException de) %7B%0A            logger.error(de);%0A        %7D
 %0A%0A  
@@ -2333,37 +2333,36 @@
 %0A        logger.
-error
+info
 (message);%0A%0A    

remove dead code from GameMoveStompController#registerMove