#pragma once

#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "UI/ThemeKeys.h"
#include "UI/ThemeRow.h"
#include "UI/UIStyleRow.h"
#include "ZeusThemeSubsystem.generated.h"

class UZeusThemeDataAsset;
class UDataTable;
class UTexture2D;
class USoundBase;
class UNiagaraSystem;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnZeusThemeChanged, FName, NewThemeId);

/**
 * Resolves which UI theme is active and answers asset lookups by EZeusThemeKey.
 *
 * Resolution precedence (see ARQUITETURA.md §1.10):
 *   1. Dev override (.ini DevThemeOverride) — ignored in Shipping builds.
 *   2. Authoritative server theme (set via ApplyServerTheme).
 *   3. Local calendar (DT_ThemeCalendar).
 *   4. DA_Theme_Default.
 *
 * Widgets must never hold hard refs to theme textures/sounds. They consume the
 * active theme through Get* methods on this subsystem.
 */
UCLASS(Config = Game)
class ZMMO_API UZeusThemeSubsystem : public UGameInstanceSubsystem
{
	GENERATED_BODY()

public:
	// USubsystem
	virtual void Initialize(FSubsystemCollectionBase& Collection) override;
	virtual void Deinitialize() override;

	/** Server-authoritative theme. Call from the ZeusNetwork ServerHello handler. */
	UFUNCTION(BlueprintCallable, Category = "Theme")
	void ApplyServerTheme(FName ThemeId);

	/** Forces a specific theme regardless of precedence. Use only for debug UI. */
	UFUNCTION(BlueprintCallable, Category = "Theme|Debug")
	void ForceTheme(FName ThemeId);

	/** Resets to the precedence-resolved theme. */
	UFUNCTION(BlueprintCallable, Category = "Theme|Debug")
	void ClearForcedTheme();

	UFUNCTION(BlueprintPure, Category = "Theme")
	FName GetActiveThemeId() const { return ActiveThemeId; }

	UFUNCTION(BlueprintCallable, Category = "Theme")
	UTexture2D* GetTexture(EZeusThemeKey Key) const;

	UFUNCTION(BlueprintCallable, Category = "Theme")
	USoundBase* GetSound(EZeusThemeKey Key) const;

	UFUNCTION(BlueprintCallable, Category = "Theme")
	UNiagaraSystem* GetVFX(EZeusThemeKey Key) const;

	UFUNCTION(BlueprintCallable, Category = "Theme")
	UObject* GetGeneric(EZeusThemeKey Key) const;

	/**
	 * Tokens de estilo (cores/fontes/dimensões) do tema ativo, resolvidos de
	 * DT_UI_Styles pelo ThemeId. Faz fallback para a linha "Default" quando o
	 * tema ativo não tem linha própria — mesma política dos assets (§5).
	 */
	UFUNCTION(BlueprintPure, Category = "Theme|Style")
	const FUIStyle& GetActiveUIStyle() const { return ActiveUIStyle; }

	/** Fired whenever the active theme actually changes. */
	UPROPERTY(BlueprintAssignable, Category = "Theme")
	FOnZeusThemeChanged OnThemeChanged;

protected:
	/** Default theme. Must be filled with every key — it is the final fallback. */
	UPROPERTY(Config, EditDefaultsOnly, Category = "Theme")
	TSoftObjectPtr<UZeusThemeDataAsset> DefaultThemeAsset;

	/** Registry of seasonal/event themes keyed by ThemeId. */
	UPROPERTY(Config, EditDefaultsOnly, Category = "Theme")
	TMap<FName, TSoftObjectPtr<UZeusThemeDataAsset>> ThemeRegistry;

	/** Local calendar table (FThemeCalendarRow). Optional. */
	UPROPERTY(Config, EditDefaultsOnly, Category = "Theme")
	TSoftObjectPtr<UDataTable> CalendarTable;

	/** Tabela de tokens de estilo (FUIStyleRow), keyed por ThemeId. */
	UPROPERTY(Config, EditDefaultsOnly, Category = "Theme")
	TSoftObjectPtr<UDataTable> UIStyleTable;

	/** Dev-only override read from DefaultGame.ini. Empty in Shipping. */
	UPROPERTY(Config, EditDefaultsOnly, Category = "Theme|Debug")
	FName DevThemeOverride;

private:
	void ResolveActiveTheme();
	FName ResolveByCalendar() const;
	UZeusThemeDataAsset* LoadThemeById(FName ThemeId) const;
	const FThemeEntry* FindEntryWithFallback(EZeusThemeKey Key) const;
	void ResolveActiveUIStyle(FName ThemeId);

	UPROPERTY(Transient)
	TObjectPtr<UZeusThemeDataAsset> ActiveTheme;

	UPROPERTY(Transient)
	TObjectPtr<UZeusThemeDataAsset> DefaultTheme;

	UPROPERTY(Transient)
	FName ActiveThemeId;

	UPROPERTY(Transient)
	FName ServerThemeId;

	UPROPERTY(Transient)
	FName ForcedThemeId;

	UPROPERTY(Transient)
	FUIStyle ActiveUIStyle;
};
