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


#include "InteractableBase.h"
#include "Components/WidgetComponent.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/InteractionSystem/InteractionManager.h"
#include "NakedDesire/Player/NakedDesireCharacter.h"


AInteractableBase::AInteractableBase()
{
	PrimaryActorTick.bCanEverTick = true;
	PrimaryActorTick.TickInterval = 0.25f;

	RootSceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	RootComponent = RootSceneComponent;

	WidgetAnchor = CreateDefaultSubobject<USceneComponent>(TEXT("Widget Anchor"));
	WidgetAnchor->SetupAttachment(RootComponent);

	InteractionTrigger = CreateDefaultSubobject<UWidgetComponent>(TEXT("Interaction Trigger"));
	InteractionTrigger->SetupAttachment(WidgetAnchor);

	InteractionTrigger->SetDrawSize(FVector2D(35, 35));
	InteractionTrigger->SetWidgetSpace(EWidgetSpace::Screen);
	InteractionTrigger->SetWindowFocusable(false);
}

void AInteractableBase::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	if (!Player)
	{
		if (ACharacter* PlayerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
		{
			Player = Cast<ANakedDesireCharacter>(PlayerCharacter);
		}
	}

	if (Player)
	{
		const TScriptInterface<IInteractionTarget> NearestInteractionTarget = Player->InteractionManager->GetNearestInteractionTarget();
		if (NearestInteractionTarget.GetObject() == this)
		{
			InteractionTrigger->SetVisibility(true);
		}
		else
		{
			InteractionTrigger->SetVisibility(false);
		}
	}
}

