package com.philihp.weblabora.action;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.philihp.weblabora.form.CreateGameForm;
import com.philihp.weblabora.jpa.Game;
import com.philihp.weblabora.jpa.State;
import com.philihp.weblabora.jpa.User;
import com.philihp.weblabora.model.Color;
import com.philihp.weblabora.model.GameCountry;
import com.philihp.weblabora.model.GameLength;
import com.philihp.weblabora.model.GamePlayers;

public class CreateGame extends BaseAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		CreateGameForm form = (CreateGameForm)actionForm;
		EntityManager em = (EntityManager)request.getAttribute("em");
		
		Game game = new Game();
		game.getPlayer1().setUser(user);
		game.getPlayer1().setColor(Color.RED.toString());
		game.getPlayer1().setMove("");
		game.getPlayer2().setColor(Color.GREEN.toString());
		game.getPlayer2().setMove("");
		game.getPlayer3().setColor(Color.BLUE.toString());
		game.getPlayer3().setMove("");
		game.getPlayer4().setColor(Color.WHITE.toString());
		game.getPlayer4().setMove("");
		game.setCountry(form.getCountry());
		game.setLength(form.getLength());
		game.setPlayers(form.getPlayers());
		user.setActiveGame(game);
		game.setState(em.find(State.class, startState(form.getPlayers(), form.getLength(), form.getCountry())));
		em.persist(game);

		return mapping.findForward("root");
	}
	
	private static int startState(Integer players, String length, String country) {
		GamePlayers gamePlayers = GamePlayers.valueOf(players);
		GameLength gameLength = GameLength.valueOf(length);
		GameCountry gameCountry = GameCountry.valueOf(country);
		return 1+gamePlayers.ordinal()
				+(gameLength.ordinal()<<2) 
				+(gameCountry.ordinal()<<3);
		//no project is complete until you use bit-shift operators
	}

}

@@ -1029,16 +1029,100 @@
 );%0D%0A%09%09%0D%0A
+%09%09State state = new State();%0D%0A%09%09state.setExplorer(user);%0D%0A%09%09em.persist(state);%0D%0A%09%09%0D%0A
 %09%09Game g
@@ -1693,585 +1693,77 @@
 ate(
-em.find(State.class, startState(form.getPlayers(), form.getLength(), form.getCountry())));%0D%0A%09%09em.persist(game);%0D%0A%0D%0A%09%09return mapping.findForward(%22root%22);%0D%0A%09%7D%0D%0A%09%0D%0A%09private static int startState(Integer players, String length, String country) %7B%0D%0A%09%09GamePlayers gamePlayers = GamePlayers.valueOf(players);%0D%0A%09%09GameLength gameLength = GameLength.valueOf(length);%0D%0A%09%09GameCountry gameCountry = GameCountry.valueOf(country);%0D%0A%09%09return 1+gamePlayers.ordinal()%0D%0A%09%09%09%09+(gameLength.ordinal()%3C%3C2) %0D%0A%09%09%09%09+(gameCountry.ordinal()%3C%3C3);%0D%0A%09%09//no project is complete until you use bit-shift operators
+state);%0D%0A%09%09em.persist(game);%0D%0A%0D%0A%09%09return mapping.findForward(%22root%22);
 %0D%0A%09%7D

create game now creates a null start state, rather than having everyone start from the same root state.