/*
========================
idMenuScreen_Shell_PartyLobby::HandleAction h
========================
*/
bool idMenuScreen_Shell_PartyLobby::HandleAction( idWidgetAction& action, const idWidgetEvent& event, idMenuWidget* widget, bool forceHandled )
{

	if( menuData == NULL )
	{
		return true;
	}
	
	if( menuData->ActiveScreen() != SHELL_AREA_PARTY_LOBBY )
	{
		return false;
	}
	
	widgetAction_t actionType = action.GetType();
	const idSWFParmList& parms = action.GetParms();
	
	switch( actionType )
	{
		case WIDGET_ACTION_JOY4_ON_PRESS:
		{
		
			idLobbyBase& activeLobby = session->GetPartyLobbyBase();
			lobbyUserID_t luid;
			if( CanKickSelectedPlayer( luid ) )
			{
				activeLobby.KickLobbyUser( luid );
			}
			return true;
		}
		case WIDGET_ACTION_GO_BACK:
		{
			class idSWFScriptFunction_Accept : public idSWFScriptFunction_RefCounted
			{
			public:
				idSWFScriptFunction_Accept() { }
				idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms )
				{
					common->Dialog().ClearDialog( GDM_LEAVE_LOBBY_RET_MAIN );
					session->Cancel();
					
					return idSWFScriptVar();
				}
			};
			class idSWFScriptFunction_Cancel : public idSWFScriptFunction_RefCounted
			{
			public:
				idSWFScriptFunction_Cancel() { }
				idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms )
				{
					common->Dialog().ClearDialog( GDM_LEAVE_LOBBY_RET_MAIN );
					return idSWFScriptVar();
				}
			};
			
			idLobbyBase& activeLobby = session->GetActivePlatformLobbyBase();
			
			if( activeLobby.GetNumActiveLobbyUsers() > 1 )
			{
				common->Dialog().AddDialog( GDM_LEAVE_LOBBY_RET_MAIN, DIALOG_ACCEPT_CANCEL, new( TAG_SWF ) idSWFScriptFunction_Accept(), new( TAG_SWF ) idSWFScriptFunction_Cancel(), false );
			}
			else
			{
				session->Cancel();
			}
			
			return true;
		}
		case WIDGET_ACTION_MUTE_PLAYER:
		{
		
			if( parms.Num() != 1 )
			{
				return true;
			}
			
			int index = parms[0].ToInteger();
			
			idLobbyBase& activeLobby = session->GetPartyLobbyBase();
			lobbyUserID_t luid = activeLobby.GetLobbyUserIdByOrdinal( index );
			if( luid.IsValid() )
			{
				session->ToggleLobbyUserVoiceMute( luid );
			}
			
			return true;
		}
		case WIDGET_ACTION_COMMAND:
		{
		
			if( options == NULL )
			{
				return true;
			}
			
			int selectionIndex = options->GetFocusIndex();
			if( parms.Num() > 1 )
			{
				selectionIndex = parms[1].ToInteger();
			}
			
			if( selectionIndex != options->GetFocusIndex() )
			{
				options->SetViewIndex( options->GetViewOffset() + selectionIndex );
				options->SetFocusIndex( selectionIndex );
			}
			
			switch( parms[0].ToInteger() )
			{
				case PARTY_CMD_QUICK:
				{
					idMatchParameters matchParameters = idMatchParameters( session->GetPartyLobbyBase().GetMatchParms() );
					
					// Reset these to random for quick match.
					matchParameters.gameMap =  GAME_MAP_RANDOM;
					matchParameters.gameMode = GAME_MODE_RANDOM;
					
					// Always a public match.
					matchParameters.matchFlags &= ~MATCH_INVITE_ONLY;
					
					session->UpdatePartyParms( matchParameters );
					
					// Update flags for game lobby.
					matchParameters.matchFlags = DefaultPartyFlags | DefaultPublicGameFlags;
					cvarSystem->MoveCVarsToDict( CVAR_SERVERINFO, matchParameters.serverInfo );
					
					// Force a default value for the si_timelimit and si_fraglimit for quickmatch
					matchParameters.serverInfo.SetInt( "si_timelimit", 15 );
					matchParameters.serverInfo.SetInt( "si_fraglimit", 10 );
					
					session->FindOrCreateMatch( matchParameters );
					break;
				}
				case PARTY_CMD_FIND:
				{
					menuData->SetNextScreen( SHELL_AREA_MODE_SELECT, MENU_TRANSITION_SIMPLE );
					break;
				}
				case PARTY_CMD_CREATE:
				{
					idMatchParameters matchParameters = idMatchParameters( session->GetPartyLobbyBase().GetMatchParms() );
					
					const bool isInviteOnly = MatchTypeInviteOnly( matchParameters.matchFlags );
					
					matchParameters.matchFlags = DefaultPartyFlags | DefaultPrivateGameFlags;
					
					if( isInviteOnly )
					{
						matchParameters.matchFlags |= MATCH_INVITE_ONLY;
					}
					
					int mode = idMath::ClampInt( -1, GAME_COUNT - 1, si_mode.GetInteger() );
					const idList< mpMap_t > maps = common->GetMapList();
					int map = idMath::ClampInt( -1, maps.Num() - 1, si_map.GetInteger() );
					
					matchParameters.gameMap = map;
					matchParameters.gameMode = mode;
					cvarSystem->MoveCVarsToDict( CVAR_SERVERINFO, matchParameters.serverInfo );
					session->CreateMatch( matchParameters );
					break;
				}
				case PARTY_CMD_PWF:
				{
					menuData->SetNextScreen( SHELL_AREA_BROWSER, MENU_TRANSITION_SIMPLE );
					break;
				}
				case PARTY_CMD_LEADERBOARDS:
				{
					ShowLeaderboards();
					break;
				}
				case PARTY_CMD_TOGGLE_PRIVACY:
				{
					idMatchParameters matchParameters = idMatchParameters( session->GetPartyLobbyBase().GetMatchParms() );
					matchParameters.matchFlags ^= MATCH_INVITE_ONLY;
					session->UpdatePartyParms( matchParameters );
					int bitSet = ( matchParameters.matchFlags & MATCH_INVITE_ONLY );
					net_inviteOnly.SetBool( bitSet != 0 ? true : false );
					break;
				}
				case PARTY_CMD_SHOW_PARTY_GAMES:
				{
					session->ShowPartySessions();
					break;
				}
				case PARTY_CMD_INVITE:
				{
					if( session->GetPartyLobbyBase().IsLobbyFull() )
					{
						common->Dialog().AddDialog( GDM_CANNOT_INVITE_LOBBY_FULL, DIALOG_CONTINUE, NULL, NULL, true, __FUNCTION__, __LINE__, false );
						return true;
					}
					
					InvitePartyOrFriends();
					break;
				}
			}
			
			return true;
		}
		case WIDGET_ACTION_START_REPEATER:
		{
		
			if( options == NULL )
			{
				return true;
			}
			
			if( parms.Num() == 4 )
			{
				int selectionIndex = parms[3].ToInteger();
				if( selectionIndex != options->GetFocusIndex() )
				{
					options->SetViewIndex( options->GetViewOffset() + selectionIndex );
					options->SetFocusIndex( selectionIndex );
				}
			}
			break;
		}
		case WIDGET_ACTION_SELECT_GAMERTAG:
		{
			int selectionIndex = lobby->GetFocusIndex();
			if( parms.Num() > 0 )
			{
				selectionIndex = parms[0].ToInteger();
			}
			
			if( selectionIndex != lobby->GetFocusIndex() )
			{
				lobby->SetViewIndex( lobby->GetViewOffset() + selectionIndex );
				lobby->SetFocusIndex( selectionIndex );
				return true;
			}
			
			idLobbyBase& activeLobby = session->GetPartyLobbyBase();
			lobbyUserID_t luid = activeLobby.GetLobbyUserIdByOrdinal( selectionIndex );
			if( luid.IsValid() )
			{
				session->ShowLobbyUserGamerCardUI( luid );
			}
			
			return true;
		}
	}
	
	return idMenuWidget::HandleAction( action, event, widget, forceHandled );
}
/*
========================
idMenuScreen_Shell_GameLobby::HandleAction h
========================
*/
bool idMenuScreen_Shell_GameLobby::HandleAction( idWidgetAction& action, const idWidgetEvent& event, idMenuWidget* widget, bool forceHandled )
{

	if( menuData == NULL )
	{
		return true;
	}
	
	if( menuData->ActiveScreen() != SHELL_AREA_GAME_LOBBY )
	{
		return false;
	}
	
	widgetAction_t actionType = action.GetType();
	const idSWFParmList& parms = action.GetParms();
	
	switch( actionType )
	{
		case WIDGET_ACTION_JOY4_ON_PRESS:
		{
			idLobbyBase& activeLobby = session->GetActivePlatformLobbyBase();
			lobbyUserID_t luid;
			if( CanKickSelectedPlayer( luid ) )
			{
				activeLobby.KickLobbyUser( luid );
			}
			return true;
		}
		case WIDGET_ACTION_GO_BACK:
		{
			class idSWFScriptFunction_Accept : public idSWFScriptFunction_RefCounted
			{
			public:
				idSWFScriptFunction_Accept() { }
				idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms )
				{
					common->Dialog().ClearDialog( GDM_LEAVE_LOBBY_RET_NEW_PARTY );
					session->Cancel();
					
					return idSWFScriptVar();
				}
			};
			class idSWFScriptFunction_Cancel : public idSWFScriptFunction_RefCounted
			{
			public:
				idSWFScriptFunction_Cancel() { }
				idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms )
				{
					common->Dialog().ClearDialog( GDM_LEAVE_LOBBY_RET_NEW_PARTY );
					return idSWFScriptVar();
				}
			};
			
			idLobbyBase& activeLobby = session->GetActivePlatformLobbyBase();
			
			if( activeLobby.GetNumActiveLobbyUsers() > 1 )
			{
				common->Dialog().AddDialog( GDM_LEAVE_LOBBY_RET_NEW_PARTY, DIALOG_ACCEPT_CANCEL, new( TAG_SWF ) idSWFScriptFunction_Accept(), new( TAG_SWF ) idSWFScriptFunction_Cancel(), false );
			}
			else
			{
				session->Cancel();
			}
			
			return true;
		}
		case WIDGET_ACTION_MUTE_PLAYER:
		{
		
			if( parms.Num() != 1 )
			{
				return true;
			}
			
			int index = parms[0].ToInteger();
			
			idLobbyBase& activeLobby = session->GetActivePlatformLobbyBase();
			lobbyUserID_t luid = activeLobby.GetLobbyUserIdByOrdinal( index );
			if( luid.IsValid() )
			{
				session->ToggleLobbyUserVoiceMute( luid );
			}
			
			return true;
		}
		case WIDGET_ACTION_COMMAND:
		{
		
			if( options == NULL )
			{
				return true;
			}
			
			int selectionIndex = options->GetFocusIndex();
			if( parms.Num() > 1 )
			{
				selectionIndex = parms[1].ToInteger();
			}
			
			if( selectionIndex != options->GetFocusIndex() )
			{
				options->SetViewIndex( options->GetViewOffset() + selectionIndex );
				options->SetFocusIndex( selectionIndex );
			}
			
			switch( parms[0].ToInteger() )
			{
				case GAME_CMD_START:
				{
					idMenuHandler_Shell* handler = dynamic_cast< idMenuHandler_Shell* const >( menuData );
					if( handler != NULL )
					{
						handler->SetTimeRemaining( 0 );
					}
					break;
				}
				case GAME_CMD_SETTINGS:
				{
					menuData->SetNextScreen( SHELL_AREA_MATCH_SETTINGS, MENU_TRANSITION_SIMPLE );
					break;
				}
				case GAME_CMD_TOGGLE_PRIVACY:
				{
					idMatchParameters matchParameters = idMatchParameters( session->GetActivePlatformLobbyBase().GetMatchParms() );
					matchParameters.matchFlags ^= MATCH_INVITE_ONLY;
					session->UpdateMatchParms( matchParameters );
					int bitSet = ( matchParameters.matchFlags & MATCH_INVITE_ONLY );
					net_inviteOnly.SetBool( bitSet != 0 ? true : false );
					
					// Must update the party parameters too for Xbox JSIP to work in game lobbies.
					idMatchParameters partyParms = session->GetPartyLobbyBase().GetMatchParms();
					if( MatchTypeInviteOnly( matchParameters.matchFlags ) )
					{
						partyParms.matchFlags |= MATCH_INVITE_ONLY;
					}
					else
					{
						partyParms.matchFlags &= ~MATCH_INVITE_ONLY;
					}
					session->UpdatePartyParms( partyParms );
					
					break;
				}
				case GAME_CMD_INVITE:
				{
					if( session->GetActivePlatformLobbyBase().IsLobbyFull() )
					{
						common->Dialog().AddDialog( GDM_CANNOT_INVITE_LOBBY_FULL, DIALOG_CONTINUE, NULL, NULL, true, __FUNCTION__, __LINE__, false );
						return true;
					}
					
					InvitePartyOrFriends();
					break;
				}
			}
			return true;
		}
		case WIDGET_ACTION_START_REPEATER:
		{
		
			if( options == NULL )
			{
				return true;
			}
			
			if( parms.Num() == 4 )
			{
				int selectionIndex = parms[3].ToInteger();
				if( selectionIndex != options->GetFocusIndex() )
				{
					options->SetViewIndex( options->GetViewOffset() + selectionIndex );
					options->SetFocusIndex( selectionIndex );
				}
			}
			break;
		}
		case WIDGET_ACTION_SELECT_GAMERTAG:
		{
			int selectionIndex = lobby->GetFocusIndex();
			if( parms.Num() > 0 )
			{
				selectionIndex = parms[0].ToInteger();
			}
			
			if( selectionIndex != lobby->GetFocusIndex() )
			{
				lobby->SetViewIndex( lobby->GetViewOffset() + selectionIndex );
				lobby->SetFocusIndex( selectionIndex );
				return true;
			}
			
			idLobbyBase& activeLobby = session->GetActivePlatformLobbyBase();
			lobbyUserID_t luid = activeLobby.GetLobbyUserIdByOrdinal( selectionIndex );
			if( luid.IsValid() )
			{
				session->ShowLobbyUserGamerCardUI( luid );
			}
			
			return true;
		}
	}
	
	return idMenuWidget::HandleAction( action, event, widget, forceHandled );
}