package sfs.cluster.lobby.handlers.cluster;

import it.gotoandplay.smartfoxserver.SmartFoxServer;
import it.gotoandplay.smartfoxserver.data.Room;
import it.gotoandplay.smartfoxserver.data.User;
import it.gotoandplay.smartfoxserver.data.Zone;
import it.gotoandplay.smartfoxserver.extensions.AbstractExtension;
import it.gotoandplay.smartfoxserver.extensions.ExtensionHelper;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import sfs.cluster.lobby.ClusterGameServerExtension;
import sfs.cluster.lobby.DataStore;
import sfs.cluster.lobby.data.ClusterEvent;
import sfs.cluster.lobby.data.ClusterRoom;
import sfs.cluster.lobby.data.RoomReservation;

/**
 * This event is fired when a request for a new game is received.
 * The room is created and a Reservation is created for the user.
 */
public class OnCreateGame implements IClusterEvtHandler
{
	ClusterGameServerExtension parentExtension;
	Logger log;
	Zone zone;
	
	public OnCreateGame(AbstractExtension parentExtension)
    {
		this.parentExtension = (ClusterGameServerExtension) parentExtension;	
	    this.zone = SmartFoxServer.getInstance().getZone(parentExtension.getOwnerZone());
	    log = SmartFoxServer.log;
    }
	
	// Handle the event
	public void handleClusterEvent(ClusterEvent event) throws Exception
	{
		String owner = (String) event.getParams().get("owner");
		String gameName = (String) event.getParams().get("gameName");
		String gameType = (String) event.getParams().get("gameType");
		
		int expiry = Integer.parseInt((String) event.getParams().get("expiry"));
	    
		//::: Create the new room and attach game extension :::::::::::::::::::::::::::::::::::::::::::
	    HashMap<Object, Object> params = new HashMap<Object, Object>();
	    params.put("name", gameName);
	    params.put("pwd", "");
	    params.put("maxU", "2");
	    params.put("maxS", "0");
	    params.put("isGame", "true");
	    params.put("xtName", gameType);
	    params.put("xtClass", gameType.equalsIgnoreCase("tris") ? "sfsTris.as" : "mazeGame.as");
	    
	    Room theRoom = ExtensionHelper.instance().createRoom(	parentExtension.getCurrentZone(), 
	    														params, 
	    														null, 
	    														false, 
	    														false
	    													);
	    
	    // Add the room cluster-wide
	    ClusterRoom newClusterRoom = new ClusterRoom(gameName);
	    newClusterRoom.setMaxPlayers(2);
	    newClusterRoom.setGameType(gameType);
	    newClusterRoom.setLocalID(theRoom.getId());

	    parentExtension.addRoom(newClusterRoom);
	    
	    // Fire an event about the new room just created
	    ClusterEvent roomEvent = new ClusterEvent(ClusterEvent.ON_ROOM_CREATED);
	    roomEvent.getParams().put("gameName", gameName);
	    roomEvent.getParams().put("gameType", gameType);
	    roomEvent.getParams().put("debug", "createGame");
	    roomEvent.getParams().put("owner", owner);
	    
	    // Dispatch to all lobby nodes
	    List<String> nodeList = DataStore.instance().getLobbyNodes();
	    
	    DataStore.instance().broadcastClusterEvent(roomEvent, nodeList);
	    		
	    // Let's see if the user is already logged in
	    User u = parentExtension.getCurrentZone().getUserByName(owner);
	    
	    // user exist, log him into the room
	    if ( u != null )
	    {
	    	int oldRoomId = u.getRoomsConnected()[0];
	    	ExtensionHelper.instance().joinRoom(u, oldRoomId, theRoom.getId(), true, "", false, true);
	    }
	    
	    // user doesn't exist, let's create a reservation. Room Expiry = 30 seconds
	    else
	    {
	    	RoomReservation reservation = new RoomReservation(owner, theRoom.getId(), expiry);
	    	parentExtension.addReservation(reservation);
	    	log.info("Reservation created: " + reservation);
	    }
	    
	}
}
