class FactValuePrereqState extends PrereqState
{
	var m_listenerID : Uint32;

	public function OnFactChanged( factValue : Int32 )
	{
		const var prereq : FactValuePrereq;
		var evaluation : Bool;
		prereq = ( ( FactValuePrereq )( GetPrereq() ) );
		evaluation = prereq.Evaluate( factValue );
		if( ( prereq.m_repeated && IsFulfilled() ) && evaluation )
		{
			OnChangedRepeated( false );
		}
		else
		{
			OnChanged( evaluation );
		}
	}

}

class FactValuePrereq extends IScriptablePrereq
{
	var m_fact : CName;
	var m_value : Int32;
	var m_comparisonType : EComparisonType;
	var m_repeated : Bool;

	protected override function Initialize( recordID : TweakDBID )
	{
		var compType : CName;
		m_fact = TweakDBInterface.GetCName( recordID + T".fact", '' );
		m_value = TweakDBInterface.GetInt( recordID + T".value", 0 );
		compType = TweakDBInterface.GetCName( recordID + T".comparisonType", '' );
		m_comparisonType = ( ( EComparisonType )( ( ( Int32 )( EnumValueFromName( 'EComparisonType', compType ) ) ) ) );
		m_repeated = TweakDBInterface.GetBool( recordID + T".repeated", false );
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var castedState : FactValuePrereqState;
		if( IsNameValid( m_fact ) )
		{
			castedState = ( ( FactValuePrereqState )( state ) );
			castedState.m_listenerID = GameInstance.GetQuestsSystem( game ).RegisterListener( m_fact, castedState, 'OnFactChanged' );
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var castedState : FactValuePrereqState;
		if( IsNameValid( m_fact ) )
		{
			castedState = ( ( FactValuePrereqState )( state ) );
			GameInstance.GetQuestsSystem( game ).UnregisterListener( m_fact, castedState.m_listenerID );
		}
	}

	protected const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var castedState : FactValuePrereqState;
		castedState = ( ( FactValuePrereqState )( state ) );
		castedState.OnChanged( IsFulfilled( game, context ) );
	}

	public const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var result : Bool;
		result = false;
		if( IsNameValid( m_fact ) )
		{
			result = Evaluate( GameInstance.GetQuestsSystem( game ).GetFact( m_fact ) );
		}
		return result;
	}

	public const function Evaluate( value : Int32 ) : Bool
	{
		return ProcessCompare( m_comparisonType, ( ( Float )( value ) ), ( ( Float )( m_value ) ) );
	}

}

