class PlayerStateMachinePrereqState 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 : PlayerStateMachinePrereq;
		prereq = ( ( PlayerStateMachinePrereq )( GetPrereq() ) );
		checkPassed = prereq.Evaluate( m_owner, value, m_prevValue );
		m_prevValue = value;
		OnChanged( checkPassed );
	}

}

class PlayerStateMachinePrereq extends IScriptablePrereq
{
	private var m_previousState : Bool;
	private var m_isInState : Bool;
	private var m_skipWhenApplied : Bool;
	var m_valueToListen : Int32;

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

	protected export override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TweakDBInterface.GetString( recordID + T".stateName", "" );
		m_valueToListen = ( ( Int32 )( EnumValueFromString( GetStateMachineEnum(), str ) ) );
		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 const virtual function GetStateMachineEnum() : String
	{
		return "";
	}

	protected const virtual function GetCurrentPSMStateIndex( bb : IBlackboard ) : Int32
	{
		return 0;
	}

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

	public export const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var owner : weak< GameObject >;
		var checkPassed : Bool;
		owner = ( ( GameObject )( context ) );
		bb = GameInstance.GetBlackboardSystem( owner.GetGame() ).GetLocalInstanced( ( ( ScriptedPuppet )( context ) ).GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		if( !( bb ) )
		{
			return false;
		}
		checkPassed = Evaluate( owner, GetCurrentPSMStateIndex( bb ), GetCurrentPSMStateIndex( bb ) );
		return checkPassed;
	}

}

class WeaponStateMachinePrereqState extends PrereqState
{
}

class WeaponStateMachinePrereq extends IScriptablePrereq
{
}

