// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimerManager.h"
#include "AgrarianInteractable.h"
#include "AgrarianSaveGame.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 BeginPlay() override;
	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, Category = "Agrarian|Resource|Persistence")
	FName PersistenceNodeId = NAME_None;

	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;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Tools")
	FName RequiredToolItemId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Tools")
	bool bAllowBareHandGathering = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Tools", meta = (ClampMin = "0"))
	int32 ToolQuantityBonus = 0;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Respawn")
	bool bRespawnsForMvp = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Respawn", meta = (ClampMin = "1"))
	float RespawnDelaySeconds = 900.0f;

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

	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;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
	FName GetResourcePersistenceId() const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
	FAgrarianSavedResourceNode CaptureResourceSaveState() const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
	void ApplyResourceSaveState(const FAgrarianSavedResourceNode& SavedNode);

protected:
	UFUNCTION()
	void OnRep_RemainingHarvests();

	bool HasRequiredTool(const AAgrarianGameCharacter* Interactor) const;
	int32 GetHarvestQuantityFor(const AAgrarianGameCharacter* Interactor) const;
	FAgrarianItemStack MakeYieldStack(const AAgrarianGameCharacter* Interactor) const;
	void UpdateDepletedState();
	void ScheduleRespawnIfNeeded();
	void RespawnNode();

	FTimerHandle RespawnTimerHandle;
};
