class StatPoolSpentPrereqListener extends BaseStatPoolPrereqListener
{
	protected var m_state : weak< StatPoolSpentPrereqState >;
	protected var m_overallSpentValue : Float;

	public export override function OnStatPoolValueChanged( oldValue : Float, newValue : Float, percToPoints : Float )
	{
		var spent : Float;
		spent = ( oldValue * percToPoints ) - ( newValue * percToPoints );
		if( spent > 0.0 )
		{
			m_overallSpentValue += spent;
			if( m_overallSpentValue >= m_state.GetThreshold() )
			{
				m_state.OnChangedRepeated();
				m_overallSpentValue = 0.0;
			}
		}
	}

	public override function RegisterState( state : PrereqState )
	{
		m_state = ( ( StatPoolSpentPrereqState )( state ) );
	}

}

class StatPoolSpentPrereqState extends PrereqState
{
	var m_neededValue : Float;
	var m_listener : BaseStatPoolPrereqListener;

	public const function GetThreshold() : Float
	{
		return m_neededValue;
	}

	public function SetThreshold( v : Float )
	{
		m_neededValue = v;
	}

}

class StatPoolSpentPrereq extends IScriptablePrereq
{
	var m_statPoolType : gamedataStatPoolType;
	var m_valueToCheck : Float;

	protected export const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var owner : GameObject;
		var castedState : StatPoolSpentPrereqState;
		owner = ( ( GameObject )( context ) );
		castedState = ( ( StatPoolSpentPrereqState )( state ) );
		castedState.SetThreshold( m_valueToCheck );
		castedState.m_listener = new StatPoolSpentPrereqListener;
		castedState.m_listener.RegisterState( castedState );
		castedState.m_listener.SetValue( m_valueToCheck );
		GameInstance.GetStatPoolsSystem( game ).RequestRegisteringListener( owner.GetEntityID(), m_statPoolType, castedState.m_listener );
		return false;
	}

	protected export const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var owner : GameObject;
		var castedState : StatPoolSpentPrereqState;
		owner = ( ( GameObject )( context ) );
		castedState = ( ( StatPoolSpentPrereqState )( state ) );
		GameInstance.GetStatPoolsSystem( game ).RequestUnregisteringListener( owner.GetEntityID(), m_statPoolType, castedState.m_listener );
		castedState.m_listener = NULL;
	}

	protected export override function Initialize( recordID : TweakDBID )
	{
		m_statPoolType = ( ( gamedataStatPoolType )( ( ( Int32 )( EnumValueFromName( 'gamedataStatPoolType', TweakDBInterface.GetCName( recordID + T".statPoolType", '' ) ) ) ) ) );
		m_valueToCheck = TweakDBInterface.GetFloat( recordID + T".valueToCheck", 0.0 );
	}

}

