// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "PlayerSlot.h"
#include "DefaultGameInstance.h"
#include "PlayerStateBase.h"

UPlayerSlot::UPlayerSlot(const FObjectInitializer& init)
	: Super(init)
{
}
void UPlayerSlot::NativeConstruct()
{
	Super::NativeConstruct();
	currentPlayerState = nullptr;
	isLocal = false;
	OnChanged();
}
void UPlayerSlot::Init(APlayerStateBase* playerState, bool isLocal)
{
	if(!playerState && m_playerStateSet)
	{
		// Clear this slot
		m_playerStateSet = false;
		OnChanged();
		return;
	}
	if (currentPlayerState == playerState)
		return;

	this->isLocal = isLocal;
	if (playerState)
	{
		// Get the player info to display in this player slot
		const TSharedPtr<const FUniqueNetId> netID = playerState->UniqueId.GetUniqueNetId();
		if (netID.IsValid())
		{
			UWorld* const world = GetWorld();
			check(world);
			UDefaultGameInstance* gameInstance = Cast<UDefaultGameInstance>(world->GetGameInstance());
			playerState->UpdatePersona();
		}
		else
		{
			// Invalid NetID somehow
			GWWARNING(L"NetID is invalid");
			return;
		}
		m_playerStateSet = true;
	}
	currentPlayerState = playerState;

	OnChanged();
}

void UPlayerSlot::OnChanged_Implementation()
{
}