// © 2025 Naked People Team. All Rights Reserved.

#include "NakedDesireGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/Interactables/ItemPickup.h"
#include "UObject/ConstructorHelpers.h"
#include "NakedDesire/SaveGame/GlobalSaveGameData.h"
#include "NakedDesire/SaveGame/ItemSaveRecord.h"
#include "NakedDesire/SaveGame/SaveSubsystem.h"

void ANakedDesireGameMode::RestartGame()
{
	UGameplayStatics::OpenLevel(this, "City");
}

void ANakedDesireGameMode::BeginPlay()
{
	Super::BeginPlay();

	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	SpawnSavedWorldItems(SaveSubsystem->GetCurrentSave());
}

void ANakedDesireGameMode::SpawnSavedWorldItems(UGlobalSaveGameData* SaveGameData)
{
	for (const FItemSaveRecord& Item : SaveGameData->GetWorldItems())
	{
		UClothingItemInstance* NewItemInstance = Cast<UClothingItemInstance>(UItemInstance::CreateFromRecord(this, Item));
		if (!NewItemInstance)
			continue;

		AItemPickup* NewItemPickup = GetWorld()->SpawnActor<AItemPickup>(ItemPickupClass, Item.WorldTransform);
		NewItemPickup->SetItem(NewItemInstance);
	}
}
