// Copyright Pacificao. All Rights Reserved.

#include "AgrarianWaterSource.h"

#include "AgrarianGameCharacter.h"
#include "AgrarianSurvivalComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "Materials/MaterialInterface.h"
#include "UObject/ConstructorHelpers.h"

AAgrarianWaterSource::AAgrarianWaterSource()
{
	bReplicates = true;
	SetNetCullDistanceSquared(FMath::Square(6500.0f));

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

	static ConstructorHelpers::FObjectFinder<UStaticMesh> PlaneMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_Plane.SM_AGR_Placeholder_Plane"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CylinderMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_Cylinder.SM_AGR_Placeholder_Cylinder"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeMesh(TEXT("/Game/Agrarian/Environment/PlaceholderMeshes/SM_AGR_Placeholder_Cube.SM_AGR_Placeholder_Cube"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> WaterMaterial(TEXT("/Game/Agrarian/Materials/M_AGR_GZ_FreshWater.M_AGR_GZ_FreshWater"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> StoneMaterial(TEXT("/Game/Agrarian/Materials/M_AGR_GZ_Stone_Sandstone.M_AGR_GZ_Stone_Sandstone"));

	if (CylinderMesh.Succeeded())
	{
		Mesh->SetStaticMesh(CylinderMesh.Object);
		Mesh->SetRelativeScale3D(FVector(2.4f, 1.7f, 0.08f));
	}
	if (StoneMaterial.Succeeded())
	{
		Mesh->SetMaterial(0, StoneMaterial.Object);
	}

	WaterSurfaceProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WaterSurfaceProxy"));
	WaterSurfaceProxy->SetupAttachment(RootComponent);
	WaterSurfaceProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	WaterSurfaceProxy->SetGenerateOverlapEvents(false);
	if (PlaneMesh.Succeeded())
	{
		WaterSurfaceProxy->SetStaticMesh(PlaneMesh.Object);
	}
	if (WaterMaterial.Succeeded())
	{
		WaterSurfaceProxy->SetMaterial(0, WaterMaterial.Object);
	}
	WaterSurfaceProxy->SetRelativeLocation(FVector(0.0f, 0.0f, 8.0f));
	WaterSurfaceProxy->SetRelativeScale3D(FVector(2.15f, 1.45f, 1.0f));

	StoneBankProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StoneBankProxy"));
	StoneBankProxy->SetupAttachment(RootComponent);
	StoneBankProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	StoneBankProxy->SetGenerateOverlapEvents(false);
	if (CylinderMesh.Succeeded())
	{
		StoneBankProxy->SetStaticMesh(CylinderMesh.Object);
	}
	if (StoneMaterial.Succeeded())
	{
		StoneBankProxy->SetMaterial(0, StoneMaterial.Object);
	}
	StoneBankProxy->SetRelativeScale3D(FVector(2.55f, 1.85f, 0.05f));

	CollectMarkerProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CollectMarkerProxy"));
	CollectMarkerProxy->SetupAttachment(RootComponent);
	CollectMarkerProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	CollectMarkerProxy->SetGenerateOverlapEvents(false);
	if (CubeMesh.Succeeded())
	{
		CollectMarkerProxy->SetStaticMesh(CubeMesh.Object);
	}
	if (WaterMaterial.Succeeded())
	{
		CollectMarkerProxy->SetMaterial(0, WaterMaterial.Object);
	}
	CollectMarkerProxy->SetRelativeLocation(FVector(0.0f, -84.0f, 24.0f));
	CollectMarkerProxy->SetRelativeScale3D(FVector(0.22f, 0.08f, 0.32f));

	DisplayName = FText::FromString(TEXT("Fresh Water"));
}

FText AAgrarianWaterSource::GetInteractionText_Implementation(const AAgrarianGameCharacter* Interactor) const
{
	return FText::Format(NSLOCTEXT("AgrarianWaterSource", "DrinkFromWaterSource", "Drink from {0}"), DisplayName);
}

bool AAgrarianWaterSource::CanInteract_Implementation(const AAgrarianGameCharacter* Interactor) const
{
	return Interactor != nullptr && Interactor->GetSurvivalComponent() != nullptr;
}

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

	if (UAgrarianSurvivalComponent* SurvivalComponent = Interactor->GetSurvivalComponent())
	{
		SurvivalComponent->AddWater(WaterRestoreAmount);
	}
}
