class EntityNoticedPlayerPrereqState extends PrereqState
{
	var m_owner : weak< GameObject >;
	var m_listenerInt : CallbackHandle;

	protected event OnStateUpdate( value : Uint32 )
	{
		var checkPassed : Bool;
		const var prereq : EntityNoticedPlayerPrereq;
		prereq = ( ( EntityNoticedPlayerPrereq )( GetPrereq() ) );
		checkPassed = prereq.Evaluate( m_owner, value );
		OnChanged( checkPassed );
	}

}

class EntityNoticedPlayerPrereq extends IScriptablePrereq
{
	private var m_isPlayerNoticed : Bool;
	private var m_valueToListen : Uint32;
	default m_valueToListen = 1;

	public const function Evaluate( owner : GameObject, value : Uint32 ) : Bool
	{
		if( m_isPlayerNoticed )
		{
			if( value >= m_valueToListen )
			{
				return true;
			}
		}
		else
		{
			if( value != m_valueToListen )
			{
				return true;
			}
		}
		return false;
	}

	protected override function Initialize( recordID : TweakDBID )
	{
		m_isPlayerNoticed = TweakDBInterface.GetBool( recordID + T".isPlayerNoticed", false );
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var castedState : EntityNoticedPlayerPrereqState;
		var player : weak< PlayerPuppet >;
		castedState = ( ( EntityNoticedPlayerPrereqState )( state ) );
		castedState.m_owner = ( ( GameObject )( context ) );
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			bb = GameInstance.GetBlackboardSystem( player.GetGame() ).Get( GetAllBlackboardDefs().PlayerPerkData );
			castedState.m_listenerInt = bb.RegisterListenerUint( GetAllBlackboardDefs().PlayerPerkData.EntityNoticedPlayer, castedState, 'OnStateUpdate' );
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var bb : IBlackboard;
		var castedState : EntityNoticedPlayerPrereqState;
		var player : weak< PlayerPuppet >;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			bb = GameInstance.GetBlackboardSystem( player.GetGame() ).Get( GetAllBlackboardDefs().PlayerPerkData );
			castedState = ( ( EntityNoticedPlayerPrereqState )( state ) );
			bb.UnregisterListenerUint( GetAllBlackboardDefs().PlayerPerkData.EntityNoticedPlayer, castedState.m_listenerInt );
		}
	}

	protected const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var player : weak< PlayerPuppet >;
		var castedState : EntityNoticedPlayerPrereqState;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			castedState = ( ( EntityNoticedPlayerPrereqState )( state ) );
			castedState.OnChanged( IsFulfilled( player.GetGame(), context ) );
		}
	}

	protected const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var player : weak< PlayerPuppet >;
		var checkPassed : Bool;
		player = ( ( PlayerPuppet )( context ) );
		bb = GameInstance.GetBlackboardSystem( player.GetGame() ).Get( GetAllBlackboardDefs().PlayerPerkData );
		checkPassed = Evaluate( player, bb.GetUint( GetAllBlackboardDefs().PlayerPerkData.EntityNoticedPlayer ) );
		return checkPassed;
	}

}

