class FactInvokerController extends MasterController
{

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

}

class FactInvokerDataEntry extends IScriptable
{
	private instanceeditable var m_fact : CName;
	private instanceeditable var m_password : CName;

	public function GetFact() : CName
	{
		return m_fact;
	}

	public function GetPassword() : CName
	{
		return m_password;
	}

}

class FactInvokerControllerPS extends MasterControllerPS
{
	private const instanceeditable inlined var m_factDataEntries : array< FactInvokerDataEntry >;
	private var m_passwords : array< CName >;
	private var m_arePasswordsInitialized : Bool;

	protected override function GameAttached()
	{
		EnsurePasswordsPresence();
	}

	public const override function GetPasswords() : array< CName >
	{
		return m_passwords;
	}

	public quest const override function IsDeviceSecured() : Bool
	{
		return m_passwords.Size() > 0;
	}

	private function EnsurePasswordsPresence()
	{
		var i : Int32;
		if( !( m_arePasswordsInitialized ) )
		{
			for( i = 0; i < m_factDataEntries.Size(); i += 1 )
			{
				m_passwords.PushBack( m_factDataEntries[ i ].GetPassword() );
			}
			m_arePasswordsInitialized = true;
		}
	}

	public override function OnAuthorizeUser( evt : AuthorizeUser ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetAll();
		TryInvokeFact( evt );
		Notify( notifier, evt );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	private function TryInvokeFact( evt : AuthorizeUser )
	{
		var enteredPassword : CName;
		var questSystem : QuestsSystem;
		var fact : CName;
		enteredPassword = evt.GetEnteredPassword();
		questSystem = GameInstance.GetQuestsSystem( GetGameInstance() );
		if( UserAuthorizationAttempt( evt.GetExecutor().GetEntityID(), enteredPassword ) && TryGetFact( enteredPassword, fact ) )
		{
			questSystem.SetFact( fact, 1 );
		}
	}

	private function TryGetFact( password : CName, out fact : CName ) : Bool
	{
		var i : Int32;
		for( i = 0; i < m_factDataEntries.Size(); i += 1 )
		{
			if( m_factDataEntries[ i ].GetPassword() == password )
			{
				fact = m_factDataEntries[ i ].GetFact();
				return true;
			}
		}
		return false;
	}

	protected override function GetInkWidgetTweakDBID( const context : ref< GetActionsContext > ) : TweakDBID
	{
		return T"DevicesUIDefinitions.KeypadWithAuthorizationButtonWidget";
	}

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		outActions.PushBack( ActionAuthorizeUser( ShouldForceAuthorizeUser( context ) ) );
		if( ShouldShowExamineIntaraction() && !( IsPlayerPerformingTakedown() ) )
		{
			outActions.PushBack( ActionToggleZoomInteraction() );
		}
		return false;
	}

	protected override function IgnoreDisallowedAction( name : String ) : Bool
	{
		if( name == "ToggleZoomInteraction" )
		{
			return true;
		}
		return false;
	}

}

