#pragma once

#include "CoreMinimal.h"
#include "Subsystems/LocalPlayerSubsystem.h"
#include "GameplayTagContainer.h"
#include "Templates/SubclassOf.h"
#include "UIManagerSubsystem.generated.h"

class UCommonActivatableWidget;
class UUIPrimaryGameLayout_Base;

/**
 * Dono do layout raiz para o LocalPlayer. ULocalPlayerSubsystem porque o
 * LocalPlayer persiste entre mapas (non-seamless do mesmo GameInstance) e
 * troca de PlayerController — o root layout sobrevive ao travel
 * boot→mundo sem ser recriado (idempotente).
 *
 * É "burro": recebe classes já resolvidas. Quem resolve estado→tela e dirige
 * a navegação é o UUIFrontEndFlowSubsystem.
 */
UCLASS()
class ZMMO_API UUIManagerSubsystem : public ULocalPlayerSubsystem
{
	GENERATED_BODY()

public:
	/** Cria o root layout e adiciona ao viewport. Idempotente. */
	UFUNCTION(BlueprintCallable, Category = "UI|Manager")
	void CreateAndAddRootLayout(TSubclassOf<UUIPrimaryGameLayout_Base> LayoutClass);

	UFUNCTION(BlueprintPure, Category = "UI|Manager")
	bool IsRootLayoutReady() const;

	UFUNCTION(BlueprintPure, Category = "UI|Manager")
	UUIPrimaryGameLayout_Base* GetRootLayout() const { return RootLayout; }

	/** Empurra a widget no topo do stack da camada. */
	UFUNCTION(BlueprintCallable, Category = "UI|Manager")
	UCommonActivatableWidget* PushScreenToLayer(
		FGameplayTag LayerTag,
		TSubclassOf<UCommonActivatableWidget> WidgetClass);

	UFUNCTION(BlueprintCallable, Category = "UI|Manager")
	void PopScreenFromLayer(FGameplayTag LayerTag, UCommonActivatableWidget* Widget);

	UFUNCTION(BlueprintCallable, Category = "UI|Manager")
	void ClearLayer(FGameplayTag LayerTag);

	// USubsystem
	virtual void Deinitialize() override;

private:
	UPROPERTY(Transient)
	TObjectPtr<UUIPrimaryGameLayout_Base> RootLayout;
};
