// Copyright Pacificao. All Rights Reserved.

#pragma once

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

class UDirectionalLightComponent;
class UExponentialHeightFogComponent;
class USceneComponent;
class USkyLightComponent;

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

public:
	AAgrarianSkyLightingController();

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

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

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Sky")
	TObjectPtr<UDirectionalLightComponent> SunLight;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Sky")
	TObjectPtr<USkyLightComponent> SkyLight;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Sky")
	TObjectPtr<UExponentialHeightFogComponent> HeightFog;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float NoonSunIntensity = 8.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float NightSunIntensity = 0.03f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float ClearSkyLightIntensity = 1.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float NightSkyLightIntensity = 0.08f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float ClearFogDensity = 0.008f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float StormFogDensity = 0.05f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Sky")
	float NorthYawDegrees = -35.0f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Sky")
	float CurrentSunAlpha = 0.0f;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Sky")
	float CurrentCloudAlpha = 0.0f;

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

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Sky")
	void RefreshSkyLighting();

protected:
	float CalculateSunAlpha(float HourOfDay, float SunriseHour, float SunsetHour) const;
	float CalculateWeatherCloudAlpha(EAgrarianWeatherType Weather, float ProviderCloudCoverPercent, bool bHasProviderCloudCover) const;
	FLinearColor CalculateSunColor(float SunAlpha, float CloudAlpha) const;
};
