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


#include "GlobalSaveGameData.h"
#include "NakedDesire/Global/Constants.h"
#include "Kismet/GameplayStatics.h"

UGlobalSaveGameData* UGlobalSaveGameData::CreateNewSaveGame()
{
	UGlobalSaveGameData* NewSave = Cast<UGlobalSaveGameData>(UGameplayStatics::CreateSaveGameObject(StaticClass()));

	if (!NewSave)
	{
		return nullptr;
	}

	return NewSave;
}

UGlobalSaveGameData* UGlobalSaveGameData::LoadOrCreateSaveGame(const FString& SlotName)
{
	if (UGlobalSaveGameData* ExistingSave = LoadGame(SlotName))
		return ExistingSave;

	UGlobalSaveGameData* NewSave = CreateNewSaveGame();

	if (NewSave)
	{
		UGameplayStatics::SaveGameToSlot(NewSave, SlotName, SLOT_PLAYER);
	}

	return NewSave;
}

UGlobalSaveGameData* UGlobalSaveGameData::LoadGame(const FString& SlotName)
{
	if (UGameplayStatics::DoesSaveGameExist(SlotName, SLOT_PLAYER))
	{
		return Cast<UGlobalSaveGameData>(UGameplayStatics::LoadGameFromSlot(SlotName, SLOT_PLAYER));
	}
	
	return nullptr;
}

bool UGlobalSaveGameData::SaveGame(const FString& SlotName)
{
	if (UGlobalSaveGameData* Save = CreateNewSaveGame())
	{
		return UGameplayStatics::SaveGameToSlot(Save, SlotName, SLOT_PLAYER);
	}

	return false;
}
