importonly final class gameConveyorControlEvent extends Event
{
	import var enable : Bool;
}

class ConveyorController extends ScriptableDeviceComponent
{

	public const override function GetPS() : ConveyorControllerPS
	{
		return ( ( ConveyorControllerPS )( GetBasePS() ) );
	}

	private function OnGameAttach()
	{
		RestoreDeviceState();
	}

	private function RestoreDeviceState()
	{
		if( GetPS().IsON() )
		{
			StartConveyor();
		}
		else
		{
			StopConveyor();
		}
	}

	protected event OnSetDeviceON( evt : SetDeviceON )
	{
		StartConveyor();
	}

	protected event OnSetDeviceOFF( evt : SetDeviceOFF )
	{
		StopConveyor();
	}

	protected event OnToggleON( evt : ToggleON )
	{
		var value : Bool;
		value = ( ( Bool )evt.prop.first );
		if( !( value ) )
		{
			StartConveyor();
		}
		else
		{
			StopConveyor();
		}
	}

	private function StartConveyor()
	{
		var evt : gameConveyorControlEvent;
		evt = new gameConveyorControlEvent;
		evt.enable = true;
		GetEntity().QueueEvent( evt );
	}

	private function StopConveyor()
	{
		var evt : gameConveyorControlEvent;
		evt = new gameConveyorControlEvent;
		evt.enable = false;
		GetEntity().QueueEvent( evt );
	}

}

class ConveyorControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#45661";
	default m_deviceState = EDeviceStatus.OFF;

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( actions, context );
		if( !( IsUserAuthorized( context.processInitiatorObject.GetEntityID() ) ) )
		{
			return false;
		}
		if( ToggleON.IsDefaultConditionMet( this, context ) )
		{
			actions.PushBack( ActionToggleON() );
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	protected override function OnSetDeviceON( evt : SetDeviceON ) : EntityNotificationType
	{
		if( IsUnpowered() || IsDisabled() )
		{
			return SendActionFailedEvent( evt, evt.GetRequesterID(), "Unpowered or Disabled" );
		}
		SetDeviceState( EDeviceStatus.ON );
		if( !( IsFinal() ) )
		{
		}
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected override function OnSetDeviceOFF( evt : SetDeviceOFF ) : EntityNotificationType
	{
		if( IsUnpowered() || IsDisabled() )
		{
			return SendActionFailedEvent( evt, evt.GetRequesterID(), "Unpowered or Disabled" );
		}
		SetDeviceState( EDeviceStatus.OFF );
		if( !( IsFinal() ) )
		{
		}
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnToggleON( evt : ToggleON ) : EntityNotificationType
	{
		var value : Bool;
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		value = ( ( Bool )evt.prop.first );
		if( !( value ) )
		{
			SetDeviceState( EDeviceStatus.ON );
		}
		else
		{
			SetDeviceState( EDeviceStatus.OFF );
		}
		if( !( IsFinal() ) )
		{
		}
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

}

