package logic;

import beans.Deminor;
import beans.InvalidCoordinateException;
import sun.plugin.dom.exception.InvalidStateException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author MACHIZAUD Andréa
 * @version 1.0 06/04/11
 */
@WebServlet(name = "Servlet DIG", urlPatterns = {"/DIG"})
public class ServletDIG extends HttpServlet {

    private static final Logger logger =
            Logger.getLogger(ServletDIG.class.getCanonicalName());

    public static final int DIG_ACTION = 1;
    public static final int FLAG_ACTION = 2;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Be ready for anything...
        try {
            //Fetch parameters
            int rowIndex = Integer.parseInt(request.getParameter("row-input")) - 1;
            int columnIndex = Integer.parseInt(request.getParameter("column-input")) - 1;
            int action = Integer.parseInt(request.getParameter("action-input"));

            Deminor deminor = (Deminor) request.getServletContext().getAttribute("deminor");

            switch (action) {
                case DIG_ACTION:
                    switch (deminor.dig(rowIndex, columnIndex)) {
                        case Deminor.GAME_EVENT_LOSE:
                            request.getSession().setAttribute("message","You lose...");
                            break;
                        case Deminor.GAME_EVENT_CONTINUE:
                            break;
                        case Deminor.GAME_EVENT_WIN:
                            throw new IllegalStateException("A win can't happen after a dig action");
                    }
                    break;
                case FLAG_ACTION:
                    switch (deminor.flag(rowIndex, columnIndex)) {
                        case Deminor.GAME_EVENT_WIN:
                            request.getSession().setAttribute("message","You won ! Well done !");
                            break;
                        case Deminor.GAME_EVENT_LOSE:
                            throw new IllegalStateException("A lose can't happen after a flag action");
                        case Deminor.GAME_EVENT_CONTINUE:
                            break;
                    }
                    break;
                default:
                    throw new InvalidActionException("Action undefined");
            }
            //Handle wrong inputs
        } catch (NumberFormatException e) {
            request.getSession().setAttribute("errorMessage", "Case non valide");
        } catch (InvalidActionException e) {
            request.getSession().setAttribute("errorMessage", e.getMessage());
        } catch (InvalidCoordinateException e) {
            request.getSession().setAttribute("errorMessage", e.getMessage());
        }
        //Go back to home
        response.sendRedirect(request.getContextPath());
    }
}
