// Copyright ExByte Studios. Item Interaction Ecosystem.

#include "PlacementInteractionActions.h"
#include "PlacementPreviewComponent.h"
#include "ItemNativeTags.h"
#include "GameFramework/Character.h"

namespace
{
	UPlacementPreviewComponent* GetPlacement(const FInteractionContext& Context)
	{
		return Context.Character ? Context.Character->FindComponentByClass<UPlacementPreviewComponent>() : nullptr;
	}
}

UInteractionAction_StartPlacement::UInteractionAction_StartPlacement()
{
	ActionTag = ItemEcosystemTags::Action_Place;
	Mode = EItemInteractionMode::StartPlacement;
	Priority = 300;
	DisplayText = NSLOCTEXT("ItemPlacement", "Place", "Place");
}
void UInteractionAction_StartPlacement::Execute_Implementation(const FInteractionContext& Context)
{
	if (UPlacementPreviewComponent* P = GetPlacement(Context))
	{
		const FGuid Id = Context.bHasTargetItem ? Context.TargetItem.InstanceId
			: (Context.bHasHeldItem ? Context.HeldItem.InstanceId : FGuid());
		if (Id.IsValid()) { P->StartPlacement(Id); }
	}
}

UInteractionAction_ConfirmPlacement::UInteractionAction_ConfirmPlacement()
{
	Mode = EItemInteractionMode::ConfirmPlacement;
	Priority = 1000;
	DisplayText = NSLOCTEXT("ItemPlacement", "Confirm", "Confirm");
}
void UInteractionAction_ConfirmPlacement::Execute_Implementation(const FInteractionContext& Context)
{
	if (UPlacementPreviewComponent* P = GetPlacement(Context)) { P->ConfirmPlacement(); }
}

UInteractionAction_CancelPlacement::UInteractionAction_CancelPlacement()
{
	Mode = EItemInteractionMode::CancelPlacement;
	Priority = 1000;
	DisplayText = NSLOCTEXT("ItemPlacement", "Cancel", "Cancel");
}
void UInteractionAction_CancelPlacement::Execute_Implementation(const FInteractionContext& Context)
{
	if (UPlacementPreviewComponent* P = GetPlacement(Context)) { P->CancelPlacement(); }
}
