// Copyright Pacificao. All Rights Reserved.

#pragma once

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

class UPointLightComponent;
class UParticleSystemComponent;
class UAgrarianPersistentActorComponent;
class UStaticMeshComponent;

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

public:
	AAgrarianCampfire();

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

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

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Fire")
	TObjectPtr<UPointLightComponent> FireLight;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Fire|Effects")
	TObjectPtr<UParticleSystemComponent> SmokeEffect;

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

	UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_FireState, Category = "Agrarian|Fire")
	bool bLit = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "Agrarian|Fire", meta = (ClampMin = "0"))
	float FuelSeconds = 0.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire", meta = (ClampMin = "0"))
	float WarmthRadius = 550.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire", meta = (ClampMin = "0"))
	float WarmthPerSecond = 0.02f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "Agrarian|Fire|Cooking")
	bool bCookingPlaceholderEnabled = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "Agrarian|Fire|Cooking", meta = (ClampMin = "0"))
	float CookingSecondsRequired = 30.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "Agrarian|Fire|Cooking", meta = (ClampMin = "0"))
	float CookingProgressSeconds = 0.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire|Weather", meta = (ClampMin = "1"))
	float RainFuelDrainMultiplier = 1.5f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire|Weather", meta = (ClampMin = "1"))
	float StormFuelDrainMultiplier = 2.5f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire|Weather", meta = (ClampMin = "0"))
	float WetWeatherExtinguishFuelThresholdSeconds = 6.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Fire|Weather")
	bool bWetWeatherCanExtinguish = true;

	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;
	virtual void CapturePersistentState_Implementation(UAgrarianPersistentActorComponent* PersistentComponent) const override;
	virtual void ApplyPersistentState_Implementation(UAgrarianPersistentActorComponent* PersistentComponent) override;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Fire")
	void AddFuel(float Seconds);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Fire")
	void Extinguish();

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Fire|Cooking")
	bool CanCook() const;

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Fire|Cooking")
	float GetCookingProgressRatio() const;

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Fire|Weather")
	float GetWeatherFuelDrainMultiplier() const;

	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Agrarian|Fire|Weather")
	bool IsWetWeatherActive() const;

protected:
	UFUNCTION()
	void OnRep_FireState();

	EAgrarianWeatherType GetCurrentWeather() const;
	void SetLit(bool bNewLit);
	void UpdateVisualState();
	void WarmNearbyCharacters(float DeltaSeconds);
};
