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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "ClothingSlotType.h"
#include "ClothingVisualsComponent.generated.h"

class UClothingManager;
class UClothingItemInstance;
class USkeletalMeshComponent;

/**
 * Renders equipped clothing by lazily spawning one skeletal mesh component per
 * occupied slot, parented to a target body mesh. Reacts to a UClothingManager's
 * equip/unequip events, so the character, mirror impostor, and cinematic double
 * share one implementation instead of hand-maintained per-slot components.
 */
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class NAKEDDESIRE_API UClothingVisualsComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UClothingVisualsComponent();

	/**
	 * Bind to a clothing manager and a body mesh (the leader-pose source and
	 * attach parent for spawned slot meshes), then apply the current equipped set.
	 * Call after the manager has hydrated (e.g. owning actor's BeginPlay).
	 */
	void Initialize(USkeletalMeshComponent* InBodyMesh, UClothingManager* InClothingManager);

	void Apply(const UClothingItemInstance* ClothingItemInstance);
	void ClearSlot(EClothingSlotType SlotType);

private:
	UFUNCTION()
	void HandleClothingEquip(UClothingItemInstance* ClothingItemInstance);

	UFUNCTION()
	void HandleClothingUnequip(UClothingItemInstance* ClothingItemInstance);

	/** Returns the slot's mesh component, creating + attaching it on first use. */
	USkeletalMeshComponent* GetOrCreateSlotMesh(EClothingSlotType SlotType);

	UPROPERTY()
	TObjectPtr<USkeletalMeshComponent> BodyMesh;

	UPROPERTY()
	TObjectPtr<UClothingManager> ClothingManager;

	UPROPERTY()
	TMap<EClothingSlotType, TObjectPtr<USkeletalMeshComponent>> SlotMeshes;
};