// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "DayNightGameMode.h"
#include "DayNightHUD.h"
#include "DayNightCharacter.h"
#include "DayNightGameStateBase.h"

#include "TimerManager.h"
#include "GameFramework/WorldSettings.h"
#include "UObject/ConstructorHelpers.h"

ADayNightGameMode::ADayNightGameMode() : Super()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));
	DefaultPawnClass = PlayerPawnClassFinder.Class;

	// use our custom HUD class
	HUDClass = ADayNightHUD::StaticClass();
}


void ADayNightGameMode::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	m_GS = Cast<ADayNightGameStateBase>(GetWorld()->GetGameState());

	FTimerHandle TimerHandle_TimeOfDayTimer;
	GetWorldTimerManager().SetTimer(TimerHandle_TimeOfDayTimer, this, &ADayNightGameMode::TimeOfDayTimer, GetWorldSettings()->GetEffectiveTimeDilation(), true);
}

void ADayNightGameMode::TimeOfDayTimer()
{
	if (m_GS)
	{
		int32 ElapsedGameMinutes = m_GS->GetElapsedGameMinutes();
		ElapsedGameMinutes += m_GS->GetTimeOfDayIncrement();

		m_GS->IncrementElapsedGameMinutes(m_GS->GetTimeOfDayIncrement());

		m_GS->GetAndUpdateIsNight();

		bool bCurrentIsNight = m_GS->IsNight();
		if (bCurrentIsNight != m_bLastIsNight)
		{
			if (!bCurrentIsNight)
			{
				// TODO: No longer night
			}
		}

		m_bLastIsNight = m_GS->IsNight();
	}
}

