#pragma once

#include "CoreMinimal.h"
#include "CommonUserWidget.h"
#include "GameplayTagContainer.h"
#include "Templates/SubclassOf.h"
#include "UIPrimaryGameLayout_Base.generated.h"

class UCommonActivatableWidget;
class UCommonActivatableWidgetStack;

/**
 * Layout raiz ("switch principal") do cliente. Mantém 4 stacks CommonUI,
 * um por camada (GameplayTag UI.Layer.* — ver UI/UILayerTags.h):
 *
 *   Stack_Game     ← UI.Layer.Game     (HUD de gameplay)
 *   Stack_GameMenu ← UI.Layer.GameMenu (menus sobre o jogo)
 *   Stack_Menu     ← UI.Layer.Menu     (front-end: Boot/Login/Lobby/…)
 *   Stack_Modal    ← UI.Layer.Modal    (diálogos/loading por cima)
 *
 * Camada Abstract; o WBP concreto (WBP_PrimaryGameLayout) herda DIRETO desta
 * classe C++ e nomeia os 4 stacks exatamente como os BindWidget abaixo
 * (ARQUITETURA.md §3.3). O UUIManagerSubsystem instancia o WBP e o adiciona
 * ao viewport; o UUIFrontEndFlowSubsystem empurra telas por camada.
 */
UCLASS(Abstract, Blueprintable)
class ZMMO_API UUIPrimaryGameLayout_Base : public UCommonUserWidget
{
	GENERATED_BODY()

public:
	/** Stack da camada (nullptr se a tag não for uma UI.Layer.* conhecida). */
	UFUNCTION(BlueprintCallable, Category = "UI|Layout")
	UCommonActivatableWidgetStack* GetLayerStack(FGameplayTag LayerTag) const;

	/** Instancia e empurra a widget no topo do stack da camada. */
	UFUNCTION(BlueprintCallable, Category = "UI|Layout")
	UCommonActivatableWidget* PushWidgetToLayer(
		FGameplayTag LayerTag,
		TSubclassOf<UCommonActivatableWidget> WidgetClass);

	/** Remove uma widget específica do stack da camada. */
	UFUNCTION(BlueprintCallable, Category = "UI|Layout")
	void PopWidgetFromLayer(FGameplayTag LayerTag, UCommonActivatableWidget* Widget);

	/** Esvazia o stack da camada. */
	UFUNCTION(BlueprintCallable, Category = "UI|Layout")
	void ClearLayer(FGameplayTag LayerTag);

protected:
	virtual void NativeOnInitialized() override;

	/** Registra um stack numa camada (chamado em NativeOnInitialized). */
	void RegisterLayer(FGameplayTag LayerTag, UCommonActivatableWidgetStack* Stack);

	// ---- Stacks por camada (o WBP concreto deve nomear assim) ----
	UPROPERTY(BlueprintReadOnly, Category = "UI|Layout", meta = (BindWidget))
	TObjectPtr<UCommonActivatableWidgetStack> Stack_Game;

	UPROPERTY(BlueprintReadOnly, Category = "UI|Layout", meta = (BindWidget))
	TObjectPtr<UCommonActivatableWidgetStack> Stack_GameMenu;

	UPROPERTY(BlueprintReadOnly, Category = "UI|Layout", meta = (BindWidget))
	TObjectPtr<UCommonActivatableWidgetStack> Stack_Menu;

	UPROPERTY(BlueprintReadOnly, Category = "UI|Layout", meta = (BindWidget))
	TObjectPtr<UCommonActivatableWidgetStack> Stack_Modal;

private:
	UPROPERTY(Transient)
	TMap<FGameplayTag, TObjectPtr<UCommonActivatableWidgetStack>> Layers;
};
