class ActivatedDeviceTransfromAnim extends InteractiveDevice
{
	private var m_animationState : Int32;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_controller = ( ( ActivatedDeviceController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( !( GetDevicePS().IsON() ) )
		{
			m_animationState = 1;
			RefreshAnimation();
		}
	}

	protected override function UpdateDeviceState( optional isDelayed : Bool ) : Bool
	{
		if( super.UpdateDeviceState( isDelayed ) )
		{
			RefreshAnimation();
			return true;
		}
		else
		{
			return false;
		}
	}

	protected const override function GetController() : ActivatedDeviceController
	{
		return ( ( ActivatedDeviceController )( m_controller ) );
	}

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

	protected event OnActivateDevice( evt : ActivateDevice )
	{
		SetGameplayRoleToNone();
		RefreshAnimation();
		if( GetDevicePS().ShouldGlitchOnActivation() )
		{
			StartGlitching( EGlitchState.DEFAULT, 1.0 );
		}
	}

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

	protected virtual function RefreshAnimation()
	{
		var playEvent : gameTransformAnimationPlayEvent;
		playEvent = new gameTransformAnimationPlayEvent;
		if( GetDevicePS().IsDisabled() )
		{
			SendSimpleAnimFeature( true, false, false );
			playEvent.animationName = 'disable';
			m_animationState = 0;
		}
		else if( ( GetDevicePS().IsON() && ( m_animationState == 0 ) ) || GetDevicePS().IsDistracting() )
		{
			SendSimpleAnimFeature( true, false, false );
			playEvent.animationName = 'activate';
			m_animationState = 1;
		}
		else if( !( GetDevicePS().IsON() ) && ( m_animationState == 1 ) )
		{
			SendSimpleAnimFeature( false, false, false );
			playEvent.animationName = 'deactivate';
			m_animationState = 0;
		}
		playEvent.looping = false;
		playEvent.timesPlayed = 1;
		playEvent.timeScale = GetDevicePS().GetAnimationTime();
		if( !( m_wasAnimationFastForwarded ) )
		{
			playEvent.timeScale = 100.0;
			m_wasAnimationFastForwarded = true;
		}
		QueueEvent( playEvent );
	}

	protected function SendSimpleAnimFeature( bool1 : Bool, bool2 : Bool, bool3 : Bool )
	{
		var animFeature : AnimFeature_SimpleDevice;
		animFeature = new AnimFeature_SimpleDevice;
		animFeature.isOpen = bool1;
		animFeature.isOpenLeft = bool2;
		animFeature.isOpenRight = bool3;
		ApplyAnimFeatureToReplicate( this, 'device', animFeature );
	}

	protected function SpawnVFXs( fx : FxResource )
	{
		var transform : WorldTransform;
		var position : WorldPosition;
		var fxSystem : FxSystem;
		if( FxResource.IsValid( fx ) )
		{
			fxSystem = GameInstance.GetFxSystem( GetGame() );
			WorldPosition.SetVector4( position, GetWorldPosition() );
			WorldTransform.SetWorldPosition( transform, position );
			fxSystem.SpawnEffect( fx, transform );
		}
	}

	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 OnActionEngineering( evt : ActionEngineering )
	{
		GetDevicePS().ActivateThisDevice();
	}

	protected event OnWorkspotFinished( componentName : CName )
	{
		GetDevicePS().ActivateThisDevice();
	}

	public const override function DeterminGameplayRoleMappinRange( const data : ref< SDeviceMappinData > ) : Float
	{
		var range : Float;
		if( data.gameplayRole != EGameplayRole.None )
		{
			range = GetDistractionRange( DeviceStimType.Distract );
		}
		return range;
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		SpawnVFXs( GetDevicePS().GetVFX() );
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.Start, ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetActivationVFXname() );
		RefreshAnimation();
	}

	protected override function StopGlitching()
	{
		RefreshAnimation();
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.BreakLoop, ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetActivationVFXname() );
	}

}

