class WeaponShootPrereqState extends PrereqState
{
	var m_listenerWeaponInt : CallbackHandle;
	var m_listenerActiveWeaponVariant : CallbackHandle;
	var m_listenerOnShootVariant : CallbackHandle;
	var m_weaponObj : weak< WeaponObject >;
	var m_owner : weak< GameObject >;
	var m_howManyAttacks : Int32;
	var m_remainingAttacks : Int32;

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

	protected event OnWeaponStateUpdate( value : Int32 )
	{
		var checkPassed : Bool;
		const var prereq : WeaponShootPrereq;
		prereq = ( ( WeaponShootPrereq )( GetPrereq() ) );
		CheckIfPlayerWeaponChanged();
		if( ( ( gamePSMRangedWeaponStates )( value ) ) == gamePSMRangedWeaponStates.Reload )
		{
			m_remainingAttacks = m_howManyAttacks;
		}
		checkPassed = prereq.Evaluate( m_owner, m_remainingAttacks );
		OnChanged( checkPassed );
	}

	protected event OnInventoryChangeStateUpdate( value : Variant )
	{
		var checkPassed : Bool;
		const var prereq : WeaponShootPrereq;
		prereq = ( ( WeaponShootPrereq )( GetPrereq() ) );
		CheckIfPlayerWeaponChanged();
		checkPassed = prereq.Evaluate( m_owner, m_remainingAttacks );
		OnChanged( checkPassed );
	}

	protected event OnShoot( value : Variant )
	{
		var checkPassed : Bool;
		const var prereq : WeaponShootPrereq;
		prereq = ( ( WeaponShootPrereq )( GetPrereq() ) );
		CheckIfPlayerWeaponChanged();
		m_remainingAttacks -= 1;
		checkPassed = prereq.Evaluate( m_owner, m_remainingAttacks );
		OnChanged( checkPassed );
	}

}

class WeaponShootPrereq extends IScriptablePrereq
{
	var m_howManyAttacks : Int32;

	protected export override function Initialize( recordID : TweakDBID )
	{
		m_howManyAttacks = TweakDBInterface.GetInt( recordID + T".howManyShots", 0 );
	}

	public const function Evaluate( owner : GameObject, remainingAttacks : Int32 ) : Bool
	{
		if( remainingAttacks < 0 )
		{
			return false;
		}
		return true;
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var bb1 : IBlackboard;
		var bb2 : IBlackboard;
		var bb3 : IBlackboard;
		var castedState : WeaponShootPrereqState;
		var player : PlayerPuppet;
		castedState = ( ( WeaponShootPrereqState )( state ) );
		castedState.m_howManyAttacks = m_howManyAttacks;
		castedState.m_owner = ( ( GameObject )( context ) );
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			bb1 = player.GetPlayerStateMachineBlackboard();
			castedState.m_listenerWeaponInt = bb1.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' );
			bb3 = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().UI_ActiveWeaponData );
			castedState.m_listenerOnShootVariant = bb3.RegisterListenerVariant( GetAllBlackboardDefs().UI_ActiveWeaponData.ShootEvent, castedState, 'OnShoot' );
		}
		return false;
	}

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

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

}

