package com.aot.engine.api;

import com.aot.engine.GameFactory;
import com.aot.engine.Match;
import com.aot.engine.board.Board;
import com.aot.engine.cards.Deck;
import com.aot.engine.cards.movements.MovementsCard;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

/**
 * <b>Rest servlet that returns the squares we can play.</b>
 *
 * Expect parameters: x, y, card, color.
 *
 * @author jenselme
 */
public abstract class GameApi {

    protected static final Response.ResponseBuilder BAD_REQUEST_BUILDER = Response.status(Response.Status.BAD_REQUEST);

    /**
     * The map of all parameters passed in the URL.
     */
    protected Map<String, String> parameters;
    /**
     * The GameFactory of the current game.
     */
    protected GameFactory gameFactory;
    /**
     * The Match the user is playing.
     */
    protected Match match;
    /**
     * The Board he is playing on.
     */
    protected Board board;
    /**
     * His currentPlayerDeck of cards.
     */
    protected Deck currentPlayerDeck;
    /**
     * The last card played.
     */
    protected MovementsCard playableCard;

    /**
     * The request done to the servlet.
     */
    @Context
    HttpServletRequest request;

    public GameApi() {
        parameters = new HashMap<>();
    }

    /**
     * Init the object's attributes.
     */
    protected void init() {
        match = gameFactory.getMatch();
        currentPlayerDeck = match.getActivePlayer().getDeck();
    }

    /**
     * Return the proper answer to the request, ie the JSON answer or a BAD_REQUEST.
     *
     * @return A JSON or BAD_REQUEST.
     */
    protected abstract String getResponse();

    /**
     * Create the bad Response object based on a message.
     *
     * @param message The message to send to the client.
     * @return A JSON object containing the error message.
     */
    protected String buildBadResponse(String message) {
        return "{\"error\": \"" + message + "\"}";
    }

    /**
     * Get the GameFactory from the session and then continue with parameters check.
     *
     * @return
     */
    protected String getGameFactoryResponse() {
        gameFactory = (GameFactory) request.getSession().getAttribute("gameFactory");
        if (gameFactory == null) {
            return buildBadResponse("No match is running");
        }

        init();
        return checkParametersAndGetResponse();
    }

    /**
     * Check that the passed parameters are consistent and returns a Response (400 if there is a
     * problem) or goes on.
     *
     * @return A Response object.
     */
    protected abstract String checkParametersAndGetResponse();

}

@@ -319,26 +319,134 @@
 vax.
-ws.rs.core.Context
+servlet.http.HttpSession;%0Aimport javax.websocket.EndpointConfig;%0Aimport javax.websocket.OnOpen;%0Aimport javax.websocket.Session
 ;%0Aim
@@ -1324,108 +1324,78 @@
 rd;%0A
-%0A
     
-/**%0A     * The request done to the servlet.%0A     */%0A    @Context%0A    HttpServletRequest request
+protected Session wsSession;%0A    protected HttpSession httpSession
 ;%0A%0A 
@@ -2366,28 +2366,19 @@
 ry) 
-request.get
+http
 Session
-()
 .get
@@ -2754,32 +2754,32 @@
 object.%0A     */%0A
-
     protected ab
@@ -2826,10 +2826,248 @@
 nse();%0A%0A
+    @OnOpen%0A    protected void open(Session session, EndpointConfig config) %7B%0A        this.wsSession = session;%0A        this.httpSession = (HttpSession) config.getUserProperties()%0A                .get(HttpSession.class.getName());%0A    %7D%0A%0A
 %7D%0A

Access http session from the websocket.