class InventoryItemDisplay extends BaseButtonView
{
	protected editable var m_RarityRoot : inkWidgetRef;
	protected editable var m_ModsRoot : inkCompoundRef;
	protected editable var m_RarityWrapper : inkWidgetRef;
	protected editable var m_IconImage : inkImageRef;
	protected editable var m_IconShadowImage : inkImageRef;
	protected editable var m_IconFallback : inkImageRef;
	protected editable var m_BackgroundShape : inkImageRef;
	protected editable var m_BackgroundHighlight : inkImageRef;
	protected editable var m_BackgroundFrame : inkImageRef;
	protected editable var m_QuantityText : inkTextRef;
	protected editable var m_ModName : CName;
	protected editable var m_toggleHighlight : inkWidgetRef;
	protected editable var m_equippedIcon : inkWidgetRef;
	protected editable var m_DefaultCategoryIconName : String;
	default m_DefaultCategoryIconName = "undefined";
	protected var m_ItemData : InventoryItemData;
	protected var m_AttachementsDisplay : array< weak< InventoryItemAttachmentDisplay > >;
	protected var m_smallSize : Vector2;
	protected var m_bigSize : Vector2;
	private var owner : weak< GameObject >;

	protected event OnInitialize()
	{
		super.OnInitialize();
		GetRootWidget().SetAnchor( inkEAnchor.TopLeft );
		m_smallSize.X = 174.0;
		m_smallSize.Y = 177.0;
		m_bigSize.X = 399.0;
		m_bigSize.Y = 177.0;
		if( m_ButtonController )
		{
			m_ButtonController.RegisterToCallback( 'OnButtonClick', this, 'OnButtonClick' );
		}
		RefreshUI();
		if( inkWidgetRef.IsValid( m_toggleHighlight ) )
		{
			inkWidgetRef.SetVisible( m_toggleHighlight, false );
		}
	}

	public virtual function Setup( const itemData : ref< InventoryItemData >, optional ownerEntity : Entity )
	{
		owner = ( ( GameObject )( ownerEntity ) );
		if( ( InventoryItemData.GetID( m_ItemData ) != InventoryItemData.GetID( itemData ) ) || ( InventoryItemData.GetQuantity( m_ItemData ) != InventoryItemData.GetQuantity( itemData ) ) )
		{
			m_ItemData = itemData;
			RefreshUI();
		}
	}

	protected virtual function RefreshUI()
	{
		SetItemSize();
		SetRarity( InventoryItemData.GetQuality( m_ItemData ) );
		SetQuantity( InventoryItemData.GetQuantity( m_ItemData ) );
		SetShape( InventoryItemData.GetItemShape( m_ItemData ) );
		ShowMods( InventoryItemData.GetAttachments( m_ItemData ) );
		SetEquippedState( InventoryItemData.IsEquipped( m_ItemData ) );
		UpdateIcon();
	}

	protected virtual function SetItemSize()
	{
		var area : gamedataEquipmentArea;
		area = InventoryItemData.GetEquipmentArea( m_ItemData );
		if( ( ( area == gamedataEquipmentArea.Weapon || area == gamedataEquipmentArea.SystemReplacementCW ) || area == gamedataEquipmentArea.ArmsCW ) || area == gamedataEquipmentArea.HandsCW )
		{
			InventoryItemData.SetItemShape( m_ItemData, EInventoryItemShape.DoubleSlot );
		}
	}

	protected virtual function SetRarity( quality : CName )
	{
		inkWidgetRef.SetState( m_RarityRoot, quality );
		if( quality == '' )
		{
			inkWidgetRef.SetVisible( m_RarityWrapper, false );
		}
		else
		{
			inkWidgetRef.SetVisible( m_RarityWrapper, true );
		}
	}

	protected virtual function SetQuantity( itemQuantity : Int32 )
	{
		var ammoCount : Int32;
		var itemRecord : Item_Record;
		var ammoQuery : ItemID;
		var weaponRecord : WeaponItem_Record;
		var category : gamedataItemCategory;
		var transSystem : TransactionSystem;
		ammoCount = 0;
		if( owner != NULL )
		{
			transSystem = GameInstance.GetTransactionSystem( owner.GetGame() );
			itemRecord = TweakDBInterface.GetItemRecord( ItemID.GetTDBID( InventoryItemData.GetID( m_ItemData ) ) );
			category = itemRecord.ItemCategory().Type();
			if( category == gamedataItemCategory.Weapon )
			{
				weaponRecord = ( ( WeaponItem_Record )( itemRecord ) );
				ammoQuery = ItemID.CreateQuery( weaponRecord.Ammo().GetID() );
				ammoCount = transSystem.GetItemQuantity( owner, ammoQuery );
			}
		}
		if( ( itemQuantity > 1 ) || InventoryItemData.GetItemType( m_ItemData ) == gamedataItemType.Con_Ammo )
		{
			inkTextRef.SetText( m_QuantityText, "x" + IntToString( itemQuantity ) );
			inkTextRef.SetVisible( m_QuantityText, true );
		}
		else
		{
			inkTextRef.SetVisible( m_QuantityText, false );
		}
		if( ammoCount > 0 )
		{
			inkTextRef.SetText( m_QuantityText, ( GetLocalizedText( "UI-ScriptExports-Ammo0" ) + ": " ) + IntToString( ammoCount ) );
			inkTextRef.SetVisible( m_QuantityText, true );
		}
	}

	protected virtual function GetShapeSize( shapeType : EInventoryItemShape ) : Vector2
	{
		var newSize : Vector2;
		switch( shapeType )
		{
			case EInventoryItemShape.SingleSlot:
				newSize = m_smallSize;
			break;
			case EInventoryItemShape.DoubleSlot:
				newSize = m_bigSize;
			break;
		}
		return newSize;
	}

	protected virtual function SetShape( shapeType : EInventoryItemShape )
	{
		var newSize : Vector2;
		newSize = GetShapeSize( shapeType );
		inkImageRef.SetSize( m_BackgroundShape, newSize );
		inkImageRef.SetSize( m_BackgroundHighlight, newSize );
		inkImageRef.SetSize( m_BackgroundFrame, newSize );
		if( inkWidgetRef.IsValid( m_toggleHighlight ) )
		{
			inkWidgetRef.SetSize( m_toggleHighlight, newSize );
		}
		if( InventoryItemData.IsEmpty( m_ItemData ) )
		{
			inkImageRef.SetTexturePart( m_BackgroundFrame, 'item_grid_frame' );
		}
		else
		{
			inkImageRef.SetTexturePart( m_BackgroundFrame, 'item_frame' );
		}
	}

	protected virtual function UpdateIcon()
	{
		var iconNameStr : String;
		var iconName : CName;
		var categoryName : CName;
		if( InventoryItemData.IsEmpty( m_ItemData ) )
		{
			inkImageRef.SetVisible( m_IconImage, false );
			inkImageRef.SetVisible( m_IconFallback, false );
			return;
		}
		iconNameStr = InventoryItemData.GetIconPath( m_ItemData );
		iconName = StringToName( iconNameStr );
		if( !( IsStringValid( iconNameStr ) ) || !( inkImageRef.IsTexturePartExist( m_IconImage, iconName ) ) )
		{
			inkImageRef.SetVisible( m_IconImage, false );
			inkImageRef.SetVisible( m_IconFallback, true );
			categoryName = StringToName( InventoryItemData.GetCategoryName( m_ItemData ) );
			if( inkImageRef.IsTexturePartExist( m_IconFallback, categoryName ) )
			{
				inkImageRef.SetTexturePart( m_IconFallback, categoryName );
			}
			else
			{
				inkImageRef.SetTexturePart( m_IconFallback, StringToName( m_DefaultCategoryIconName ) );
			}
		}
		else
		{
			inkImageRef.SetVisible( m_IconFallback, false );
			inkImageRef.SetVisible( m_IconImage, true );
			inkImageRef.SetTexturePart( m_IconImage, iconName );
		}
	}

	protected virtual function ShowMods( const attachements : ref< array< InventoryItemAttachments > > )
	{
		var currentItem : weak< InventoryItemAttachmentDisplay >;
		var i, count : Int32;
		if( !( inkCompoundRef.IsValid( m_ModsRoot ) ) || !( IsNameValid( m_ModName ) ) )
		{
			return;
		}
		count = attachements.Size();
		while( m_AttachementsDisplay.Size() > count )
		{
			currentItem = m_AttachementsDisplay.PopBack();
			inkCompoundRef.RemoveChild( m_ModsRoot, currentItem.GetRootWidget() );
		}
		while( m_AttachementsDisplay.Size() < count )
		{
			currentItem = ( ( InventoryItemAttachmentDisplay )( SpawnFromLocal( inkCompoundRef.Get( m_ModsRoot ), m_ModName ).GetController() ) );
			m_AttachementsDisplay.PushBack( currentItem );
		}
		for( i = 0; i < count; i += 1 )
		{
			m_AttachementsDisplay[ i ].Setup( attachements[ i ].ItemData );
		}
	}

	public function SetEquippedState( equipped : Bool )
	{
		if( inkWidgetRef.IsValid( m_equippedIcon ) )
		{
			if( equipped )
			{
				inkWidgetRef.SetVisible( m_equippedIcon, true );
			}
			else
			{
				inkWidgetRef.SetVisible( m_equippedIcon, false );
			}
		}
	}

	public function SelectItem()
	{
		if( inkWidgetRef.IsValid( m_toggleHighlight ) )
		{
			inkWidgetRef.SetVisible( m_toggleHighlight, true );
			InventoryItemData.SetIsAvailable( m_ItemData, false );
		}
	}

	public function UnselectItem()
	{
		if( inkWidgetRef.IsValid( m_toggleHighlight ) )
		{
			inkWidgetRef.SetVisible( m_toggleHighlight, false );
			InventoryItemData.SetIsAvailable( m_ItemData, true );
		}
	}

	protected override function ButtonStateChanged( oldState : inkEButtonState, newState : inkEButtonState )
	{
		if( newState == inkEButtonState.Hover )
		{
			CallCustomCallback( 'OnRequestTooltip' );
			CallCustomCallback( 'OnItemHoverOver' );
		}
		else if( newState == inkEButtonState.Normal )
		{
			CallCustomCallback( 'OnDismissTooltip' );
			CallCustomCallback( 'OnItemHoverOut' );
		}
	}

	protected event OnButtonClick( controller : weak< inkButtonController > )
	{
		CallCustomCallback( 'OnClick' );
	}

	public function Mark( index : Int32 )
	{
		var i, limit : Int32;
		for( i = 0, limit = m_AttachementsDisplay.Size(); i < limit; i += 1 )
		{
			m_AttachementsDisplay[ i ].Mark( index == i );
		}
	}

	public function PlayIntroAnimation( delay : Float, duration : Float ) {}

	public function GetItemData() : InventoryItemData
	{
		return m_ItemData;
	}

	public function GetWidgetForTooltip() : weak< inkWidget >
	{
		return inkWidgetRef.Get( m_RarityRoot );
	}

}

class InventoryItemModSlotDisplay extends inkLogicController
{
	private editable var m_slotBorder : inkWidgetRef;
	private editable var m_slotBackground : inkWidgetRef;

	public function Setup( const inventoryItemData : ref< InventoryItemData > )
	{
		var quality : CName;
		var isEmpty : Bool;
		var itemRecord : weak< Item_Record >;
		isEmpty = InventoryItemData.IsEmpty( inventoryItemData );
		inkWidgetRef.SetVisible( m_slotBackground, !( isEmpty ) );
		inkWidgetRef.SetVisible( m_slotBorder, isEmpty );
		quality = InventoryItemData.GetQuality( inventoryItemData );
		if( !( isEmpty ) )
		{
			itemRecord = TweakDBInterface.GetItemRecord( ItemID.GetTDBID( InventoryItemData.GetID( inventoryItemData ) ) );
			if( ( itemRecord != NULL ) && itemRecord.TagsContains( 'ChimeraMod' ) )
			{
				quality = UIItemsHelper.QualityEnumToName( gamedataQuality.Iconic );
			}
		}
		inkWidgetRef.SetState( m_slotBackground, ( ( IsNameValid( quality ) ) ? ( quality ) : ( 'Empty' ) ) );
	}

	public function Setup( modInfo : UIInventoryItemMod )
	{
		var quality : CName;
		var attachmentInfo : UIInventoryItemModAttachment;
		attachmentInfo = ( ( UIInventoryItemModAttachment )( modInfo ) );
		inkWidgetRef.SetVisible( m_slotBackground, !( attachmentInfo.IsEmpty ) );
		inkWidgetRef.SetVisible( m_slotBorder, attachmentInfo.IsEmpty );
		quality = UIItemsHelper.QualityEnumToName( attachmentInfo.Quality );
		inkWidgetRef.SetState( m_slotBackground, ( ( IsNameValid( quality ) ) ? ( quality ) : ( 'Empty' ) ) );
	}

}

class InventoryItemAttachmentDisplay extends inkLogicController
{
	private editable var m_QualityRootRef : inkWidgetRef;
	private editable var m_ShapeRef : inkWidgetRef;
	private editable var m_BorderRef : inkWidgetRef;
	private editable var m_MarkedStateName : CName;
	default m_MarkedStateName = 'Marked';

	public function Setup( const itemData : ref< InventoryItemData > )
	{
		Setup( !( InventoryItemData.IsEmpty( itemData ) ), InventoryItemData.GetQuality( itemData ) );
	}

	public function Setup( visible : Bool, quality : CName )
	{
		inkWidgetRef.SetVisible( m_ShapeRef, visible );
		if( inkWidgetRef.IsValid( m_QualityRootRef ) )
		{
			inkWidgetRef.SetState( m_QualityRootRef, quality );
		}
		Mark( visible );
	}

	public function Mark( marked : Bool )
	{
		var stateName : CName;
		stateName = ( ( marked ) ? ( m_MarkedStateName ) : ( inkWidget.DefaultState() ) );
		inkWidgetRef.SetState( m_ShapeRef, stateName );
	}

}

