class NewItemTooltipAttachmentGroupController extends inkLogicController
{
	private editable var m_indicatorContainer : inkWidgetRef;
	private editable var m_indicatorWidget : inkWidgetRef;
	private editable var m_rarityContainer : inkWidgetRef;
	private editable var m_rarityWidget : inkImageRef;
	private editable var m_entriesContainer : inkCompoundRef;
	private var m_entriesControllers : array< weak< NewItemTooltipAttachmentEntryController > >;
	private var m_entriesData : array< NewItemTooltipAttachmentEntryData >;
	private var m_requestedEntries : Int32;
	private var m_isEmpty : Bool;
	private var m_colorState : CName;
	private editable var m_isCrafting : Bool;

	public constexpr static function StaticDefaultColorState() : CName
	{
		return 'Default';
	}

	public constexpr function DefaultColorState() : CName
	{
		return NewItemTooltipAttachmentGroupController.StaticDefaultColorState();
	}

	public function SetData( data : MinimalItemTooltipModData )
	{
		if( ( ( MinimalItemTooltipModAttachmentData )( data ) ) )
		{
			SetData( ( ( MinimalItemTooltipModAttachmentData )( data ) ) );
			return;
		}
		m_entriesData.Clear();
		m_isEmpty = false;
		m_colorState = DefaultColorState();
		m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( ( ( MinimalItemTooltipModRecordData )( data ) ).description, ( ( MinimalItemTooltipModRecordData )( data ) ).dataPackage ) );
		Update();
	}

	public function SetData( data : MinimalItemTooltipModAttachmentData )
	{
		var i : Int32;
		var ability : InventoryItemAbility;
		m_entriesData.Clear();
		if( data.isEmpty || ( data.abilitiesSize == 0 ) )
		{
			m_isEmpty = data.isEmpty;
			m_colorState = data.qualityName;
			m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( data.slotName ) );
		}
		else
		{
			m_isEmpty = data.isEmpty;
			m_colorState = data.qualityName;
			for( i = 0; i < data.abilitiesSize; i += 1 )
			{
				ability = data.abilities[ i ];
				if( ability.Description != "" )
				{
					m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( ability.Description, ability.LocalizationDataPackage ) );
				}
				else
				{
					m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( GetLocalizedText( "UI-Labels-EmptySlot" ) ) );
				}
			}
		}
		Update();
	}

	public function SetData( data : UIInventoryItemMod )
	{
		if( ( ( UIInventoryItemModAttachment )( data ) ) )
		{
			SetData( ( ( UIInventoryItemModAttachment )( data ) ) );
			return;
		}
		m_isEmpty = false;
		m_colorState = DefaultColorState();
		m_entriesData.Clear();
		m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( ( ( UIInventoryItemModDataPackage )( data ) ).Description, ( ( UIInventoryItemModDataPackage )( data ) ).DataPackage, ( ( UIInventoryItemModDataPackage )( data ) ).AttunementData ) );
		Update();
	}

	public function SetData( data : UIInventoryItemModAttachment )
	{
		var i : Int32;
		var ability : InventoryItemAbility;
		m_entriesData.Clear();
		if( data.IsEmpty || ( data.AbilitiesSize == 0 ) )
		{
			m_isEmpty = data.IsEmpty;
			if( data.Quality == gamedataQuality.Invalid )
			{
				m_colorState = 'Empty';
			}
			else
			{
				m_colorState = UIItemsHelper.QualityStringToStateName( UIItemsHelper.QualityEnumToString( data.Quality ) );
			}
			m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( data.SlotName ) );
		}
		else
		{
			m_isEmpty = false;
			m_colorState = UIItemsHelper.QualityStringToStateName( UIItemsHelper.QualityEnumToString( data.Quality ) );
			for( i = 0; i < data.AbilitiesSize; i += 1 )
			{
				ability = data.Abilities[ i ];
				if( ability.Description != "" )
				{
					m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( ability.Description, ability.LocalizationDataPackage ) );
				}
				else
				{
					m_entriesData.PushBack( NewItemTooltipAttachmentEntryData.Make( GetLocalizedText( "UI-Labels-EmptySlot" ) ) );
				}
			}
		}
		Update();
	}

	private function Update()
	{
		UpdateEntries();
		UpdateState();
	}

	private function UpdateEntries()
	{
		var i, limit : Int32;
		var dataSize : Int32;
		dataSize = m_entriesData.Size();
		for( ; m_requestedEntries < dataSize; m_requestedEntries += 1 )
		{
			AsyncSpawnFromLocal( inkCompoundRef.Get( m_entriesContainer ), 'itemTooltipModEntry', this, 'OnEntrySpawned' );
		}
		for( i = 0, limit = m_entriesControllers.Size(); i < limit; i += 1 )
		{
			if( i < dataSize )
			{
				m_entriesControllers[ i ].GetRootWidget().SetVisible( true );
				m_entriesControllers[ i ].SetData( m_entriesData[ i ] );
			}
			else
			{
				m_entriesControllers[ i ].GetRootWidget().SetVisible( false );
			}
		}
	}

	private function UpdateState()
	{
		inkImageRef.SetVisible( m_rarityWidget, !( m_isEmpty ) );
		inkWidgetRef.SetState( m_rarityContainer, m_colorState );
		inkWidgetRef.SetVisible( m_indicatorWidget, !( m_isEmpty ) );
	}

	protected event OnEntrySpawned( widget : inkWidget, userData : IScriptable )
	{
		var index : Int32;
		m_entriesControllers.PushBack( ( ( NewItemTooltipAttachmentEntryController )( widget.GetController() ) ) );
		index = m_entriesControllers.Size() - 1;
		if( index >= m_entriesData.Size() )
		{
			widget.SetVisible( false );
		}
		else
		{
			( ( NewItemTooltipAttachmentEntryController )( widget.GetController() ) ).GetContext( m_isCrafting );
			( ( NewItemTooltipAttachmentEntryController )( widget.GetController() ) ).SetData( m_entriesData[ index ] );
		}
	}

	public function GetContext( isCrafting : Bool )
	{
		m_isCrafting = isCrafting;
	}

}

class NewItemTooltipAttachmentEntrySpawnData
{
	var index : Int32;
}

class NewItemTooltipAttachmentEntryController extends inkLogicController
{
	protected editable var m_text : inkTextRef;
	protected editable var m_attunementContainer : inkWidgetRef;
	protected editable var m_attunementText : inkTextRef;
	protected editable var m_attunementIcon : inkImageRef;
	protected var m_settings : UserSettings;
	protected var m_settingsListener : NewItemTooltipAttachmentEntrySettingsListener;
	protected var m_groupPath : CName;
	default m_groupPath = '/accessibility/interface';
	protected var m_bigFontEnabled : Bool;
	protected var m_isCrafting : Bool;

	protected event OnInitialize()
	{
		inkWidgetRef.SetVisible( m_attunementContainer, false );
	}

	public function SetData( data : NewItemTooltipAttachmentEntryData )
	{
		inkTextRef.SetText( m_text, data.text );
		inkTextRef.SetState( m_text, data.colorState );
		m_settings = new UserSettings;
		m_settingsListener = new NewItemTooltipAttachmentEntrySettingsListener;
		m_settingsListener.RegisterController( this );
		m_settingsListener.Register( m_groupPath );
		NewUpdateTooltipSize();
		if( data.dataPackage && data.dataPackage.GetParamsCount() )
		{
			inkTextRef.SetTextParameters( m_text, data.dataPackage.GetTextParams() );
		}
		if( data.attunementData != NULL )
		{
			inkTextRef.SetText( m_attunementText, data.attunementData.Name );
			InkImageUtils.RequestSetImage( this, m_attunementIcon, data.attunementData.Icon );
			inkWidgetRef.SetVisible( m_attunementContainer, true );
		}
		else
		{
			inkWidgetRef.SetVisible( m_attunementContainer, false );
		}
	}

	public function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		switch( varName )
		{
			case 'BigFont':
				NewUpdateTooltipSize();
			break;
			default:
				break;
		}
	}

	private function NewUpdateTooltipSize()
	{
		var configVar : ConfigVarBool;
		configVar = ( ( ConfigVarBool )( m_settings.GetVar( m_groupPath, 'BigFont' ) ) );
		NewSetTooltipSize( configVar.GetValue() );
	}

	protected virtual function NewSetTooltipSize( value : Bool )
	{
		if( value == true && !( m_isCrafting ) )
		{
			inkTextRef.SetWrappingAtPosition( m_text, 668.0 );
			m_bigFontEnabled = true;
		}
		else
		{
			inkTextRef.SetWrappingAtPosition( m_text, 538.0 );
			m_bigFontEnabled = false;
		}
	}

	public function GetContext( isCrafting : Bool )
	{
		m_isCrafting = isCrafting;
	}

}

class NewItemTooltipAttachmentEntrySettingsListener extends ConfigVarListener
{
	private var m_ctrl : weak< NewItemTooltipAttachmentEntryController >;

	public function RegisterController( ctrl : NewItemTooltipAttachmentEntryController )
	{
		m_ctrl = ctrl;
	}

	public export override function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		m_ctrl.OnVarModified( groupPath, varName, varType, reason );
	}

}

class NewItemTooltipAttachmentEntryData
{
	var text : String;
	var colorState : CName;
	var dataPackage : UILocalizationDataPackage;
	var attunementData : UIInventoryItemModAttunementData;

	public static function Make( text : String, optional dataPackage : UILocalizationDataPackage, optional attunementData : UIInventoryItemModAttunementData ) : NewItemTooltipAttachmentEntryData
	{
		return NewItemTooltipAttachmentEntryData.Make( text, NewItemTooltipAttachmentGroupController.StaticDefaultColorState(), dataPackage, attunementData );
	}

	public static function Make( text : String, colorState : CName, optional dataPackage : UILocalizationDataPackage, optional attunementData : UIInventoryItemModAttunementData ) : NewItemTooltipAttachmentEntryData
	{
		var instance : NewItemTooltipAttachmentEntryData;
		instance = new NewItemTooltipAttachmentEntryData;
		instance.text = text;
		instance.colorState = colorState;
		instance.dataPackage = dataPackage;
		instance.attunementData = attunementData;
		return instance;
	}

}

