// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AgrarianPersistentStateProvider.h"
#include "AgrarianShelterActor.generated.h"

class UBoxComponent;
class UAgrarianPersistentActorComponent;
class UStaticMeshComponent;

UCLASS(Blueprintable)
class AAgrarianShelterActor : public AActor, public IAgrarianPersistentStateProvider
{
	GENERATED_BODY()

public:
	AAgrarianShelterActor();

	virtual void BeginPlay() override;
	virtual float TakeDamage(float DamageAmount, const FDamageEvent& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
	virtual void CapturePersistentState_Implementation(UAgrarianPersistentActorComponent* PersistentComponent) const override;
	virtual void ApplyPersistentState_Implementation(UAgrarianPersistentActorComponent* PersistentComponent) override;

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

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Shelter")
	TObjectPtr<UBoxComponent> ProtectionVolume;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Shelter")
	TObjectPtr<UAgrarianPersistentActorComponent> PersistentActorComponent;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Shelter", meta = (ClampMin = "0", ClampMax = "1"))
	float WeatherProtection = 0.65f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "Agrarian|Shelter|Damage", meta = (ClampMin = "1"))
	float MaxStructureHealth = 100.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_StructureHealth, Category = "Agrarian|Shelter|Damage", meta = (ClampMin = "0"))
	float CurrentStructureHealth = 100.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Shelter|Damage")
	bool bCanBeDeconstructed = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Shelter|Damage")
	bool bDestroyWhenHealthDepleted = true;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Shelter|Damage")
	bool ApplyStructureDamage(float DamageAmount, AActor* DamageCauser);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Shelter|Damage")
	bool RepairStructure(float RepairAmount);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Shelter|Damage")
	bool Deconstruct(AActor* RequestingActor);

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Shelter|Damage")
	float GetStructureHealthRatio() const;

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Shelter|Damage")
	bool IsStructureDamaged() const;

protected:
	UFUNCTION()
	void OnRep_StructureHealth();

	void ClampStructureHealth();
};
