class ReloadFromEmptyPrereqState extends PrereqState
{
	var m_owner : weak< GameObject >;
	var m_minAmountOfAmmoReloaded : Int32;
	var m_listenerWeaponInt : CallbackHandle;
	var m_listenerActiveWeaponVariant : CallbackHandle;
	private var m_reloadingInProgress : Bool;

	protected event OnWeaponStateUpdate( value : Int32 )
	{
		var weaponObj : WeaponObject;
		if( ( ( gamePSMRangedWeaponStates )( value ) ) == gamePSMRangedWeaponStates.Reload )
		{
			weaponObj = ScriptedPuppet.GetActiveWeapon( m_owner );
			if( WeaponObject.GetMagazineAmmoCount( weaponObj ) == 0 )
			{
				m_reloadingInProgress = true;
			}
			OnChanged( false );
		}
		else if( m_reloadingInProgress )
		{
			m_reloadingInProgress = false;
			weaponObj = ScriptedPuppet.GetActiveWeapon( m_owner );
			OnChanged( ( ( Int32 )( WeaponObject.GetMagazineAmmoCount( weaponObj ) ) ) >= m_minAmountOfAmmoReloaded );
		}
	}

	protected event OnInventoryChangeStateUpdate( value : Variant )
	{
		m_reloadingInProgress = false;
		OnChanged( false );
	}

}

class ReloadFromEmptyPrereq extends IScriptablePrereq
{
	private var m_minAmountOfAmmoReloaded : Int32;

	protected export override function Initialize( recordID : TweakDBID )
	{
		m_minAmountOfAmmoReloaded = TDB.GetInt( recordID + T".minAmountOfAmmoReloaded" );
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var castedState : ReloadFromEmptyPrereqState;
		var player : PlayerPuppet;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			castedState = ( ( ReloadFromEmptyPrereqState )( state ) );
			castedState.m_owner = ( ( GameObject )( context ) );
			castedState.m_minAmountOfAmmoReloaded = m_minAmountOfAmmoReloaded;
			bb = player.GetPlayerStateMachineBlackboard();
			castedState.m_listenerWeaponInt = bb.RegisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.Weapon, castedState, 'OnWeaponStateUpdate' );
			bb = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().UI_ActiveWeaponData );
			castedState.m_listenerActiveWeaponVariant = bb.RegisterListenerVariant( GetAllBlackboardDefs().UI_ActiveWeaponData.WeaponRecordID, castedState, 'OnInventoryChangeStateUpdate' );
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var bb : IBlackboard;
		var castedState : ReloadFromEmptyPrereqState;
		var player : PlayerPuppet;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			castedState = ( ( ReloadFromEmptyPrereqState )( state ) );
			bb = player.GetPlayerStateMachineBlackboard();
			if( castedState.m_listenerWeaponInt )
			{
				bb.UnregisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.Weapon, castedState.m_listenerWeaponInt );
			}
			bb = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().UI_ActiveWeaponData );
			if( castedState.m_listenerActiveWeaponVariant )
			{
				bb.UnregisterListenerVariant( GetAllBlackboardDefs().UI_ActiveWeaponData.WeaponRecordID, castedState.m_listenerActiveWeaponVariant );
			}
		}
	}

	protected const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var player : PlayerPuppet;
		var castedState : ReloadFromEmptyPrereqState;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			castedState = ( ( ReloadFromEmptyPrereqState )( state ) );
			castedState.OnChanged( false );
		}
	}

}

