// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AgrarianTypes.h"
#include "AgrarianWeatherAudioController.generated.h"

class UAudioComponent;
class USceneComponent;
class USoundBase;

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

public:
	AAgrarianWeatherAudioController();

	virtual void BeginPlay() override;
	virtual void Tick(float DeltaSeconds) override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	TObjectPtr<USceneComponent> SceneRoot;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	TObjectPtr<UAudioComponent> AmbientAudio;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	TObjectPtr<UAudioComponent> RainAudio;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	TObjectPtr<UAudioComponent> WindAudio;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	TObjectPtr<UAudioComponent> StormAudio;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio")
	TObjectPtr<USoundBase> ClearAmbientSound;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio")
	TObjectPtr<USoundBase> RainLoopSound;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio")
	TObjectPtr<USoundBase> WindLoopSound;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio")
	TObjectPtr<USoundBase> StormLoopSound;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.1"))
	float VolumeInterpSpeed = 1.5f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.0", ClampMax = "1.0"))
	float AmbientDayVolume = 0.35f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.0", ClampMax = "1.0"))
	float AmbientNightVolume = 0.22f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.0", ClampMax = "1.0"))
	float MaxRainVolume = 0.8f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.0", ClampMax = "1.0"))
	float MaxWindVolume = 0.7f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Weather Audio", meta = (ClampMin = "0.0", ClampMax = "1.0"))
	float MaxStormVolume = 0.9f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	EAgrarianWeatherType CurrentWeather = EAgrarianWeatherType::Clear;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	float CurrentAmbientVolume = 0.0f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	float CurrentRainVolume = 0.0f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	float CurrentWindVolume = 0.0f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Weather Audio")
	float CurrentStormVolume = 0.0f;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Weather Audio")
	void RefreshWeatherAudio(float DeltaSeconds);

protected:
	void AssignConfiguredSounds();
	void ApplyComponentVolume(UAudioComponent* AudioComponent, float Volume) const;
	float GetProviderWindAlpha(float WindSpeedKmh, bool bHasProviderData) const;
};
