class WeaponFirstReloadStatePrereqState extends PrereqState
{
	var m_listenerWeaponInt : CallbackHandle;
	var m_listenerActiveWeaponVariant : CallbackHandle;
	var m_weaponObj : weak< WeaponObject >;
	var m_owner : weak< GameObject >;
	var m_reloaded : Bool;

	public function CheckIfPlayerWeaponChanged()
	{
		var weaponObj : WeaponObject;
		weaponObj = ScriptedPuppet.GetActiveWeapon( m_owner );
		if( weaponObj != m_weaponObj )
		{
			m_weaponObj = weaponObj;
			m_reloaded = false;
		}
	}

	protected event OnWeaponStateUpdate( value : Int32 )
	{
		CheckIfPlayerWeaponChanged();
		if( ( ( gamePSMRangedWeaponStates )( value ) ) == gamePSMRangedWeaponStates.Reload )
		{
			m_reloaded = true;
		}
		OnChanged( !( m_reloaded ) );
	}

	protected event OnInventoryChangeStateUpdate( value : Variant )
	{
		CheckIfPlayerWeaponChanged();
		OnChanged( !( m_reloaded ) );
	}

}

class WeaponFirstReloadStatePrereq extends IScriptablePrereq
{

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

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

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

}

