// Copyright Pacificao. All Rights Reserved.

#include "AgrarianItemPickup.h"
#include "AgrarianGameCharacter.h"
#include "AgrarianInventoryComponent.h"
#include "AgrarianItemDefinitionAsset.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "Materials/MaterialInterface.h"
#include "UObject/ConstructorHelpers.h"

AAgrarianItemPickup::AAgrarianItemPickup()
{
	bReplicates = true;
	SetNetCullDistanceSquared(FMath::Square(3000.0f));

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	RootComponent = Mesh;
	Mesh->SetCollisionProfileName(TEXT("BlockAll"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> ChamferCubeMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_ChamferCube.SM_AGR_Placeholder_ChamferCube"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_Cube.SM_AGR_Placeholder_Cube"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CylinderMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_Cylinder.SM_AGR_Placeholder_Cylinder"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> WoodMaterial(TEXT("/Game/Agrarian/Materials/M_AGR_GZ_Wood_Resource.M_AGR_GZ_Wood_Resource"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> FiberMaterial(TEXT("/Game/Agrarian/Materials/M_AGR_GZ_Fiber_Resource.M_AGR_GZ_Fiber_Resource"));

	if (ChamferCubeMesh.Succeeded())
	{
		Mesh->SetStaticMesh(ChamferCubeMesh.Object);
		Mesh->SetRelativeScale3D(FVector(0.42f, 0.32f, 0.2f));
	}
	if (WoodMaterial.Succeeded())
	{
		Mesh->SetMaterial(0, WoodMaterial.Object);
	}

	BundleProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BundleProxy"));
	BundleProxy->SetupAttachment(RootComponent);
	BundleProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	BundleProxy->SetGenerateOverlapEvents(false);
	if (CylinderMesh.Succeeded())
	{
		BundleProxy->SetStaticMesh(CylinderMesh.Object);
	}
	if (WoodMaterial.Succeeded())
	{
		BundleProxy->SetMaterial(0, WoodMaterial.Object);
	}
	BundleProxy->SetRelativeLocation(FVector(0.0f, 0.0f, 16.0f));
	BundleProxy->SetRelativeRotation(FRotator(0.0f, 90.0f, 90.0f));
	BundleProxy->SetRelativeScale3D(FVector(0.12f, 0.12f, 0.58f));

	StrapProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StrapProxy"));
	StrapProxy->SetupAttachment(RootComponent);
	StrapProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	StrapProxy->SetGenerateOverlapEvents(false);
	if (CubeMesh.Succeeded())
	{
		StrapProxy->SetStaticMesh(CubeMesh.Object);
	}
	if (FiberMaterial.Succeeded())
	{
		StrapProxy->SetMaterial(0, FiberMaterial.Object);
	}
	StrapProxy->SetRelativeLocation(FVector(0.0f, 0.0f, 18.0f));
	StrapProxy->SetRelativeScale3D(FVector(0.08f, 0.54f, 0.08f));

	ItemMarkerProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ItemMarkerProxy"));
	ItemMarkerProxy->SetupAttachment(RootComponent);
	ItemMarkerProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	ItemMarkerProxy->SetGenerateOverlapEvents(false);
	if (CubeMesh.Succeeded())
	{
		ItemMarkerProxy->SetStaticMesh(CubeMesh.Object);
	}
	if (FiberMaterial.Succeeded())
	{
		ItemMarkerProxy->SetMaterial(0, FiberMaterial.Object);
	}
	ItemMarkerProxy->SetRelativeLocation(FVector(0.0f, -18.0f, 26.0f));
	ItemMarkerProxy->SetRelativeScale3D(FVector(0.16f, 0.04f, 0.16f));

	PickupStack.ItemId = TEXT("wood");
	PickupStack.DisplayName = FText::FromString(TEXT("Wood"));
	PickupStack.Quantity = 1;
	PickupStack.UnitWeight = 1.0f;
	InteractionVerb = FText::FromString(TEXT("Pick up"));
}

FText AAgrarianItemPickup::GetInteractionText_Implementation(const AAgrarianGameCharacter* Interactor) const
{
	const FAgrarianItemStack Stack = MakePickupStack();
	const FText DisplayName = Stack.DisplayName.IsEmpty() ? FText::FromName(Stack.ItemId) : Stack.DisplayName;
	const FString Verb = InteractionVerb.IsEmpty() ? FString(TEXT("Pick up")) : InteractionVerb.ToString();

	if (Stack.Quantity > 1)
	{
		return FText::FromString(FString::Printf(TEXT("%s %s x%d"), *Verb, *DisplayName.ToString(), Stack.Quantity));
	}

	return FText::FromString(FString::Printf(TEXT("%s %s"), *Verb, *DisplayName.ToString()));
}

bool AAgrarianItemPickup::CanInteract_Implementation(const AAgrarianGameCharacter* Interactor) const
{
	return Interactor != nullptr && MakePickupStack().IsValidStack();
}

void AAgrarianItemPickup::Interact_Implementation(AAgrarianGameCharacter* Interactor)
{
	if (!HasAuthority() || !Interactor)
	{
		return;
	}

	UAgrarianInventoryComponent* Inventory = Interactor->GetInventoryComponent();
	if (!Inventory)
	{
		return;
	}

	const FAgrarianItemStack Stack = MakePickupStack();
	if (Inventory->AddItem(Stack))
	{
		Destroy();
	}
}

FAgrarianItemStack AAgrarianItemPickup::MakePickupStack() const
{
	if (ItemDefinition)
	{
		return ItemDefinition->MakeStack(Quantity);
	}

	FAgrarianItemStack Stack = PickupStack;
	if (Quantity > 0)
	{
		Stack.Quantity = Quantity;
	}
	return Stack;
}
