class Activator extends InteractiveMasterDevice
{
	default m_controllerTypeName = 'ActivatorController';
	private var m_animFeature : AnimFeature_SimpleDevice;
	private var hitCount : Int32;
	private var m_meshComponent : MeshComponent;
	var meshAppearence : CName;
	default meshAppearence = 'default';
	var meshAppearenceBreaking : CName;
	default meshAppearenceBreaking = 'Yellow';
	var meshAppearenceBroken : CName;
	default meshAppearenceBroken = 'red';
	var defaultDelay : Float;
	default defaultDelay = 2.98f;
	var yellowDelay : Float;
	default yellowDelay = 1.68f;
	var redDelay : Float;
	default redDelay = 4.03f;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'mesh', 'MeshComponent', true );
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_meshComponent = ( ( MeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'mesh' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( ActivatorController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

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

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

	protected override function EnterWorkspot( activator : GameObject, optional freeCamera : Bool, optional componentName : CName, optional deviceData : CName )
	{
		var workspotSystem : WorkspotGameSystem;
		workspotSystem = GameInstance.GetWorkspotSystem( activator.GetGame() );
		workspotSystem.PlayInDeviceSimple( this, activator, freeCamera, componentName, 'deviceWorkspot' );
	}

	protected event OnDisassembleDevice( evt : DisassembleDevice )
	{
		var playerStateMachineBlackboard : IBlackboard;
		var playerPuppet : PlayerPuppet;
		playerPuppet = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject() ) );
		EnterWorkspot( playerPuppet, false, 'disassembleWorkspot' );
		playerStateMachineBlackboard = GameInstance.GetBlackboardSystem( GetGame() ).GetLocalInstanced( playerPuppet.GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		playerStateMachineBlackboard.SetBool( GetAllBlackboardDefs().PlayerStateMachine.IsInteractingWithDevice, true );
		UpdateDeviceState();
		DelayApperanceSwitchEvent( meshAppearenceBreaking, yellowDelay );
		DelayApperanceSwitchEvent( meshAppearence, defaultDelay );
		DelayApperanceSwitchEvent( meshAppearenceBroken, redDelay );
	}

	protected event OnSpiderbotActivateActivator( evt : SpiderbotActivateActivator )
	{
		var spiderbotOrderDeviceEvent : SpiderbotOrderDeviceEvent;
		var locationOverrideRef : NodeRef;
		var locationOverrideID : EntityID;
		var locationOverrideObject : weak< GameObject >;
		var locationOverrideGlobalRef : GlobalNodeRef;
		SendSetIsSpiderbotInteractionOrderedEvent( true );
		spiderbotOrderDeviceEvent = new SpiderbotOrderDeviceEvent;
		spiderbotOrderDeviceEvent.target = this;
		locationOverrideRef = GetDevicePS().GetSpiderbotInteractionLocationOverride();
		locationOverrideGlobalRef = ResolveNodeRefWithEntityID( locationOverrideRef, GetEntityID() );
		if( GlobalNodeRef.IsDefined( ResolveNodeRef( locationOverrideRef, GlobalNodeID.GetRoot() ) ) )
		{
			locationOverrideID = ( ( EntityID )( locationOverrideGlobalRef ) );
			locationOverrideObject = ( ( GameObject )( GameInstance.FindEntityByID( GetGame(), locationOverrideID ) ) );
			spiderbotOrderDeviceEvent.overrideMovementTarget = locationOverrideObject;
		}
		evt.GetExecutor().QueueEvent( spiderbotOrderDeviceEvent );
	}

	protected event OnSpiderbotOrderCompletedEvent( evt : SpiderbotOrderCompletedEvent )
	{
		SendSetIsSpiderbotInteractionOrderedEvent( false );
		GameInstance.GetActivityLogSystem( GetGame() ).AddLog( "SPIDERBOT HAS FINISHED ACTIVATING THE DEVICE ... " );
		GetDevicePS().ActivateConnectedDevices();
		SetGameplayRoleToNone();
	}

	protected event OnToggleActivation( evt : ToggleActivation )
	{
		UpdateDeviceState();
		SetGameplayRoleToNone();
	}

	protected event OnDelayApperanceSwitchEvent( evt : panelApperanceSwitchEvent )
	{
		SetMeshAppearanceEvent( this, evt.newApperance );
	}

	protected event OnWorkspotFinished( componentName : CName )
	{
		super.OnWorkspotFinished( componentName );
		if( componentName == 'disassembleWorkspot' )
		{
			m_disassemblableComponent.ObtainParts();
			GetDevicePS().ActivateConnectedDevices();
			SetGameplayRoleToNone();
			UpdateAnimState();
		}
	}

	private function UpdateAnimState()
	{
		if( !( m_animFeature ) )
		{
			m_animFeature = new AnimFeature_SimpleDevice;
		}
		m_animFeature.isOpen = true;
		m_interaction.Toggle( false );
		AnimationControllerComponent.ApplyFeature( this, 'DeviceMaintenancePanel', m_animFeature );
	}

	private function DelayApperanceSwitchEvent( newApperance : CName, time : Float )
	{
		var evt : panelApperanceSwitchEvent;
		evt = new panelApperanceSwitchEvent;
		evt.newApperance = newApperance;
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, evt, time );
	}

	protected event OnHit( evt : gameHitEvent )
	{
		if( GetDevicePS().GetDurabilityType() == EDeviceDurabilityType.DESTRUCTIBLE )
		{
			hitCount = hitCount + 1;
			if( hitCount > 1 )
			{
				GetDevicePS().ActivateConnectedDevices();
				SetGameplayRoleToNone();
				m_meshComponent.Toggle( false );
			}
		}
	}

	public const override function DeterminGameplayRole() : EGameplayRole
	{
		return EGameplayRole.ControlOtherDevice;
	}

	public const override function GetCurrentGameplayRole() : EGameplayRole
	{
		var gameplayRole : EGameplayRole;
		if( gameplayRole != EGameplayRole.None )
		{
			return gameplayRole;
		}
		else
		{
			return m_gameplayRoleComponent.GetCurrentGameplayRole();
		}
	}

}

