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


#include "GlobalSaveGameData.h"

#include "ItemSaveRecord.h"
#include "NakedDesire/Global/Constants.h"
#include "NakedDesire/Items/ItemInstance.h"
#include "Kismet/GameplayStatics.h"

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

	NewSave->Money = STARTING_MONEY;

	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(UGlobalSaveGameData* SaveGameData, const FString& SlotName)
{
	return UGameplayStatics::SaveGameToSlot(SaveGameData, SlotName, SLOT_PLAYER);
}

FItemSaveRecord UGlobalSaveGameData::AddWardrobeItem(const UItemInstance* ItemInstance)
{
	FItemSaveRecord NewSaveRecord = ItemInstance->ToSaveRecord();
	WardrobeItems.Push(NewSaveRecord);
	return NewSaveRecord;
}

bool UGlobalSaveGameData::UpdateWardrobeItem(UItemInstance* ItemInstance)
{
	for (auto& ItemSaveRecord : WardrobeItems)
	{
		if (ItemSaveRecord.InstanceId == ItemInstance->GetInstanceId())
		{
			ItemSaveRecord = ItemInstance->ToSaveRecord();
			return true;
		}
	}

	return false;
}

bool UGlobalSaveGameData::RemoveWardrobeItem(UItemInstance* ItemInstance)
{
	const int32 RemovedElementsCount = WardrobeItems.RemoveAll([ItemInstance](const FItemSaveRecord& Item)
	{
		return Item.InstanceId == ItemInstance->GetInstanceId();
	});

	return RemovedElementsCount > 0;
}

FItemSaveRecord UGlobalSaveGameData::AddEquippedItem(const UItemInstance* ItemInstance)
{
	FItemSaveRecord NewSaveRecord = ItemInstance->ToSaveRecord();
	EquippedItems.Push(NewSaveRecord);
	return NewSaveRecord;
}

void UGlobalSaveGameData::AddEquippedItem(const FItemSaveRecord& ItemRecord)
{
	EquippedItems.Push(ItemRecord);
}

bool UGlobalSaveGameData::UpdateEquippedItem(UItemInstance* ItemInstance)
{
	for (auto& ItemSaveRecord : EquippedItems)
	{
		if (ItemSaveRecord.InstanceId == ItemInstance->GetInstanceId())
		{
			ItemSaveRecord = ItemInstance->ToSaveRecord();
			return true;
		}
	}

	return false;
}

bool UGlobalSaveGameData::RemoveEquippedItem(UItemInstance* ItemInstance)
{
	const int32 RemovedElementsCount = EquippedItems.RemoveAll([ItemInstance](const FItemSaveRecord& Item)
	{
		return Item.InstanceId == ItemInstance->GetInstanceId();
	});

	return RemovedElementsCount > 0;
}

FItemSaveRecord UGlobalSaveGameData::AddWorldItem(const UItemInstance* ItemInstance, FTransform Transform)
{
	FItemSaveRecord NewSaveRecord = ItemInstance->ToSaveRecord();
	NewSaveRecord.WorldTransform = Transform;
	WorldItems.Push(NewSaveRecord);
	return NewSaveRecord;
}

bool UGlobalSaveGameData::UpdateWorldItem(UItemInstance* ItemInstance, FTransform Transform)
{
	for (auto& ItemSaveRecord : WorldItems)
	{
		if (ItemSaveRecord.InstanceId == ItemInstance->GetInstanceId())
		{
			ItemSaveRecord = ItemInstance->ToSaveRecord();
			ItemSaveRecord.WorldTransform = Transform;
			return true;
		}
	}

	return false;
}

bool UGlobalSaveGameData::RemoveWorldItem(UItemInstance* ItemInstance)
{
	const int32 RemovedElementsCount = WorldItems.RemoveAll([ItemInstance](const FItemSaveRecord& Item)
	{
		return Item.InstanceId == ItemInstance->GetInstanceId();
	});

	return RemovedElementsCount > 0;
}