class ClothingSetIconsPopupData extends inkGameNotificationData
{
	var IconID : TweakDBID;
	var IconChanged : Bool;
	var IconIDs : array< TweakDBID >;
}

class ClothingSetIconsPopup extends inkGameController
{
	private editable var m_iconGrid : inkWidgetRef;
	private editable var m_buttonHintsRoot : inkWidgetRef;
	private var m_data : ClothingSetIconsPopupData;
	private editable var m_libraryPath : inkWidgetLibraryReference;

	protected event OnInitialize()
	{
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_data = ( ( ClothingSetIconsPopupData )( GetRootWidget().GetUserData( 'ClothingSetIconsPopupData' ) ) );
		SetButtonHints();
		FillIconGrid( m_data.IconIDs );
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
	}

	private function FillIconGrid( const iconIDs : ref< array< TweakDBID > > )
	{
		var i : Int32;
		var iconController : weak< ClothingSetIconButton >;
		for( i = 0; i < iconIDs.Size(); i += 1 )
		{
			iconController = ( ( ClothingSetIconButton )( SpawnFromLocal( inkWidgetRef.Get( m_iconGrid ), 'iconButton' ).GetController() ) );
			iconController.SetIcon( iconIDs[ i ], iconIDs[ i ] == m_data.IconID );
		}
	}

	protected event OnSetIconClick( e : SetIconSelectEvent )
	{
		Close( true, e.IconID );
	}

	private function SetButtonHints()
	{
		AddButtonHints( 'UI_Cancel', "UI-ResourceExports-Cancel" );
	}

	private function AddButtonHints( actionName : CName, const label : ref< String > )
	{
		var buttonHint : LabelInputDisplayController;
		buttonHint = ( ( LabelInputDisplayController )( SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsRoot ), inkWidgetLibraryResource.GetPath( m_libraryPath.widgetLibrary ), m_libraryPath.widgetItem ).GetController() ) );
		buttonHint.SetInputActionLabel( actionName, label );
	}

	protected event OnHandlePressInput( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'cancel' ) )
		{
			Close( false );
		}
	}

	private function Close( success : Bool, optional iconID : TweakDBID )
	{
		var closeData : ClothingSetIconsPopupData;
		closeData = new ClothingSetIconsPopupData;
		closeData.IconID = iconID;
		closeData.IconChanged = success;
		m_data.token.TriggerCallback( closeData );
	}

}

class ClothingSetIconButton extends BaseButtonView
{
	private editable var m_setIcon : inkImageRef;
	private editable var m_currentIconFrame : inkWidgetRef;
	private var m_iconID : TweakDBID;

	protected event OnInitialize()
	{
		super.OnInitialize();
		RegisterToCallback( 'OnRelease', this, 'OnSetIconClick' );
	}

	protected event OnUninitialize()
	{
		UnregisterFromCallback( 'OnRelease', this, 'OnSetIconClick' );
	}

	protected event OnSetIconClick( e : inkPointerEvent )
	{
		var evt : SetIconSelectEvent;
		if( e.IsAction( 'click' ) )
		{
			evt = new SetIconSelectEvent;
			evt.IconID = m_iconID;
			QueueEvent( evt );
		}
	}

	public function SetIcon( iconID : TweakDBID, choosen : Bool )
	{
		InkImageUtils.RequestSetImage( this, m_setIcon, iconID );
		m_iconID = iconID;
		inkWidgetRef.SetVisible( m_currentIconFrame, choosen );
	}

}

class SetIconSelectEvent extends Event
{
	var IconID : TweakDBID;
}

