// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "Timer.h"
#include "Kismet/KismetStringLibrary.h"

void UTimer::NativeConstruct()
{
	m_duration = 0.0f;
	m_text = Cast<UTextBlock>(WidgetTree->FindWidget("Text"));
	m_timerText = Cast<UTextBlock>(WidgetTree->FindWidget("Time"));
	m_clock = Cast<UImage>(WidgetTree->FindWidget("Klokje"));
	if(!m_text)
		GWARNING("No \"Text\" text widget found in " + GetName());
	if(!m_text)
		GWARNING("No \"Time\" text widget found in " + GetName());
	if(!m_clock)
		GWARNING("No \"Klokje\" image widget found in " + GetName());
	Super::NativeConstruct();
}

void UTimer::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
	m_time = FMath::Clamp(m_time - InDeltaTime, 0.0f, m_duration);
	if(m_duration == 0.0f)
		OnSetTimer(0.0f);
	else
		OnSetTimer(m_time / m_duration);

	if(m_timerText)
	{
		FString msg = UKismetStringLibrary::TimeSecondsToString(m_time);
		m_timerText->SetText(FText::FromString(msg));
	}

	Super::NativeTick(MyGeometry, InDeltaTime);
}

void UTimer::SetTimer(float duration, bool reset)
{
	m_duration = duration;
	if(reset)
		m_time = duration;
}
void UTimer::SetText(const FString& text)
{
	if(m_text)
		m_text->SetText(FText::FromString(text));
}
