class DrillerScanEvent extends Event
{
	var newIsScanning : Bool;
}

class DrillMachineScanManager extends ScriptableComponent
{
	private var m_ppStarting : Bool;
	private var m_ppEnding : Bool;
	private var m_ppCurrentStartTime : Float;
	private var m_ppCurrentEndFrame : Int32;
	private var m_idleToScanTime : Float;
	default m_idleToScanTime = 1.7;
	private var m_ppOffFrameDelay : Int32;
	default m_ppOffFrameDelay = 2;

	protected event OnDrillerScanEvent( evt : DrillerScanEvent )
	{
		m_ppStarting = evt.newIsScanning;
		if( !( evt.newIsScanning ) )
		{
			m_ppEnding = true;
		}
	}

	private function OnUpdate( dt : Float )
	{
		if( m_ppStarting )
		{
			m_ppCurrentStartTime += dt;
			if( m_ppCurrentStartTime > m_idleToScanTime )
			{
				m_ppStarting = false;
				m_ppCurrentStartTime = 0.0;
				QueuePostProcessEvent( true );
			}
		}
		if( m_ppEnding )
		{
			m_ppCurrentEndFrame += 1;
			if( m_ppCurrentEndFrame > m_ppOffFrameDelay )
			{
				m_ppEnding = false;
				m_ppCurrentEndFrame = 0;
				QueuePostProcessEvent( false );
			}
		}
	}

	private function QueuePostProcessEvent( isEnabled : Bool )
	{
		var evt : DrillScanPostProcessEvent;
		evt = new DrillScanPostProcessEvent;
		evt.IsEnabled = isEnabled;
		GetOwner().QueueEvent( evt );
	}

}

class RewireComponent extends ScriptableComponent
{
	instanceeditable var miniGameVideoPath : ResRef;
	default miniGameVideoPath = R"base\movies\loading_screen_temp.bk2";
	instanceeditable var miniGameAudioEvent : CName;
	instanceeditable var miniGameVideoLenght : Float;
	default miniGameVideoLenght = 5.f;
	private var m_rewireEvent : RewireEvent;
	private var m_rewireCurrentLenght : Float;
	private var m_isActive : Bool;
	default m_isActive = false;

	protected event OnTakeControl( ri : EntityResolveComponentsInterface ) {}

	protected event OnRewireStart( rewireEvent : RewireEvent )
	{
		m_rewireCurrentLenght = 0.0;
		m_rewireEvent = rewireEvent;
		m_isActive = true;
		RewireFinished();
		ToggleMovie( true );
	}

	private function OnUpdate( dt : Float )
	{
		if( m_isActive )
		{
			m_rewireCurrentLenght += dt;
			if( m_rewireCurrentLenght > miniGameVideoLenght )
			{
				RewireFinished();
			}
		}
	}

	private function RewireFinished()
	{
		m_isActive = false;
		m_rewireEvent.sucess = true;
		m_rewireEvent.state = EDrillMachineRewireState.RewireFinished;
		ToggleMovie( false );
		GetOwner().QueueEventForEntityID( m_rewireEvent.ownerID, m_rewireEvent );
		GetOwner().QueueEventForEntityID( m_rewireEvent.activatorID, m_rewireEvent );
	}

	private function ToggleMovie( play : Bool ) {}
}

