abstract class AIItemHandlingCondition extends AIbehaviorconditionScript
{
}

class CheckUnregisteredWeapon extends AIItemHandlingCondition
{
	private var m_primaryItemArrayRecordTweakDBID : array< TweakDBID >;
	private var m_secondaryItemArrayRecordTweakDBID : array< TweakDBID >;
	private var m_transactionSystem : TransactionSystem;
	private var m_puppet : weak< ScriptedPuppet >;
	private var m_initialized : Bool;

	protected export override function Activate( context : ScriptExecutionContext )
	{
		var characterRecord : weak< Character_Record >;
		var itemsRecordArray : array< weak< NPCEquipmentItem_Record > >;
		var itemInRecordItemID : ItemID;
		var emptyItemID : ItemID;
		var itemInRecordTweakDBID : TweakDBID;
		var emptyTweakDBID : TweakDBID;
		var i, itemsCount : Int32;
		SetUpdateInterval( context, RandomizeOffsetForUpdateInterval( 3.0 ) );
		if( m_initialized )
		{
			return;
		}
		m_initialized = true;
		m_puppet = GetPuppet( context );
		if( !( m_puppet ) )
		{
			return;
		}
		characterRecord = TweakDBInterface.GetCharacterRecord( m_puppet.GetRecordID() );
		AIActionTransactionSystem.CalculateEquipmentItems( m_puppet, characterRecord.PrimaryEquipment(), itemsRecordArray, -1 );
		itemsCount = itemsRecordArray.Size();
		m_primaryItemArrayRecordTweakDBID.Resize( itemsCount );
		if( itemsCount > 0 )
		{
			for( i = 0; i < itemsCount; i += 1 )
			{
				AIActionTransactionSystem.GetItemID( m_puppet, itemsRecordArray[ i ].Item(), itemsRecordArray[ i ].OnBodySlot().GetID(), itemInRecordItemID );
				itemInRecordTweakDBID = ItemID.GetTDBID( itemInRecordItemID );
				m_primaryItemArrayRecordTweakDBID[ i ] = itemInRecordTweakDBID;
				itemInRecordItemID = emptyItemID;
				itemInRecordTweakDBID = emptyTweakDBID;
			}
			itemsRecordArray.Clear();
		}
		AIActionTransactionSystem.CalculateEquipmentItems( m_puppet, characterRecord.SecondaryEquipment(), itemsRecordArray, -1 );
		itemsCount = itemsRecordArray.Size();
		m_secondaryItemArrayRecordTweakDBID.Resize( itemsCount );
		if( itemsCount > 0 )
		{
			for( i = 0; i < itemsCount; i += 1 )
			{
				AIActionTransactionSystem.GetItemID( m_puppet, itemsRecordArray[ i ].Item(), itemsRecordArray[ i ].OnBodySlot().GetID(), itemInRecordItemID );
				itemInRecordTweakDBID = ItemID.GetTDBID( itemInRecordItemID );
				m_secondaryItemArrayRecordTweakDBID[ i ] = itemInRecordTweakDBID;
				itemInRecordItemID = emptyItemID;
				itemInRecordTweakDBID = emptyTweakDBID;
			}
		}
		m_transactionSystem = GameInstance.GetTransactionSystem( GetPuppet( context ).GetGame() );
	}

	protected export override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var itemInCurrentLeftSlot : weak< ItemObject >;
		var itemInCurrentRightSlot : weak< ItemObject >;
		var emptyTweakDBID : TweakDBID;
		var itemInCurrentLeftSlotTweakDBID : TweakDBID;
		var itemInCurrentRightSlotTweakDBID : TweakDBID;
		var i, itemsCount : Int32;
		itemInCurrentRightSlot = m_transactionSystem.GetItemInSlot( m_puppet, T"AttachmentSlots.WeaponRight" );
		itemInCurrentLeftSlot = m_transactionSystem.GetItemInSlot( m_puppet, T"AttachmentSlots.WeaponLeft" );
		if( !( itemInCurrentRightSlot ) && !( itemInCurrentLeftSlot ) )
		{
			return false;
		}
		itemInCurrentRightSlotTweakDBID = ItemID.GetTDBID( itemInCurrentRightSlot.GetItemID() );
		itemInCurrentLeftSlotTweakDBID = ItemID.GetTDBID( itemInCurrentLeftSlot.GetItemID() );
		itemsCount = m_primaryItemArrayRecordTweakDBID.Size();
		if( itemsCount > 0 )
		{
			for( i = 0; i < itemsCount; i += 1 )
			{
				if( m_primaryItemArrayRecordTweakDBID[ i ] == emptyTweakDBID )
				{
					return false;
				}
				if( m_primaryItemArrayRecordTweakDBID[ i ] == itemInCurrentLeftSlotTweakDBID )
				{
					return false;
				}
				if( m_primaryItemArrayRecordTweakDBID[ i ] == itemInCurrentRightSlotTweakDBID )
				{
					return false;
				}
			}
		}
		itemsCount = m_secondaryItemArrayRecordTweakDBID.Size();
		if( itemsCount > 0 )
		{
			for( i = 0; i < itemsCount; i += 1 )
			{
				if( m_secondaryItemArrayRecordTweakDBID[ i ] == emptyTweakDBID )
				{
					return false;
				}
				if( m_secondaryItemArrayRecordTweakDBID[ i ] == itemInCurrentLeftSlotTweakDBID )
				{
					return false;
				}
				if( m_secondaryItemArrayRecordTweakDBID[ i ] == itemInCurrentRightSlotTweakDBID )
				{
					return false;
				}
			}
		}
		return true;
	}

}

class CheckEquippedWeapon extends AIItemHandlingCondition
{
	editable inlined var m_slotID : AIArgumentMapping;
	editable inlined var m_itemID : AIArgumentMapping;
	protected var m_slotIDName : TweakDBID;
	protected var m_itemIDName : TweakDBID;

	protected export override function Activate( context : ScriptExecutionContext )
	{
		SetUpdateInterval( context, RandomizeOffsetForUpdateInterval( 0.2 ) );
	}

	protected export override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var owner : GameObject;
		owner = GetPuppet( context );
		if( owner )
		{
			if( m_slotID && !( TDBID.IsValid( m_slotIDName ) ) )
			{
				m_slotIDName = ScriptExecutionContext.GetTweakDBIDMappingValue( context, m_slotID );
			}
			if( m_itemID && !( TDBID.IsValid( m_itemIDName ) ) )
			{
				m_itemIDName = ScriptExecutionContext.GetTweakDBIDMappingValue( context, m_itemID );
			}
			ScriptExecutionContext.DebugLog( context, 'script', ( ( "SLOT ID: " + TDBID.ToStringDEBUG( m_slotIDName ) ) + ",  ITEM ID: " ) + TDBID.ToStringDEBUG( m_itemIDName ) );
			return GameInstance.GetTransactionSystem( owner.GetGame() ).HasItemInSlot( owner, m_slotIDName, ItemID.CreateQuery( m_itemIDName ) );
		}
		return false;
	}

}

class CheckEquippedWeaponType extends AIItemHandlingCondition
{
	editable var m_weaponTypeToCheck : CName;

	protected override function Activate( context : ScriptExecutionContext )
	{
		SetUpdateInterval( context, RandomizeOffsetForUpdateInterval( 0.2 ) );
	}

	protected override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var owner : GameObject;
		var itemTypeRecordData : CName;
		var item : ItemObject;
		owner = GetPuppet( context );
		if( owner )
		{
			item = ( ( ItemObject )( GameInstance.GetTransactionSystem( owner.GetGame() ).GetItemInSlot( owner, T"AttachmentSlots.WeaponRight" ) ) );
			itemTypeRecordData = TweakDBInterface.GetItemRecord( ItemID.GetTDBID( item.GetItemID() ) ).ItemType().Name();
			return m_weaponTypeToCheck == itemTypeRecordData;
		}
		return false;
	}

}

