class SecurityLocker extends InteractiveDevice
{
	default m_controllerTypeName = 'SecurityLockerController';
	private var m_cachedEvent : UseSecurityLocker;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
		EntityRequestComponentsInterface.RequestComponent( ri, 'inventory', 'gameInventory', true );
		EntityRequestComponentsInterface.RequestComponent( ri, 'ui', 'AdvertisementWidgetComponent', false );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_uiComponent = ( ( IWorldWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ui' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( SecurityLockerController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

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

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

	protected override function CutPower()
	{
		super.CutPower();
		TurnOffScreen();
	}

	protected override function TurnOnDevice()
	{
		super.TurnOnDevice();
		TurnOnScreen();
	}

	protected override function TurnOffDevice()
	{
		super.TurnOffDevice();
		TurnOffScreen();
	}

	private function TurnOffScreen()
	{
		if( m_uiComponent != NULL )
		{
			m_uiComponent.Toggle( false );
		}
	}

	private function TurnOnScreen()
	{
		if( m_uiComponent != NULL )
		{
			m_uiComponent.Toggle( true );
		}
	}

	protected event OnUseSecurityLocker( evt : UseSecurityLocker )
	{
		m_cachedEvent = evt;
		if( !( ( ( Bool )evt.prop.first ) ) )
		{
			DisarmUser( evt );
		}
		else
		{
			ReturnArms( evt );
		}
	}

	protected event OnDisarm( evt : Disarm )
	{
		if( GetDevicePS().ShouldDisableCyberware() )
		{
			ActivateCyberwere( false );
		}
		if( GetDevicePS().IsPartOfSystem( ESystems.SecuritySystem ) )
		{
			GetDevicePS().GetSecuritySystem().AuthorizeUser( evt.requester.GetEntityID(), GetDevicePS().GetAuthorizationLevel() );
		}
	}

	private function DisarmUser( evt : UseSecurityLocker )
	{
		var disarm : Disarm;
		if( !( evt.GetExecutor() ) )
		{
			return;
		}
		disarm = new Disarm;
		disarm.requester = this;
		evt.GetExecutor().QueueEvent( disarm );
		PlaySoundEvent( this, GetDevicePS().GetStoreSFX() );
	}

	private function ReturnArms( evt : UseSecurityLocker )
	{
		var arm : Arm;
		arm = new Arm;
		arm.requester = this;
		evt.GetExecutor().QueueEvent( arm );
		if( GetDevicePS().ShouldDisableCyberware() )
		{
			ActivateCyberwere( true );
		}
		PlaySoundEvent( this, GetDevicePS().GetReturnSFX() );
	}

	private function TransferItems( const items : ref< array< weak< gameItemData > > >, from : GameObject, to : GameObject )
	{
		var i : Int32;
		var transactionSystem : TransactionSystem;
		transactionSystem = GameInstance.GetTransactionSystem( GetGame() );
		for( i = 0; i < items.Size(); i += 1 )
		{
			if( !( IsFinal() ) )
			{
			}
			transactionSystem.TransferItem( from, to, items[ i ].GetID(), items[ i ].GetQuantity() );
		}
	}

	private function ActivateCyberwere( activate : Bool )
	{
		var noCyberware : TweakDBID;
		var obj : weak< GameObject >;
		noCyberware = T"GameplayRestriction.SecurityLocker";
		obj = m_cachedEvent.GetExecutor();
		if( obj && TDBID.IsValid( noCyberware ) )
		{
			if( activate )
			{
				StatusEffectHelper.RemoveStatusEffect( obj, noCyberware );
			}
			else
			{
				StatusEffectHelper.ApplyStatusEffect( obj, noCyberware );
			}
			GetDevicePS().DisconnectPersonalLink( m_cachedEvent );
		}
	}

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

}

