#include "UIPrimaryGameLayout_Base.h"

#include "CommonActivatableWidget.h"
#include "Widgets/CommonActivatableWidgetContainer.h"
#include "UI/UILayerTags.h"

void UUIPrimaryGameLayout_Base::NativeOnInitialized()
{
	Super::NativeOnInitialized();

	RegisterLayer(ZMMOUITags::UI_Layer_Game, Stack_Game);
	RegisterLayer(ZMMOUITags::UI_Layer_GameMenu, Stack_GameMenu);
	RegisterLayer(ZMMOUITags::UI_Layer_Menu, Stack_Menu);
	RegisterLayer(ZMMOUITags::UI_Layer_Modal, Stack_Modal);
}

void UUIPrimaryGameLayout_Base::RegisterLayer(FGameplayTag LayerTag, UCommonActivatableWidgetStack* Stack)
{
	if (Stack)
	{
		Layers.Add(LayerTag, Stack);
	}
}

UCommonActivatableWidgetStack* UUIPrimaryGameLayout_Base::GetLayerStack(FGameplayTag LayerTag) const
{
	const TObjectPtr<UCommonActivatableWidgetStack>* Found = Layers.Find(LayerTag);
	return Found ? *Found : nullptr;
}

UCommonActivatableWidget* UUIPrimaryGameLayout_Base::PushWidgetToLayer(
	FGameplayTag LayerTag,
	TSubclassOf<UCommonActivatableWidget> WidgetClass)
{
	if (!WidgetClass)
	{
		return nullptr;
	}
	if (UCommonActivatableWidgetStack* Stack = GetLayerStack(LayerTag))
	{
		return Stack->AddWidget(WidgetClass);
	}
	return nullptr;
}

void UUIPrimaryGameLayout_Base::PopWidgetFromLayer(FGameplayTag LayerTag, UCommonActivatableWidget* Widget)
{
	if (!Widget)
	{
		return;
	}
	if (UCommonActivatableWidgetStack* Stack = GetLayerStack(LayerTag))
	{
		Stack->RemoveWidget(*Widget);
	}
}

void UUIPrimaryGameLayout_Base::ClearLayer(FGameplayTag LayerTag)
{
	if (UCommonActivatableWidgetStack* Stack = GetLayerStack(LayerTag))
	{
		Stack->ClearWidgets();
	}
}
