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


#include "LocationConstraint.h"

#include "NakedDesire/Locations/LocationSubsystem.h"
#include "NakedDesire/Player/NakedDesireCharacter.h"

#define LOCTEXT_NAMESPACE "Commissions.Constraints.Location"

void ULocationConstraint::OnActivate()
{
	if (ULocationSubsystem* Locations = GetLocations())
	{
		Locations->OnLocationEntered.AddUniqueDynamic(this, &ULocationConstraint::HandleLocationChanged);
		Locations->OnLocationExited.AddUniqueDynamic(this, &ULocationConstraint::HandleLocationChanged);
	}
}

void ULocationConstraint::OnDeactivate()
{
	if (ULocationSubsystem* Locations = GetLocations())
	{
		Locations->OnLocationEntered.RemoveDynamic(this, &ULocationConstraint::HandleLocationChanged);
		Locations->OnLocationExited.RemoveDynamic(this, &ULocationConstraint::HandleLocationChanged);
	}
}

bool ULocationConstraint::IsMet() const
{
	const ULocationSubsystem* Locations = GetLocations();
	return Locations && Locations->IsPlayerInLocation(RequiredLocation);
}

void ULocationConstraint::HandleLocationChanged(ULocationData* Location)
{
	NotifyChanged();
}

ULocationSubsystem* ULocationConstraint::GetLocations() const
{
	UWorld* World = Player ? Player->GetWorld() : nullptr;
	return World ? World->GetSubsystem<ULocationSubsystem>() : nullptr;
}

FText ULocationConstraint::GetDescription() const
{
	return FText::Format(LOCTEXT("AtLocation", "while at {0}"), FText::FromName(RequiredLocation.GetTagName()));
}

#undef LOCTEXT_NAMESPACE