// Copyright ExByte Studios. Item Interaction Ecosystem.

#include "ItemInteractionActions.h"
#include "ItemInteractionLog.h"
#include "ItemInteractionInterfaces.h"
#include "ItemNativeTags.h"
#include "InventoryComponent.h"
#include "GameFramework/Character.h"

// --- Instant Pickup ---

UInteractionAction_InstantPickup::UInteractionAction_InstantPickup()
{
	ActionTag = ItemEcosystemTags::Action_Pickup_Instant;
	Mode = EItemInteractionMode::InstantPickup;
	Priority = 600;
	DisplayText = NSLOCTEXT("ItemInteraction", "PickUp", "Pick Up");
}

void UInteractionAction_InstantPickup::Execute_Implementation(const FInteractionContext& Context)
{
	if (!Context.TargetActor || !Context.PlayerInventory || !Context.TargetActor->Implements<UWorldItemProvider>())
	{
		return;
	}
	IWorldItemProvider* Provider = Cast<IWorldItemProvider>(Context.TargetActor);
	FItemInstanceData Item;
	if (!Provider || !Provider->IsItemAvailable() || !Provider->GetProvidedItem(Item))
	{
		return;
	}
	const int32 Before = Item.Quantity;
	Context.PlayerInventory->AddItem(Item);
	if (Before - Item.Quantity <= 0)
	{
		return;
	}
	if (Item.Quantity <= 0) { Provider->NotifyItemTaken(Context.Character.Get()); }
	else { Provider->UpdateProvidedItem(Item); }
}

// --- Open Loot UI (client-side; no server effect) ---

UInteractionAction_OpenLootUI::UInteractionAction_OpenLootUI()
{
	ActionTag = ItemEcosystemTags::Action_Open;
	Mode = EItemInteractionMode::OpenLootUI;
	Priority = 700;
	DisplayText = NSLOCTEXT("ItemInteraction", "Open", "Open");
}

// --- Inspect ---

UInteractionAction_Inspect::UInteractionAction_Inspect()
{
	ActionTag = ItemEcosystemTags::Action_Inspect;
	Mode = EItemInteractionMode::Inspect;
	Priority = 400;
	DisplayText = NSLOCTEXT("ItemInteraction", "Inspect", "Inspect");
}

void UInteractionAction_Inspect::Execute_Implementation(const FInteractionContext& Context)
{
	UE_LOG(LogItemInteraction, Verbose, TEXT("Inspect %s"), *GetNameSafe(Context.TargetActor));
}

// --- Combine / Transfer / Split / Merge ---
// These are inventory-UI verbs; their execution runs through the interaction
// component's inventory RPCs. The classes exist as authoring units (section 16).

UInteractionAction_Combine::UInteractionAction_Combine()
{
	Mode = EItemInteractionMode::Combine;
	Priority = 300;
	DisplayText = NSLOCTEXT("ItemInteraction", "Combine", "Combine");
}

UInteractionAction_TransferContainerItem::UInteractionAction_TransferContainerItem()
{
	Mode = EItemInteractionMode::TransferContainerItem;
	Priority = 300;
	DisplayText = NSLOCTEXT("ItemInteraction", "Transfer", "Transfer");
}

UInteractionAction_SplitStack::UInteractionAction_SplitStack()
{
	Mode = EItemInteractionMode::SplitStack;
	Priority = 300;
	DisplayText = NSLOCTEXT("ItemInteraction", "Split", "Split Stack");
}

UInteractionAction_MergeStack::UInteractionAction_MergeStack()
{
	Mode = EItemInteractionMode::MergeStack;
	Priority = 300;
	DisplayText = NSLOCTEXT("ItemInteraction", "Merge", "Merge Stack");
}
