class NPCStatePrereqState extends PrereqState
{
	var m_owner : weak< GameObject >;
	var m_listenerInt : CallbackHandle;
	var m_prevValue : Int32;

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

}

class NPCStatePrereq extends IScriptablePrereq
{
	private var m_previousState : Bool;
	private var m_isInState : Bool;
	private var m_skipWhenApplied : Bool;

	public const function Evaluate( owner : GameObject, newValue : Int32, prevValue : Int32 ) : Bool
	{
		var stateToCheck : Int32;
		stateToCheck = GetStateToCheck();
		if( m_previousState )
		{
			if( ( newValue != stateToCheck ) && ( prevValue == stateToCheck ) )
			{
				return true;
			}
		}
		else if( m_isInState )
		{
			if( newValue == stateToCheck )
			{
				return true;
			}
		}
		else
		{
			if( newValue != stateToCheck )
			{
				return true;
			}
		}
		return false;
	}

	protected override function Initialize( recordID : TweakDBID )
	{
		m_previousState = TweakDBInterface.GetBool( recordID + T".previousState", false );
		m_isInState = TweakDBInterface.GetBool( recordID + T".isInState", false );
		m_skipWhenApplied = TweakDBInterface.GetBool( recordID + T".skipWhenApplied", false );
	}

	protected export const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var owner : weak< GameObject >;
		var castedState : NPCStatePrereqState;
		if( m_skipWhenApplied )
		{
			return;
		}
		owner = ( ( GameObject )( context ) );
		if( owner )
		{
			castedState = ( ( NPCStatePrereqState )( state ) );
			castedState.OnChanged( Evaluate( owner, castedState.m_prevValue, castedState.m_prevValue ) );
		}
	}

	protected const final function GetStateName( recordID : TweakDBID ) : String
	{
		return TweakDBInterface.GetString( recordID + T".stateName", "" );
	}

	protected const virtual function GetStateToCheck() : Int32
	{
		return -1;
	}

}

