class ItemSlotsFilledPrereqState extends PrereqState
{
	var m_equipmentBlackboardCallback : CallbackHandle;
	var m_owner : weak< GameObject >;
	var m_equipAreas : array< gamedataEquipmentArea >;

	public function CheckEquipAreaSlots()
	{
		var equipmentSystemPlayerData : EquipmentSystemPlayerData;
		var i, slotCount, itemCount : Int32;
		if( !( m_owner ) )
		{
			return;
		}
		equipmentSystemPlayerData = EquipmentSystem.GetData( m_owner );
		for( i = 0; i < m_equipAreas.Size(); i += 1 )
		{
			slotCount += equipmentSystemPlayerData.GetNumberOfSlots( m_equipAreas[ i ] );
			itemCount += equipmentSystemPlayerData.GetNumberOfItemsInEquipmentArea( m_equipAreas[ i ] );
		}
		OnChanged( slotCount == itemCount );
	}

	protected event OnEquipAreaChanged( value : Int32 )
	{
		if( m_equipAreas.Contains( ( ( gamedataEquipmentArea )( value ) ) ) )
		{
			CheckEquipAreaSlots();
		}
	}

}

class ItemSlotsFilledPrereq extends IScriptablePrereq
{
	var m_equipAreas : array< gamedataEquipmentArea >;

	protected export override function Initialize( recordID : TweakDBID )
	{
		var i : Int32;
		var areas : array< CName >;
		var equipArea : gamedataEquipmentArea;
		areas = TDB.GetCNameArray( recordID + T".equipAreas" );
		for( i = 0; i < areas.Size(); i += 1 )
		{
			equipArea = ( ( gamedataEquipmentArea )( ( ( Int32 )( EnumValueFromName( 'gamedataEquipmentArea', areas[ i ] ) ) ) ) );
			m_equipAreas.PushBack( equipArea );
		}
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var equipmentBlackboard : weak< IBlackboard >;
		var castedState : ItemSlotsFilledPrereqState;
		var player : GameObject;
		castedState = ( ( ItemSlotsFilledPrereqState )( state ) );
		player = ( ( GameObject )( context ) );
		if( player )
		{
			castedState.m_owner = player;
			equipmentBlackboard = GameInstance.GetBlackboardSystem( player.GetGame() ).Get( GetAllBlackboardDefs().UI_Equipment );
			castedState.m_equipmentBlackboardCallback = equipmentBlackboard.RegisterListenerInt( GetAllBlackboardDefs().UI_Equipment.areaChanged, castedState, 'OnEquipAreaChanged' );
			castedState.m_equipAreas = m_equipAreas;
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var equipmentBlackboard : weak< IBlackboard >;
		var castedState : ItemSlotsFilledPrereqState;
		var player : GameObject;
		castedState = ( ( ItemSlotsFilledPrereqState )( state ) );
		player = ( ( GameObject )( context ) );
		if( player )
		{
			equipmentBlackboard = GameInstance.GetBlackboardSystem( player.GetGame() ).Get( GetAllBlackboardDefs().UI_Equipment );
			if( castedState.m_equipmentBlackboardCallback )
			{
				equipmentBlackboard.UnregisterListenerVariant( GetAllBlackboardDefs().UI_Equipment.itemEquipped, castedState.m_equipmentBlackboardCallback );
			}
		}
	}

	protected const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var castedState : ItemSlotsFilledPrereqState;
		castedState = ( ( ItemSlotsFilledPrereqState )( state ) );
		castedState.CheckEquipAreaSlots();
	}

}

