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


#include "ExposeBodyPartRestriction.h"

#include "NakedDesire/Clothing/ClothingItem.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/Player/NakedDesireCharacter.h"
#include "NakedDesire/Clothing/ClothingManager.h"

#define LOCTEXT_NAMESPACE "Missions.Restrictions.ExposeBodyPart"
const FText RestrictionsExposeBodyPartDescription = LOCTEXT("Description", "Expose {BodyPart}");
#undef LOCTEXT_NAMESPACE

void UExposeBodyPartRestriction::Init(ANakedDesireCharacter* PlayerCharacter)
{
	Super::Init(PlayerCharacter);

	PlayerCharacter->ClothingManager->OnClothingEquip.AddUniqueDynamic(this, &UExposeBodyPartRestriction::EquipClothing);
	PlayerCharacter->ClothingManager->OnClothingUnequip.AddUniqueDynamic(this, &UExposeBodyPartRestriction::UnequipClothing);

	CheckClothing();
}

void UExposeBodyPartRestriction::Stop()
{
	Super::Stop();

	Player->ClothingManager->OnClothingEquip.Remove(this, TEXT("ClothingEquip"));
	Player->ClothingManager->OnClothingUnequip.Remove(this, TEXT("ClothingUnequip"));
}

FText UExposeBodyPartRestriction::GetDescription() const
{
	FText Part = UPrivateBodyPartType::GetString(BodyPart);
	
	return FText::Format(RestrictionsExposeBodyPartDescription,
		FFormatNamedArguments
		{
			{TEXT("BodyPart"), Part}
		});
}

void UExposeBodyPartRestriction::EquipClothing(const UClothingItemInstance* ClothingItemInstance)
{
	if (IsSuccess && ClothingItemInstance->GetClothingItem()->CoveredBodyParts.Contains(BodyPart))
	{
		Init(Player);
	}
}

void UExposeBodyPartRestriction::UnequipClothing(const UClothingItemInstance* ClothingItemInstance)
{
	if (IsSuccess)
		return;

	CheckClothing();
}

void UExposeBodyPartRestriction::CheckClothing()
{
	if (!Player || !Player->ClothingManager || Player->ClothingManager->ClothingSlots.IsEmpty())
	{
		return;
	}
	
	const FClothingSlotData* TargetClothingItem = Player->ClothingManager->ClothingSlots.FindByPredicate([this](const FClothingSlotData& ClothingSlot)
	{
		if (ClothingSlot.ClothingItemInstance)
		{
			return ClothingSlot.ClothingItemInstance->GetClothingItem()->CoveredBodyParts.Contains(BodyPart);
		}

		return false;
	});
	if (!TargetClothingItem)
	{
		Complete();
	}
}
