class TooltipSpecialAbilityList extends inkLogicController
{
	private editable var m_libraryItemName : CName;
	private editable var m_container : inkCompoundRef;
	private var m_itemsList : array< weak< inkWidget > >;
	private var m_data : array< InventoryItemAbility >;
	private var m_qualityName : CName;

	public function SetData( qualityState : CName, const data : ref< array< InventoryItemAbility > > )
	{
		m_qualityName = qualityState;
		m_data = data;
		UpdateLayout();
	}

	public function ClearData( toLeave : Int32 )
	{
		var statItem : weak< inkWidget >;
		while( m_itemsList.Size() > toLeave )
		{
			statItem = m_itemsList.PopBack();
			inkCompoundRef.RemoveChild( m_container, statItem );
		}
	}

	private function UpdateLayout()
	{
		var i, dataCount : Int32;
		var widget : weak< inkWidget >;
		var abilityItem : weak< TooltipSpecialAbilityDisplay >;
		dataCount = m_data.Size();
		ClearData( dataCount );
		while( m_itemsList.Size() < dataCount )
		{
			widget = SpawnFromLocal( inkCompoundRef.Get( m_container ), m_libraryItemName );
			widget.SetStyle( inkCompoundRef.GetStylePath( m_container ) );
			m_itemsList.PushBack( widget );
		}
		for( i = 0; i < dataCount; i += 1 )
		{
			widget = m_itemsList[ i ];
			abilityItem = ( ( TooltipSpecialAbilityDisplay )( widget.GetController() ) );
			abilityItem.Setup( m_qualityName, m_data[ i ] );
		}
	}

	public function UpdateVisibility( force : Bool )
	{
		GetRootWidget().SetVisible( force );
	}

	public function UpdateVisibility()
	{
		GetRootWidget().SetVisible( m_itemsList.Size() > 0 );
	}

}

class TooltipSpecialAbilityDisplay extends inkLogicController
{
	private editable var m_AbilityIcon : inkImageRef;
	private editable var m_AbilityDescription : inkTextRef;
	private editable var m_QualityRoot : inkWidgetRef;

	public function Setup( qualityName : CName, const data : ref< InventoryItemAbility > )
	{
		inkImageRef.SetTexturePart( m_AbilityIcon, data.IconPath );
		if( inkTextRef.IsValid( m_AbilityDescription ) )
		{
			inkTextRef.SetText( m_AbilityDescription, data.Description );
		}
		if( inkWidgetRef.IsValid( m_QualityRoot ) )
		{
			inkWidgetRef.SetState( m_QualityRoot, qualityName );
		}
	}

}

class InventoryItemAttachmentsList extends inkLogicController
{
	private editable var m_libraryItemName : CName;
	private editable var m_container : inkCompoundRef;
	private var m_itemsList : array< weak< inkWidget > >;
	private var m_data : array< CName >;

	public function SetData( const data : ref< array< CName > > )
	{
		m_data = data;
		UpdateLayout();
	}

	public function ClearData( toLeave : Int32 )
	{
		var statItem : weak< inkWidget >;
		while( m_itemsList.Size() > toLeave )
		{
			statItem = m_itemsList.PopBack();
			inkCompoundRef.RemoveChild( m_container, statItem );
		}
	}

	private function UpdateLayout()
	{
		var i, dataCount : Int32;
		var widget : weak< inkWidget >;
		var attachmentItem : weak< InventoryItemAttachmentDisplay >;
		dataCount = m_data.Size();
		ClearData( dataCount );
		while( m_itemsList.Size() < dataCount )
		{
			widget = SpawnFromLocal( inkCompoundRef.Get( m_container ), m_libraryItemName );
			widget.SetStyle( inkCompoundRef.GetStylePath( m_container ) );
			m_itemsList.PushBack( widget );
		}
		for( i = 0; i < dataCount; i += 1 )
		{
			widget = m_itemsList[ i ];
			attachmentItem = ( ( InventoryItemAttachmentDisplay )( widget.GetController() ) );
			attachmentItem.Setup( IsNameValid( m_data[ i ] ), m_data[ i ] );
		}
	}

	public function UpdateVisibility( force : Bool )
	{
		GetRootWidget().SetVisible( force );
	}

	public function UpdateVisibility()
	{
		GetRootWidget().SetVisible( m_itemsList.Size() > 0 );
	}

}

class InventoryItemStatList extends inkLogicController
{
	editable var m_libraryItemName : CName;
	private var m_container : weak< inkCompoundWidget >;
	private var m_data : array< InventoryTooltipData_StatData >;
	private var m_itemsList : array< weak< inkWidget > >;

	protected event OnInitialize()
	{
		m_container = ( ( inkCompoundWidget )( GetRootWidget() ) );
	}

	public function SetData( const data : ref< array< InventoryTooltipData_StatData > > )
	{
		m_data = data;
		UpdateLayout();
	}

	public function ClearData( toLeave : Int32 )
	{
		var statItem : weak< inkWidget >;
		while( m_itemsList.Size() > toLeave )
		{
			statItem = m_itemsList.PopBack();
			m_container.RemoveChild( statItem );
		}
	}

	private function UpdateLayout()
	{
		var i, dataCount : Int32;
		var statItem : weak< inkWidget >;
		var statItemCtrl : weak< InventoryItemStatItem >;
		dataCount = m_data.Size();
		ClearData( dataCount );
		while( m_itemsList.Size() < dataCount )
		{
			statItem = SpawnFromLocal( m_container, m_libraryItemName );
			statItem.SetStyle( m_container.GetStylePath() );
			m_itemsList.PushBack( statItem );
		}
		for( i = 0; i < dataCount; i += 1 )
		{
			statItem = m_itemsList[ i ];
			statItemCtrl = ( ( InventoryItemStatItem )( statItem.GetController() ) );
			statItemCtrl.SetData( m_data[ i ] );
		}
	}

	public function UpdateVisibility( force : Bool )
	{
		GetRootWidget().SetVisible( force );
	}

	public function UpdateVisibility()
	{
		GetRootWidget().SetVisible( m_itemsList.Size() > 0 );
	}

}

class InventoryItemStatItem extends inkLogicController
{
	private editable var m_labelText : inkTextRef;
	private editable var m_valueText : inkTextRef;
	private editable var m_differenceBarRef : inkWidgetRef;
	private editable var m_diffrenceArrowIndicatorRef : inkWidgetRef;
	var m_root : weak< inkWidget >;
	var m_differenceBar : weak< StatisticDifferenceBarController >;
	private editable var m_negativeState : CName;
	default m_negativeState = 'worse';
	private editable var m_positiveState : CName;
	default m_positiveState = 'better';

	protected event OnInitialize()
	{
		m_root = GetRootWidget();
		if( inkWidgetRef.IsValid( m_differenceBarRef ) )
		{
			m_differenceBar = ( ( StatisticDifferenceBarController )( inkWidgetRef.GetController( m_differenceBarRef ) ) );
		}
	}

	public function SetData( const data : ref< InventoryTooltipData_StatData > )
	{
		var stateName : CName;
		var diffValue : Int32;
		stateName = inkWidget.DefaultState();
		diffValue = data.diffValue;
		if( diffValue != 0 )
		{
			stateName = ( ( diffValue > 0 ) ? ( m_positiveState ) : ( m_negativeState ) );
		}
		if( data.state != EInventoryDataStatDisplayType.Value )
		{
			inkTextRef.SetText( m_valueText, IntToString( data.currentValue ) );
			if( m_differenceBar )
			{
				m_differenceBar.Setup( data );
				m_differenceBar.GetRootWidget().SetVisible( true );
			}
		}
		else
		{
			if( m_differenceBar )
			{
				m_differenceBar.GetRootWidget().SetVisible( false );
			}
			inkTextRef.SetText( m_valueText, IntToString( data.currentValue ) );
		}
		if( inkWidgetRef.Get( m_diffrenceArrowIndicatorRef ) )
		{
			if( diffValue == 0 )
			{
				inkWidgetRef.SetVisible( m_diffrenceArrowIndicatorRef, false );
			}
			else
			{
				inkWidgetRef.SetVisible( m_diffrenceArrowIndicatorRef, true );
				if( stateName == m_positiveState )
				{
					inkWidgetRef.SetRotation( m_diffrenceArrowIndicatorRef, 30.0 );
				}
				else
				{
					inkWidgetRef.SetRotation( m_diffrenceArrowIndicatorRef, 210.0 );
				}
			}
		}
		inkTextRef.SetText( m_labelText, data.statName );
		m_root.SetState( stateName );
	}

}

class StatisticDifferenceBarController extends inkLogicController
{
	private editable var m_filled : inkWidgetRef;
	private editable var m_difference : inkWidgetRef;
	private editable var m_empty : inkWidgetRef;

	public function Setup( const dataObject : ref< InventoryTooltipData_StatData > )
	{
		var size, empty, difference, filled : Float;
		var rMax, rMin, rDiff, rValue : Float;
		rMax = ( ( Float )( dataObject.maxStatValue ) );
		rMin = ( ( Float )( dataObject.minStatValue ) );
		rDiff = ( ( Float )( dataObject.diffValue ) );
		rValue = ( ( Float )( dataObject.currentValue ) );
		size = rMax - rMin;
		filled = MinF( rValue, rValue - rDiff ) / size;
		difference = AbsF( rDiff ) / size;
		empty = ( 1.0 - filled ) - difference;
		inkWidgetRef.SetSizeCoefficient( m_filled, filled );
		inkWidgetRef.SetSizeCoefficient( m_difference, difference );
		inkWidgetRef.SetSizeCoefficient( m_empty, empty );
	}

}

class DamageTypeIndicator extends inkLogicController
{
	private editable var m_DamageIconRef : inkImageRef;
	private editable var m_DamageTypeLabelRef : inkTextRef;

	public function Setup( damageType : gamedataDamageType )
	{
		var damageTypeString : String;
		var iconRef : UIIconReference;
		if( damageType == gamedataDamageType.Invalid )
		{
			GetRootWidget().SetVisible( false );
		}
		else
		{
			damageTypeString = ( ( String )( damageType ) );
			inkTextRef.SetLetterCase( m_DamageTypeLabelRef, textLetterCase.UpperCase );
			inkTextRef.SetText( m_DamageTypeLabelRef, damageTypeString );
			iconRef = new UIIconReference;
			iconRef.iconID = UIItemsHelper.GetTweakDBIDForDamageType( damageType );
			inkImageRef.RequestSetImage( m_DamageIconRef, iconRef );
			GetRootWidget().SetState( UIItemsHelper.GetStateNameForDamageType( damageType ) );
			GetRootWidget().SetVisible( true );
		}
	}

}

