/*******************************************************************************************
 * 
 * TITLE: 		SFS2X Space Race
 * VERSION:		1.0
 * RELEASE:		2011-03-15
 * COPYRIGHT:	2011 gotoAndPlay() - http://www.smartfoxserver.com
 * DEVELOPER:	A51 Integrated - http://a51integrated.com
 * 
 * This file is part of SFS2X Space Race.
 * 
 * Contributers: Wayne Helman, Fabricio Medeiros, David Irons,
 * 				 Steve Schoger, Ryan Albrecht, Andy Rohan
 * 
 * SFS2X Space Race is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * included license for more details.
 *
 * You are not allowed to rent, lend, lease, license or distribute SFS2X Space Race or a
 * modified version of SFS2X Space Race to any other person or organization in any way.
 * 
 * For commercial licensing information, please contact gotoAndPlay().
 * 
 *******************************************************************************************/

package sfs2x.extensions.games.spacerace.handlers;

import com.smartfoxserver.v2.SmartFoxServer;
import com.smartfoxserver.v2.annotations.Instantiation;
import com.smartfoxserver.v2.annotations.Instantiation.InstantiationMode;
import com.smartfoxserver.v2.annotations.MultiHandler;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.variables.RoomVariable;
import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
import com.smartfoxserver.v2.entities.variables.VariableType;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.SFSExtension;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sfs2x.extensions.games.spacerace.SpaceRaceGameExtension;
import sfs2x.extensions.games.spacerace.SpaceRaceZoneExtension;

/**
 * SFS2X Space Race demo - Multi Handler
 * Called when a racer collects any type of advantage or shoots their blaster; loses health or goes off track
 * Utilises SmartFoxServer 2X extension dot-syntax capability
 *  
 * @author A51 Integrated
 *
 */
@Instantiation(InstantiationMode.SINGLE_INSTANCE)
@MultiHandler
public class GamePlayEvents extends BaseClientRequestHandler
{
	public Room curRoom;
	
	//Obtain a reference to this handlers parent extension
	SpaceRaceGameExtension gameExt;
		
	@Override
	public void handleClientRequest(User user, ISFSObject params)
	{		
		gameExt = (SpaceRaceGameExtension) getParentExtension();
		curRoom = gameExt.getGameRoom();
		// Obtain the user list for the current room
		List<User> userList = curRoom.getUserList();
		
		// Obtain the request id
        String requestId = params.getUtfString(SFSExtension.MULTIHANDLER_REQUEST_ID);
        // Add player's name to object before broadcasting to room
		params.putUtfString(SpaceRaceGameExtension.PLAYER_NAME, user.getName());
		
        if(requestId.equals(SpaceRaceGameExtension.CMD_ADVANTAGE)) 
        {
        	handleAdvantage(user, params, userList);
        }
        else if(requestId.equals(SpaceRaceGameExtension.CMD_OFFTRACK)) 
        {
        	handleOffTrack(user, params, userList);
        }
        else if (requestId.equals(SpaceRaceGameExtension.CMD_HEALTHCHECK))
        {
			handleHealthUpdate(user, params, userList);
		}
		
		
	}
	
	public void handleAdvantage(User user, ISFSObject params, List<User> userList)
	{		
		// Send the object to all user in the game room using TCP
		gameExt.send(SpaceRaceGameExtension.CMD_ADVANTAGE, params, userList, false);
	}

	public void handleOffTrack(User user, ISFSObject params, List<User> userList)
	{		
		// Send the object to all user in the game room using TCP
		gameExt.send(SpaceRaceGameExtension.CMD_OFFTRACK, params, userList, false);
	}

	public void handleHealthUpdate(User user, ISFSObject params, List<User> userList)
	{		
		// Send the object to all user in the game room using TCP
		gameExt.send(SpaceRaceGameExtension.CMD_HEALTHCHECK, params, userList, false);
		
		// If a craft is destroyed update RoomVariable so ranking list at game end is correct 
		if (params.getInt("life") == 0)
		{
			ISFSObject obj = new SFSObject();
			List<RoomVariable> varList = curRoom.getVariables();
			
			Iterator<RoomVariable> iterator = varList.iterator();
			while (iterator.hasNext())
			{
				RoomVariable variable = iterator.next();
				if (variable.getType() == VariableType.OBJECT)
				{
					obj = (SFSObject) variable.getSFSObjectValue();
					
					if (obj.containsKey(SpaceRaceZoneExtension.PLAYER_NAME))
					{
						// Update the variable this user is associated with
						if (obj.getUtfString(SpaceRaceZoneExtension.PLAYER_NAME).equalsIgnoreCase(user.getName()))
						{
							SFSObject new_obj = new SFSObject();
							new_obj.putUtfString(SpaceRaceZoneExtension.PLAYER_NAME, user.getName());
							new_obj.putInt("t", -2);
							
							RoomVariable var = new SFSRoomVariable(variable.getName(), new_obj);
							List<RoomVariable> listOfVars = new ArrayList<RoomVariable>();
							listOfVars.add(var);
							
							// Update the RoomVariables
							SmartFoxServer.getInstance().getAPIManager().getSFSApi().setRoomVariables(user, curRoom, listOfVars);
						}
					}
				}				
			}
		}
	}
}