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


#include "Wardrobe.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/SaveGame/GlobalSaveGameData.h"
#include "NakedDesire/SaveGame/ItemSaveRecord.h"
#include "NakedDesire/SaveGame/SaveSubsystem.h"

void AWardrobe::AddItem(UClothingItemInstance* ClothingItemInstance)
{
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	SaveSubsystem->GetCurrentSave()->AddWardrobeItem(ClothingItemInstance);
}

void AWardrobe::RemoveItem(UClothingItemInstance* ClothingItemInstance) const
{
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	
	SaveSubsystem->GetCurrentSave()->RemoveWardrobeItem(ClothingItemInstance);
}

void AWardrobe::BeginPlay()
{
	Super::BeginPlay();
	
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	UGlobalSaveGameData* SaveGame = SaveSubsystem->GetCurrentSave();
	
	for (const FItemSaveRecord& ItemSaveRecord : SaveGame->GetWardrobeItems())
	{
		UClothingItemInstance* NewItemInstance = Cast<UClothingItemInstance>(UItemInstance::CreateFromRecord(this, ItemSaveRecord));
		if (!NewItemInstance)
			continue;

		ClothingItems.Push(NewItemInstance);
	}
}
