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


#include "ItemPickup.h"
#include "Components/BoxComponent.h"
#include "Components/WidgetComponent.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Clothing/ClothingItemDefinition.h"
#include "NakedDesire/Clothing/DroppedClothingSubsystem.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/Clothing/ClothingManager.h"
#include "NakedDesire/Player/NakedDesireCharacter.h"
#include "NakedDesire/SaveGame/GlobalSaveGameData.h"
#include "NakedDesire/SaveGame/SaveSubsystem.h"

AItemPickup::AItemPickup()
{
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	SetRootComponent(Mesh);
	Mesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	
	Collider = CreateDefaultSubobject<UBoxComponent>(TEXT("Collider"));
	Collider->SetupAttachment(RootComponent);
	Collider->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	
	InteractionHint = CreateDefaultSubobject<UWidgetComponent>(TEXT("Interaction Hint"));
	InteractionHint->SetupAttachment(RootComponent);
}

void AItemPickup::Interact_Implementation(ANakedDesireCharacter* Player)
{
	if (ClothingItemInstance)
	{
		USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
		Player->ClothingManager->TakeClothing(ClothingItemInstance);
		SaveSubsystem->GetCurrentSave()->RemoveWorldItem(ClothingItemInstance);
		Destroy();
	}
}

bool AItemPickup::CanInteract_Implementation(ANakedDesireCharacter* Player) const
{
	return ClothingItemInstance != nullptr;
}

void AItemPickup::HideInteractionHint_Implementation()
{
	ApplyOutline(Mesh, false, 0);
	InteractionHint->SetVisibility(false);
}

void AItemPickup::ShowInteractionFocusHint_Implementation()
{
	ApplyOutline(Mesh, true, 2);
	InteractionHint->SetVisibility(true);
}

void AItemPickup::ShowInteractionProximityHint_Implementation()
{
	ApplyOutline(Mesh, true, 1);
	InteractionHint->SetVisibility(false);
}

void AItemPickup::BeginPlay()
{
	Super::BeginPlay();

	InteractionHint->SetVisibility(false);

	if (UDroppedClothingSubsystem* Dropped = GetWorld()->GetSubsystem<UDroppedClothingSubsystem>())
		Dropped->RegisterPickup(this);
}

void AItemPickup::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	if (UDroppedClothingSubsystem* Dropped = GetWorld()->GetSubsystem<UDroppedClothingSubsystem>())
		Dropped->UnregisterPickup(this);

	Super::EndPlay(EndPlayReason);
}

void AItemPickup::SetItem(UClothingItemInstance* InItem)
{
	ClothingItemInstance = InItem;
	
	Mesh->SetStaticMesh(InItem->GetClothingItemDefinition()->StaticMesh);
	if (!InItem->GetClothingItemDefinition()->Materials.IsEmpty())
	{
		for (const TPair<FName, UMaterialInstance*>& Material : InItem->GetClothingItemDefinition()->Materials)
		{
			Mesh->SetMaterialByName(Material.Key, Material.Value);
		}
	}
}

void AItemPickup::ApplyOutline(UStaticMeshComponent* InMesh, bool bEnabled, int32 StencilValue)
{
	InMesh->SetRenderCustomDepth(bEnabled);
	InMesh->SetCustomDepthStencilValue(StencilValue);
}
