// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class DAYNIGHT_API ATimeofDayManager : public AActor
{
	GENERATED_BODY()

	class ADayNightGameStateBase* m_GS;

	/* Cached bool of bIsNight to determine when we entered/left the night */
	bool m_bLastNightState;

	/* Sunbrightness as specified in the level (considered as daytime sun) */
	float m_OriginalSunBrightness;

	/* Target brightness to lerp towards */
	float m_TargetSunBrightness;

	/* Required difference in Sky intensity before we re-capture the sky */
	float m_RequiredCaptureDelta;

	/* Last skylight intensity that was captured */
	float m_LastCapturedIntensity;

	/** Get all of the actors in the level (SkySphere etc..) */
	void GatherConfig();


	template<typename T>
	TArray<T*> GetAllActorsWithName(const FName& NameOfActor);

protected:

	void UpdateSkylight();

	/** Skysphere actor */
	AActor* m_SkySphere;

	/** Primary sun of the level. */
	class ADirectionalLight* m_PrimarySunLight;

	/** Skylight actor */
	class ASkyLight* m_SkyLightActor;

	/** Last local time of day to apply sun movement prediction */
	float m_LastTimeOfDay;

	/** Delta seconds since the time of day was last updated */
	float m_TimeSinceLastIncrement;

public:

	/** Get the skysphere from the level. */
	UFUNCTION(BlueprintPure, Category = "TimeOfDay")
	FORCEINLINE AActor* GetSkySphere() const { return m_SkySphere; }
	
public:
	ATimeofDayManager();

protected:
	virtual void BeginPlay() override;
	virtual void Tick(float DeltaTime) override;
	

};

