class Transition extends Event
{
	var listenerID : Uint32;
}

class SecurityArea extends InteractiveMasterDevice
{
	default m_controllerTypeName = 'SecurityAreaController';
	private var m_area : TriggerComponent;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'area', 'TriggerComponent', false );
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_area = ( ( TriggerComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'area' ) ) );
		m_controller = ( ( SecurityAreaController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected event OnGameAttached()
	{
		super.OnGameAttached();
		m_area.Toggle( ( ( SecurityAreaControllerPS )( GetDevicePS() ) ).IsActive() );
	}

	protected event OnDetach()
	{
		SendFakeExitEventToObjectsInsideMe();
		UnregisterTimeSystemListeners();
	}

	protected event OnSlaveStateChanged( evt : PSDeviceChangedEvent )
	{
		return false;
	}

	protected event OnRegisterTimeListeners( evt : RegisterTimeListeners )
	{
		RegisterTimeSystemListeners();
	}

	private function RegisterTimeSystemListeners()
	{
		GetDevicePS().RegisterTimeSystemListeners( this );
	}

	private function UnregisterTimeSystemListeners()
	{
		GetDevicePS().UnregisterTimeSystemListeners();
	}

	protected event OnTransition( evt : Transition )
	{
		GetDevicePS().ApplyTransition( evt.listenerID );
	}

	protected event OnManageAreaComponent( evt : ManageAreaComponent )
	{
		m_area.Toggle( evt.enable );
	}

	private const override function GetController() : SecurityAreaController
	{
		return ( ( SecurityAreaController )( m_controller ) );
	}

	public const override function GetDevicePS() : SecurityAreaControllerPS
	{
		return GetController().GetPS();
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		var mappinSystem : MappinSystem;
		var obj : GameObject;
		var shouldProcess : Bool;
		var isPlayer : Bool;
		obj = ( ( GameObject )( EntityGameInterface.GetEntity( evt.activator ) ) );
		if( obj.IsPlayer() )
		{
			shouldProcess = true;
			isPlayer = true;
		}
		else if( ( ( ScriptedPuppet )( obj ) ) && !( ( ( ScriptedPuppet )( obj ) ).IsCrowd() ) )
		{
			shouldProcess = true;
		}
		else
		{
			shouldProcess = false;
		}
		if( shouldProcess )
		{
			GetDevicePS().AreaEntered( evt );
			if( isPlayer )
			{
				mappinSystem = GameInstance.GetMappinSystem( GetGame() );
				if( mappinSystem )
				{
					mappinSystem.OnAreaEntered( evt );
				}
			}
		}
	}

	protected event OnAreaExit( evt : AreaExitedEvent )
	{
		OnAreaExitInternal( ( ( GameObject )( EntityGameInterface.GetEntity( evt.activator ) ) ), evt.triggerID );
	}

	private function OnAreaExitInternal( obj : weak< GameObject >, triggerID : EntityID )
	{
		var mappinSystem : MappinSystem;
		var shouldProcess : Bool;
		var isPlayer : Bool;
		if( obj.IsPlayer() )
		{
			shouldProcess = true;
			isPlayer = true;
		}
		else if( ( ( ScriptedPuppet )( obj ) ) && !( ( ( ScriptedPuppet )( obj ) ).IsCrowd() ) )
		{
			shouldProcess = true;
		}
		else
		{
			shouldProcess = false;
		}
		if( shouldProcess )
		{
			GetDevicePS().AreaExited( obj );
			if( isPlayer )
			{
				mappinSystem = GameInstance.GetMappinSystem( GetGame() );
				if( mappinSystem )
				{
					mappinSystem.OnAreaExited( obj, triggerID );
				}
			}
		}
	}

	protected override function AdjustInteractionComponent()
	{
		m_interaction.Toggle( false );
	}

	public const override function GetDefaultHighlight() : FocusForcedHighlightData
	{
		return NULL;
	}

	protected event OnQuestIllegalActionAreaNotification( evt : QuestIllegalActionAreaNotification )
	{
		GetDevicePS().OnQuestIllegalActionAreaNotification( evt );
	}

	protected event OnQuestCombatActionAreaNotification( evt : QuestCombatActionAreaNotification )
	{
		GetDevicePS().OnQuestCombatActionAreaNotification( evt );
	}

	protected event OnQuestAddTransition( evt : QuestAddTransition )
	{
		GetDevicePS().OnQuestAddTransition( evt );
	}

	protected event OnQuestRemoveTransition( evt : QuestRemoveTransition )
	{
		GetDevicePS().OnQuestRemoveTransition( evt );
	}

	protected event OnQuestExecuteTranstion( evt : QuestExecuteTransition )
	{
		GetDevicePS().OnQuestExecuteTransition( evt );
	}

	private function SendFakeExitEventToObjectsInsideMe()
	{
		var ps : SecurityAreaControllerPS;
		var objectsInsideMe : array< AreaEntry >;
		var objectInsideMe : AreaEntry;
		var index : Int32;
		var object : weak< GameObject >;
		ps = ( ( SecurityAreaControllerPS )( GetDevicePS() ) );
		objectsInsideMe = ps.GetUsersInPerimeter();
		for( index = 0; index < objectsInsideMe.Size(); index += 1 )
		{
			objectInsideMe = objectsInsideMe[ index ];
			object = ( ( GameObject )( GameInstance.FindEntityByID( GetGameInstance(), objectInsideMe.user ) ) );
			OnAreaExitInternal( object, GetEntityID() );
		}
	}

}

