class GameTimePrereqState extends PrereqState
{
	var m_listener : Uint32;
	var m_repeated : Bool;

	public virtual function UpdatePrereq()
	{
		if( m_repeated )
		{
			OnChangedRepeated( false );
		}
		else
		{
			OnChanged( true );
		}
	}

}

class GameTimePrereq extends IScriptablePrereq
{
	var m_delay : Float;
	var m_repeated : Bool;
	private var m_delayFromStat : gamedataStatType;
	default m_delayFromStat = gamedataStatType.Invalid;

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var evt : DelayPrereqEvent;
		var castedState : GameTimePrereqState;
		var delay : Float;
		var statVal : Float;
		delay = m_delay;
		castedState = ( ( GameTimePrereqState )( state ) );
		evt = new DelayPrereqEvent;
		evt.m_state = castedState;
		castedState.m_repeated = m_repeated;
		if( m_delayFromStat != gamedataStatType.Invalid )
		{
			statVal = GameInstance.GetStatsSystem( game ).GetStatValue( ( ( GameObject )( context ) ).GetEntityID(), m_delayFromStat );
			if( statVal > 0.0 )
			{
				delay = statVal / TDB.GetFloat( T"timeSystem.settings.realTimeMultiplier", 1.0 );
			}
		}
		if( m_repeated )
		{
			castedState.m_listener = GameInstance.GetTimeSystem( game ).RegisterDelayedListener( ( ( GameObject )( context ) ), evt, GameInstance.GetTimeSystem( game ).RealTimeSecondsToGameTime( delay ), -1, false );
		}
		else
		{
			castedState.m_listener = GameInstance.GetTimeSystem( game ).RegisterDelayedListener( ( ( GameObject )( context ) ), evt, GameInstance.GetTimeSystem( game ).RealTimeSecondsToGameTime( delay ), 1, false );
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		GameInstance.GetTimeSystem( game ).UnregisterListener( ( ( GameTimePrereqState )( state ) ).m_listener );
	}

	protected override function Initialize( recordID : TweakDBID )
	{
		var statRecord : Stat_Record;
		m_delay = TDB.GetFloat( recordID + T".delay", 0.0 );
		m_repeated = TDB.GetBool( recordID + T".repeated", false );
		statRecord = TDB.GetStatRecord( TDB.GetForeignKey( recordID + T".delayFromStat" ) );
		if( statRecord )
		{
			m_delayFromStat = statRecord.StatType();
		}
	}

}

class DelayPrereqEvent extends Event
{
	var m_state : GameTimePrereqState;
}

