// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AgrarianInteractable.h"
#include "AgrarianTypes.h"
#include "AgrarianResourceNode.generated.h"

class UStaticMeshComponent;
class UAgrarianItemDefinitionAsset;

UCLASS(Blueprintable)
class AAgrarianResourceNode : public AActor, public IAgrarianInteractable
{
	GENERATED_BODY()

public:
	AAgrarianResourceNode();

	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Resource")
	TObjectPtr<UStaticMeshComponent> Mesh;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource")
	FAgrarianItemStack YieldItem;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource")
	TObjectPtr<UAgrarianItemDefinitionAsset> YieldItemDefinition;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_RemainingHarvests, Category = "Agrarian|Resource", meta = (ClampMin = "0"))
	int32 RemainingHarvests = 5;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource", meta = (ClampMin = "1"))
	int32 QuantityPerHarvest = 1;

	virtual FText GetInteractionText_Implementation(const AAgrarianGameCharacter* Interactor) const override;
	virtual bool CanInteract_Implementation(const AAgrarianGameCharacter* Interactor) const override;
	virtual void Interact_Implementation(AAgrarianGameCharacter* Interactor) override;

protected:
	UFUNCTION()
	void OnRep_RemainingHarvests();

	FAgrarianItemStack MakeYieldStack() const;
	void UpdateDepletedState();
};
