#include "ZMMOPlayerState.h"

#include "Components/ActorComponent.h"
#include "Misc/ConfigCacheIni.h"
#include "ZMMO.h"

AZMMOPlayerState::AZMMOPlayerState()
{
	// Component Registry: instancia cada classe declarada em
	// DefaultGame.ini::[/Script/ZMMO.ZMMOPlayerState]+ComponentClasses=...
	//
	// Le manualmente via GConfig para evitar timing do UPROPERTY(Config),
	// que requer LoadConfig() ja ter rodado no CDO antes da instance ser
	// construida. Pra propriedades em arrays config com `+` syntax, lendo
	// direto e mais previsivel.
	if (ComponentClasses.Num() == 0 && GConfig)
	{
		TArray<FString> ClassPaths;
		GConfig->GetArray(TEXT("/Script/ZMMO.ZMMOPlayerState"),
		                  TEXT("ComponentClasses"),
		                  ClassPaths,
		                  GGameIni);
		for (const FString& Path : ClassPaths)
		{
			if (UClass* C = LoadClass<UActorComponent>(nullptr, *Path))
			{
				ComponentClasses.Add(C);
			}
			else
			{
				UE_LOG(LogZMMO, Warning,
					TEXT("ZMMOPlayerState: ComponentClasses path nao resolveu: %s"), *Path);
			}
		}
	}

	UE_LOG(LogZMMO, Log,
		TEXT("AZMMOPlayerState ctor: %d componente(s) no registry (instance=%s)"),
		ComponentClasses.Num(), *GetName());

	int32 SubobjectIndex = 0;
	for (const TSubclassOf<UActorComponent>& CompClassRef : ComponentClasses)
	{
		UClass* CompClass = CompClassRef.Get();
		if (!CompClass)
		{
			UE_LOG(LogZMMO, Warning,
				TEXT("ZMMOPlayerState: ComponentClass null em ComponentClasses[%d]"),
				SubobjectIndex);
			++SubobjectIndex;
			continue;
		}
		const FName CompName = MakeUniqueObjectName(this, CompClass,
			FName(*FString::Printf(TEXT("ZMMOComp_%d_%s"), SubobjectIndex, *CompClass->GetName())));
		// Overload runtime de CreateDefaultSubobject retorna UObject* (sem template) —
		// precisa cast para UActorComponent.
		UActorComponent* Comp = Cast<UActorComponent>(CreateDefaultSubobject(CompName, CompClass, CompClass,
			/*bIsRequired*/ false, /*bIsTransient*/ false));
		if (Comp)
		{
			UE_LOG(LogZMMO, Log,
				TEXT("ZMMOPlayerState: componente registrado [%d] %s"),
				SubobjectIndex, *CompClass->GetName());
		}
		else
		{
			UE_LOG(LogZMMO, Warning,
				TEXT("ZMMOPlayerState: CreateDefaultSubobject falhou para %s"),
				*CompClass->GetName());
		}
		++SubobjectIndex;
	}
}

void AZMMOPlayerState::SetPublicIdentity(int64 InEntityId, const FString& InCharId,
                                          int32 InBaseLevel, int32 InClassId)
{
	ZMMOEntityId = InEntityId;
	CharId = InCharId;
	BaseLevel = InBaseLevel;
	ClassId = InClassId;
}
