#include "ZMMOAttributeComponent.h"

#include "Engine/GameInstance.h"
#include "ZeusNetworkSubsystem.h"

UZMMOAttributeComponent::UZMMOAttributeComponent()
{
	PrimaryComponentTick.bCanEverTick = false;
}

void UZMMOAttributeComponent::ApplySnapshot(const FZMMOAttributesSnapshot& InSnapshot)
{
	const int32 OldHp = Current.Hp;
	const int32 OldSp = Current.Sp;
	Current = InSnapshot;
	OnAttributesChanged.Broadcast(Current);
	if (OldHp != Current.Hp || OldSp != Current.Sp)
	{
		OnHpSpChanged.Broadcast(Current.Hp, Current.Sp);
	}
}

void UZMMOAttributeComponent::ApplyHpSpUpdate(int32 NewHp, int32 NewSp)
{
	if (Current.Hp == NewHp && Current.Sp == NewSp)
	{
		return;
	}
	Current.Hp = NewHp;
	Current.Sp = NewSp;
	OnHpSpChanged.Broadcast(Current.Hp, Current.Sp);
}

void UZMMOAttributeComponent::NotifyLevelUp(int32 NewBaseLevel, int32 StatusPointDelta)
{
	// O snapshot subsequente vai trazer todos os outros campos atualizados —
	// aqui apenas dispara o delegate para efeitos visuais imediatos.
	OnLevelUp.Broadcast(NewBaseLevel, StatusPointDelta);
}

void UZMMOAttributeComponent::NotifyStatAllocReply(bool bAccepted, int32 Reason)
{
	OnStatAllocReply.Broadcast(bAccepted, Reason);
}

void UZMMOAttributeComponent::RequestStatAlloc(int32 StatId, int32 Amount)
{
	UWorld* World = GetWorld();
	if (!World) { return; }
	UGameInstance* GI = World->GetGameInstance();
	if (!GI) { return; }
	UZeusNetworkSubsystem* Net = GI->GetSubsystem<UZeusNetworkSubsystem>();
	if (!Net) { return; }
	Net->SendStatAlloc(StatId, Amount);
}
