enum ItemLabelType
{
	New = 0,
	Quest = 1,
	Money = 2,
	Equipped = 3,
	Owned = 4,
	Buyback = 5,
	DLCNew = 6,
}

class ItemLabelContainerController extends inkLogicController
{
	protected var m_items : array< weak< ItemLabelController > >;

	public function Add( type : ItemLabelType, optional params : String )
	{
		var root : weak< inkCompoundWidget >;
		var widget : weak< inkWidget >;
		var item : weak< ItemLabelController >;
		var i : Int32;
		root = GetRootCompoundWidget();
		for( i = 0; i < m_items.Size(); i += 1 )
		{
			if( m_items[ i ].GetType() == type )
			{
				return;
			}
		}
		if( type == ItemLabelType.New )
		{
			widget = SpawnFromLocal( root, 'itemLabelNew' );
		}
		else if( type == ItemLabelType.Owned )
		{
			widget = SpawnFromLocal( root, 'itemLabelOwned' );
		}
		else
		{
			widget = SpawnFromLocal( root, 'itemLabel' );
		}
		item = ( ( ItemLabelController )( widget.GetController() ) );
		widget.SetVAlign( inkEVerticalAlign.Top );
		widget.SetHAlign( inkEHorizontalAlign.Left );
		item.Setup( type, params );
		m_items.PushBack( item );
		Reorder();
	}

	public function Remove( type : ItemLabelType )
	{
		var i : Int32;
		for( i = 0; i < m_items.Size(); i += 1 )
		{
			if( m_items[ i ].GetType() == type )
			{
				GetRootCompoundWidget().RemoveChild( m_items[ i ].GetRootWidget() );
				return;
			}
		}
	}

	public function Has( type : ItemLabelType ) : Bool
	{
		var i : Int32;
		for( i = 0; i < m_items.Size(); i += 1 )
		{
			if( m_items[ i ].GetType() == type )
			{
				return true;
			}
		}
		return false;
	}

	public function Clear()
	{
		m_items.Clear();
		GetRootCompoundWidget().RemoveAllChildren();
	}

	protected function Reorder()
	{
		var i, size : Int32;
		var temp : weak< ItemLabelController >;
		var sorted : Bool;
		var root : weak< inkCompoundWidget >;
		root = GetRootCompoundWidget();
		size = m_items.Size();
		while( !( sorted ) )
		{
			sorted = true;
			for( i = 0; i < ( size - 1 ); i += 1 )
			{
				if( ( ( Int32 )( m_items[ i ].GetType() ) ) > ( ( Int32 )( m_items[ ( ( i + 1 ) % size ) ].GetType() ) ) )
				{
					temp = m_items[ i ];
					m_items[ i ] = m_items[ ( i + 1 ) % size ];
					m_items[ ( i + 1 ) % size ] = temp;
					sorted = false;
				}
			}
		}
		for( i = 0; i < size; i += 1 )
		{
			root.ReorderChild( m_items[ i ].GetRootWidget(), i );
		}
	}

}

class ItemLabelController extends inkLogicController
{
	protected editable var m_label : inkTextRef;
	protected editable var m_moneyIcon : inkImageRef;
	protected var m_type : ItemLabelType;

	public function Setup( type : ItemLabelType, optional params : String )
	{
		var labelKey, labelText : String;
		inkTextRef.SetVisible( m_label, type != ItemLabelType.Money );
		inkImageRef.SetVisible( m_moneyIcon, type == ItemLabelType.Money );
		labelKey = GetLabelKey( type );
		m_type = type;
		labelText = GetLocalizedText( labelKey );
		if( !( IsStringValid( labelText ) ) )
		{
			labelText = labelKey;
		}
		GetRootWidget().SetState( GetState( type ) );
		inkTextRef.SetText( m_label, labelText + params );
	}

	public function GetType() : ItemLabelType
	{
		return m_type;
	}

	protected static function GetLabelKey( type : ItemLabelType ) : String
	{
		switch( type )
		{
			case ItemLabelType.New:
				return "UI-ItemLabel-New";
			case ItemLabelType.Quest:
				return "UI-ItemLabel-Quest";
			case ItemLabelType.Money:
				return "UI-ItemLabel-Money";
			case ItemLabelType.Equipped:
				return "UI-ItemLabel-Equipped";
			case ItemLabelType.Owned:
				return "UI-ItemLabel-Owned";
			case ItemLabelType.Buyback:
				return "UI-ItemLabel-Buyback";
			case ItemLabelType.DLCNew:
				return "UI-ItemLabel-New";
		}
		return "None";
	}

	protected static function GetState( type : ItemLabelType ) : CName
	{
		switch( type )
		{
			case ItemLabelType.New:
				return 'New';
			case ItemLabelType.Quest:
				return 'Quest';
			case ItemLabelType.Money:
				return 'Money';
			case ItemLabelType.Equipped:
				return 'Equipped';
			case ItemLabelType.Owned:
				return 'Owned';
			case ItemLabelType.Buyback:
				return 'Buyback';
			case ItemLabelType.DLCNew:
				return 'New';
		}
		return 'None';
	}

}

