package org.amc.game.chessserver;

import org.amc.game.chess.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.concurrent.ConcurrentMap;

import javax.servlet.ServletContext;

@Controller
@SessionAttributes({"GAME_UUID","PLAYER"})
@RequestMapping("/joinGame")
public class ServerJoinChessGameController {
    private ServletContext context;
    
    @RequestMapping(method=RequestMethod.POST)
    public void joinGame(@ModelAttribute("PLAYER") Player player,@RequestParam long gameUUID){
        
    }

    @Autowired
    void setServletContext(ServletContext context){
        this.context=context;
    }
    
    @ModelAttribute("GAMEMAP")
    @SuppressWarnings(value = "unchecked")
    private ConcurrentMap<Long, ServerChessGame> getGameMap() {
        synchronized (context) {
            return (ConcurrentMap<Long, ServerChessGame>) context.getAttribute(ServerConstants.GAMEMAP
                            .toString());
        }
    }
}

@@ -58,24 +58,56 @@
 ess.Player;%0A
+import org.apache.log4j.Logger;%0A
 import org.s
@@ -521,16 +521,69 @@
 ributes;
+%0Aimport org.springframework.web.servlet.ModelAndView;
 %0A%0Aimport
@@ -787,16 +787,112 @@
 oller %7B%0A
+    private static final Logger logger = Logger.getLogger(ServerJoinChessGameController.class);%0A
     priv
@@ -982,20 +982,28 @@
  public 
-void
+ModelAndView
  joinGam
@@ -1025,69 +1025,960 @@
 te(%22
-PLAYER%22) Player player,@RequestParam long gameUUID)%7B%0A        
+GAMEMAP%22) ConcurrentMap%3CLong, ServerChessGame%3E gameMap, @ModelAttribute(%22PLAYER%22) Player player,@RequestParam long gameUUID)%7B%0A        ModelAndView mav=new ModelAndView();%0A        %0A        ServerChessGame chessGame=gameMap.get(gameUUID);%0A        if(!chessGame.getCurrentStatus().equals(ServerChessGame.status.AWAITING_PLAYER))%7B%0A            mav.setViewName(%22forward:/app/chessgame/chessapplication%22);%0A            mav.getModel().put(%22ERRORS%22, %22Can't join chess game%22);%0A        %7Delse if(player.equals(chessGame.getPlayer()))%7B%0A            mav.setViewName(%22forward:/app/chessgame/chessapplication%22);%0A            mav.getModel().put(%22ERRORS%22, %22Player can't join a game they started%22);%0A        %7Delse%7B%0A            chessGame.addOpponent(player);%0A            mav.getModel().put(%22GAME_UUID%22, gameUUID);%0A            logger.info(String.format(%22Chess Game(%25d): has been started%22,gameUUID));%0A            mav.setViewName(%22chessGamePortal%22);%0A        %7D%0A        return mav;
 %0A   

implement first draft of joinGame method