class AmmoStateHitTriggeredPrereqState extends GenericHitPrereqState
{

	public override function Evaluate( hitEvent : gameHitEvent ) : Bool
	{
		var checkPassed : Bool;
		var maxAmmo : Uint32;
		var currentAmmo : Uint32;
		var weapon : weak< WeaponObject >;
		const var prereq : AmmoStateHitTriggeredPrereq;
		prereq = ( ( AmmoStateHitTriggeredPrereq )( GetPrereq() ) );
		weapon = hitEvent.attackData.GetWeapon();
		if( weapon )
		{
			currentAmmo = WeaponObject.GetMagazineAmmoCount( weapon );
		}
		switch( prereq.m_valueToListen )
		{
			case EMagazineAmmoState.FirstBullet:
				maxAmmo = GameInstance.GetBlackboardSystem( hitEvent.target.GetGame() ).Get( GetAllBlackboardDefs().Weapon ).GetUint( GetAllBlackboardDefs().Weapon.MagazineAmmoCapacity );
			checkPassed = currentAmmo >= maxAmmo;
			break;
			case EMagazineAmmoState.LastBullet:
				checkPassed = currentAmmo <= ( ( Uint32 )( 0 ) );
			break;
			default:
				return false;
		}
		return checkPassed;
	}

}

class AmmoStateHitTriggeredPrereq extends HitTriggeredPrereq
{
	var m_valueToListen : EMagazineAmmoState;

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var owner : weak< GameObject >;
		var castedState : GenericHitPrereqState;
		owner = ( ( GameObject )( context ) );
		castedState = ( ( GenericHitPrereqState )( state ) );
		if( m_callbackType == gameDamageCallbackType.HitTriggered )
		{
			castedState.m_listener = new AmmoStateHitTriggeredCallback;
		}
		else
		{
			return false;
		}
		castedState.m_listener.RegisterState( castedState );
		if( m_isSync )
		{
			GameInstance.GetDamageSystem( game ).RegisterSyncListener( castedState.m_listener, owner.GetEntityID(), m_callbackType, m_pipelineStage, m_pipelineType );
		}
		else
		{
			GameInstance.GetDamageSystem( game ).RegisterListener( castedState.m_listener, owner.GetEntityID(), m_callbackType, m_pipelineType );
		}
		return false;
	}

	protected override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TweakDBInterface.GetString( recordID + T".ammoState", "" );
		m_valueToListen = ( ( EMagazineAmmoState )( ( ( Int32 )( EnumValueFromString( "EMagazineAmmoState", str ) ) ) ) );
		super.Initialize( recordID );
	}

}

class AmmoStateHitCallback extends HitCallback
{

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

	protected override function UpdateState( hitEvent : gameHitEvent )
	{
		var checkPassed : Bool;
		m_state.SetHitEvent( hitEvent );
		checkPassed = m_state.Evaluate( hitEvent );
		if( checkPassed )
		{
			m_state.OnChangedRepeated( false );
		}
	}

}

class AmmoStateHitTriggeredCallback extends AmmoStateHitCallback
{

	protected override function OnHitTriggered( hitEvent : gameHitEvent )
	{
		UpdateState( hitEvent );
	}

	protected override function OnHitReceived( hitEvent : gameHitEvent ) {}
}

