class ScannerControlComponent extends ScriptableComponent
{
	private var m_currentScanType : MechanicalScanType;
	private var m_currentScanEffect : EffectInstance;
	private var m_currentScanAnimation : CName;
	private editable var m_scannerTriggerComponentName : CName;
	private var m_scannerTriggerComponent : IComponent;
	private var m_a : TriggerComponent;
	private var m_isScanningPlayer : Bool;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, m_scannerTriggerComponentName, 'entTriggerComponent', false );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_scannerTriggerComponent = EntityResolveComponentsInterface.GetComponent( ri, m_scannerTriggerComponentName );
		m_a = ( ( TriggerComponent )( EntityResolveComponentsInterface.GetComponent( ri, m_scannerTriggerComponentName ) ) );
	}

	protected export function OnGameAttach()
	{
		m_currentScanType = MechanicalScanType.None;
		if( m_scannerTriggerComponent )
		{
			m_scannerTriggerComponent.Toggle( false );
		}
	}

	protected event OnAIEvent( aiEvent : AIEvent )
	{
		switch( aiEvent.name )
		{
			case 'ScanShort':
				StartScanning( MechanicalScanType.Short );
			break;
			case 'ScanLong':
				StartScanning( MechanicalScanType.Long );
			break;
			case 'ScanDanger':
				StartScanning( MechanicalScanType.Danger );
			break;
			case 'StopScanning':
				StopScanning();
			break;
			default:
				break;
		}
	}

	protected event OnDeath( evt : gameDeathEvent )
	{
		StopScanning();
	}

	protected event OnAreaEnter( trigger : AreaEnteredEvent )
	{
		var player : ScriptedPuppet;
		player = ( ( ScriptedPuppet )( GameInstance.GetPlayerSystem( GetOwner().GetGame() ).GetLocalPlayerControlledGameObject() ) );
		if( !( m_isScanningPlayer ) && ( EntityGameInterface.GetEntity( trigger.activator ).GetEntityID() == player.GetEntityID() ) )
		{
			StartFullscreenPlayerVFX();
		}
	}

	protected event OnAreaExit( trigger : AreaExitedEvent )
	{
		var player : ScriptedPuppet;
		player = ( ( ScriptedPuppet )( GameInstance.GetPlayerSystem( GetOwner().GetGame() ).GetLocalPlayerControlledGameObject() ) );
		if( m_isScanningPlayer && ( EntityGameInterface.GetEntity( trigger.activator ).GetEntityID() == player.GetEntityID() ) )
		{
			StopFullscreenPlayerVFX();
		}
	}

	protected function StartFullscreenPlayerVFX()
	{
		var player : GameObject;
		if( m_currentScanType == MechanicalScanType.None || m_currentScanType == MechanicalScanType.Short )
		{
			return;
		}
		player = GameInstance.GetPlayerSystem( GetOwner().GetGame() ).GetLocalPlayerControlledGameObject();
		if( m_currentScanType == MechanicalScanType.Long )
		{
			GameObjectEffectHelper.StartEffectEvent( player, 'screen_scanning_loop', true );
		}
		else if( m_currentScanType == MechanicalScanType.Danger )
		{
			GameObjectEffectHelper.StartEffectEvent( player, 'screen_scanning_red_loop', true );
		}
		m_isScanningPlayer = true;
	}

	protected function StopFullscreenPlayerVFX()
	{
		var player : GameObject;
		if( !( m_isScanningPlayer ) )
		{
			return;
		}
		player = GameInstance.GetPlayerSystem( GetOwner().GetGame() ).GetLocalPlayerControlledGameObject();
		GameObjectEffectHelper.BreakEffectLoopEvent( player, 'screen_scanning_loop' );
		GameObjectEffectHelper.BreakEffectLoopEvent( player, 'screen_scanning_red_loop' );
		m_isScanningPlayer = false;
	}

	private function StartScanning( scanType : MechanicalScanType )
	{
		if( m_currentScanType == MechanicalScanType.None )
		{
			PlayScannerSlotAnimation( 'scan_default' );
		}
		StopCurrentScanningEffect();
		switch( scanType )
		{
			case MechanicalScanType.Short:
				GameObject.StartReplicatedEffectEvent( GetOwner(), 'scan_short' );
			break;
			case MechanicalScanType.Long:
				GameObject.StartReplicatedEffectEvent( GetOwner(), 'scan' );
			break;
			case MechanicalScanType.Danger:
				GameObject.StartReplicatedEffectEvent( GetOwner(), 'scan_red' );
			break;
			default:
				break;
		}
		m_currentScanType = scanType;
		if( m_scannerTriggerComponent && ( m_currentScanType == MechanicalScanType.Long || m_currentScanType == MechanicalScanType.Danger ) )
		{
			if( m_isScanningPlayer )
			{
				StopFullscreenPlayerVFX();
				StartFullscreenPlayerVFX();
			}
			m_scannerTriggerComponent.Toggle( true );
		}
	}

	private function StopCurrentScanningEffect()
	{
		switch( m_currentScanType )
		{
			case MechanicalScanType.Short:
				GameObject.BreakReplicatedEffectLoopEvent( GetOwner(), 'scan_short' );
			break;
			case MechanicalScanType.Long:
				GameObject.BreakReplicatedEffectLoopEvent( GetOwner(), 'scan' );
			break;
			case MechanicalScanType.Danger:
				GameObject.BreakReplicatedEffectLoopEvent( GetOwner(), 'scan_red' );
			break;
			default:
				break;
		}
	}

	private function StopScanning()
	{
		StopCurrentScanningEffect();
		StopScannerSlotAnimation();
		if( m_scannerTriggerComponent )
		{
			m_scannerTriggerComponent.Toggle( false );
			StopFullscreenPlayerVFX();
		}
		m_currentScanType = MechanicalScanType.None;
	}

	protected function PlayScannerSlotAnimation( animationName : CName )
	{
		var transformAnimationPlayEvent : gameTransformAnimationPlayEvent;
		transformAnimationPlayEvent = new gameTransformAnimationPlayEvent;
		transformAnimationPlayEvent.animationName = animationName;
		transformAnimationPlayEvent.looping = true;
		transformAnimationPlayEvent.timeScale = 1.0;
		GetOwner().QueueEvent( transformAnimationPlayEvent );
	}

	protected function StopScannerSlotAnimation()
	{
		var transformAnimationResetEvent : gameTransformAnimationResetEvent;
		transformAnimationResetEvent = new gameTransformAnimationResetEvent;
		transformAnimationResetEvent.animationName = m_currentScanAnimation;
		GetOwner().QueueEvent( transformAnimationResetEvent );
	}

}

