// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AgrarianWildlifeSpawnManager.generated.h"

class AAgrarianWildlifeBase;

UCLASS(Blueprintable)
class AAgrarianWildlifeSpawnManager : public AActor
{
	GENERATED_BODY()

public:
	AAgrarianWildlifeSpawnManager();

	virtual void BeginPlay() override;
	virtual void Tick(float DeltaSeconds) override;
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning")
	TSubclassOf<AAgrarianWildlifeBase> WildlifeClass;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning", meta = (ClampMin = "0"))
	int32 InitialSpawnCount = 3;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning", meta = (ClampMin = "0"))
	int32 MaxActiveWildlife = 6;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning", meta = (ClampMin = "0"))
	float SpawnRadius = 3500.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning", meta = (ClampMin = "0.1"))
	float SpawnIntervalSeconds = 20.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning")
	bool bSpawnOnBeginPlay = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning")
	bool bProjectSpawnsToNavigation = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Wildlife|Spawning")
	FVector NavigationProjectionExtent = FVector(650.0f, 650.0f, 900.0f);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Wildlife|Spawning")
	AAgrarianWildlifeBase* SpawnWildlife();

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Wildlife|Spawning")
	int32 GetActiveWildlifeCount() const;

protected:
	void SpawnInitialWildlife();
	bool ChooseSpawnLocation(FVector& OutLocation) const;
	void RemoveInvalidSpawnedWildlife();

	UPROPERTY(Replicated)
	TArray<TObjectPtr<AAgrarianWildlifeBase>> SpawnedWildlife;

	float SpawnTimer = 0.0f;
};
