class FilterRadioGroup extends inkRadioGroupController
{
	private editable var m_libraryPath : inkWidgetLibraryReference;
	private var m_TooltipsManager : weak< gameuiTooltipsManager >;
	private var m_TooltipIndex : Int32;
	private var m_toggles : array< weak< inkToggleController > >;
	private var m_rootRef : weak< inkCompoundWidget >;

	public function SetData( enumCount : Int32, optional tooltipsManager : weak< gameuiTooltipsManager >, optional tooltipIndex : Int32 )
	{
		var i : Int32;
		var data : array< Int32 >;
		for( i = 0; i < enumCount; i += 1 )
		{
			data.PushBack( i );
		}
		SetData( data, tooltipsManager, tooltipIndex );
	}

	public function SetData( const data : ref< array< Int32 > >, optional tooltipsManager : weak< gameuiTooltipsManager >, optional tooltipIndex : Int32 )
	{
		var i, limit : Int32;
		var filterToggle : ToggleController;
		limit = data.Size();
		m_TooltipIndex = tooltipIndex;
		m_TooltipsManager = tooltipsManager;
		m_rootRef = GetRootCompoundWidget();
		while( m_rootRef.GetNumChildren() > limit )
		{
			m_toggles.Erase( 0 );
			m_rootRef.RemoveChildByIndex( 0 );
			RemoveToggle( 0 );
		}
		while( m_rootRef.GetNumChildren() < limit )
		{
			filterToggle = ( ( ToggleController )( SpawnFromExternal( m_rootRef, inkWidgetLibraryResource.GetPath( m_libraryPath.widgetLibrary ), m_libraryPath.widgetItem ).GetController() ) );
			AddToggle( filterToggle );
			m_toggles.PushBack( filterToggle );
			filterToggle.RegisterToCallback( 'OnHoverOver', this, 'OnHoverOver' );
			filterToggle.RegisterToCallback( 'OnHoverOut', this, 'OnHoverOut' );
		}
		for( i = 0; i < data.Size(); i += 1 )
		{
			filterToggle = ( ( ToggleController )( m_toggles[ i ] ) );
			filterToggle.SetToggleData( data[ i ] );
		}
	}

	public function AddFilter( data : Int32 )
	{
		var i : Int32;
		var filterToggle : ToggleController;
		for( i = 0; i < m_toggles.Size(); i += 1 )
		{
			if( ( ( ToggleController )( m_toggles[ i ] ) ).GetData() == data )
			{
				return;
			}
		}
		filterToggle = ( ( ToggleController )( SpawnFromExternal( m_rootRef, inkWidgetLibraryResource.GetPath( m_libraryPath.widgetLibrary ), m_libraryPath.widgetItem ).GetController() ) );
		AddToggle( filterToggle );
		m_toggles.PushBack( filterToggle );
		filterToggle.SetToggleData( data );
		filterToggle.RegisterToCallback( 'OnHoverOver', this, 'OnHoverOver' );
		filterToggle.RegisterToCallback( 'OnHoverOut', this, 'OnHoverOut' );
	}

	public function RemoveFilter( data : Int32 )
	{
		var i : Int32;
		for( i = 0; i < m_toggles.Size(); i += 1 )
		{
			if( ( ( ToggleController )( m_toggles[ i ] ) ).GetData() == data )
			{
				m_toggles.Erase( i );
				m_rootRef.RemoveChildByIndex( i );
				RemoveToggle( i );
				return;
			}
		}
	}

	public function ToggleData( data : Int32 )
	{
		var i : Int32;
		var filterToggle : ToggleController;
		for( i = 0; i < m_toggles.Size(); i += 1 )
		{
			filterToggle = ( ( ToggleController )( m_toggles[ i ] ) );
			if( filterToggle.GetData() == data )
			{
				filterToggle.Toggle();
			}
		}
	}

	protected event OnHoverOver( evt : inkPointerEvent )
	{
		var widget : inkWidget;
		var tooltipData : MessageTooltipData;
		var controller : ToggleController;
		var parentEvent : FilterRadioItemHoverOver;
		widget = evt.GetCurrentTarget();
		controller = ( ( ToggleController )( widget.GetController() ) );
		if( m_TooltipsManager )
		{
			tooltipData = new MessageTooltipData;
			tooltipData.Title = GetLocalizedText( controller.GetLabelKey() );
			m_TooltipsManager.ShowTooltipAtWidget( m_TooltipIndex, evt.GetCurrentTarget(), tooltipData, gameuiETooltipPlacement.RightCenter );
		}
		parentEvent = new FilterRadioItemHoverOver;
		parentEvent.target = evt.GetTarget();
		parentEvent.identifier = controller.GetData();
		QueueEvent( parentEvent );
	}

	protected event OnHoverOut( evt : inkPointerEvent )
	{
		var widget : inkWidget;
		var controller : ToggleController;
		var parentEvent : FilterRadioItemHoverOut;
		widget = evt.GetCurrentTarget();
		controller = ( ( ToggleController )( widget.GetController() ) );
		if( m_TooltipsManager )
		{
			m_TooltipsManager.HideTooltips();
		}
		parentEvent = new FilterRadioItemHoverOut;
		parentEvent.target = evt.GetTarget();
		parentEvent.identifier = controller.GetData();
		QueueEvent( parentEvent );
	}

}

class ToggleController extends inkToggleController
{
	protected editable var m_label : inkTextRef;
	protected editable var m_icon : inkImageRef;
	protected var m_data : Int32;

	public function SetToggleData( data : Int32 )
	{
		m_data = data;
		inkTextRef.SetText( m_label, GetLabelKey() );
		InkImageUtils.RequestSetImage( this, m_icon, GetIcon() );
	}

	public function GetData() : Int32
	{
		return m_data;
	}

	public virtual function GetLabelKey() : String
	{
		return "";
	}

	public virtual function GetIcon() : String
	{
		return "";
	}

}

class FilterRadioItemHoverOver extends Event
{
	var target : weak< inkWidget >;
	var identifier : Int32;
}

class FilterRadioItemHoverOut extends Event
{
	var target : weak< inkWidget >;
	var identifier : Int32;
}

