#include "SkyPortalRunner.h"
#include "SkyPortalSubsystem.h"

FPortalStatusChecker::FPortalStatusChecker(UEngineSubsystem* InSubsystem, float InCheckInterval)
	: SkyPortalSubsystemRef(InSubsystem), CheckInterval(InCheckInterval), bShouldRun(true)
{
}

bool FPortalStatusChecker::Init()
{
	// Initialization logic, if necessary (e.g., logging)
	return true;
}

uint32 FPortalStatusChecker::Run()
{
	// Main loop of the thread, runs until Stop() is called
	while (bShouldRun)
	{
		// Check the portal status
		CheckPortalStatus();

		// Sleep for the specified interval
		FPlatformProcess::Sleep(CheckInterval);
	}

	return 0;  // Exit code for the thread
}

void FPortalStatusChecker::Stop()
{
	// Signal the thread to stop running
	bShouldRun = false;
}

void FPortalStatusChecker::Exit()
{
	// Cleanup after the thread has stopped
}

void FPortalStatusChecker::CheckPortalStatus()
{
	// Ensure the subsystem is valid
	if (SkyPortalSubsystemRef && Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef))
	{
		USkyPortalIO* PortalHandleRef = Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef)->PortalHandle;
		UE_LOG(LogSkyportalIO, Verbose, TEXT("Check portal"));
		if (PortalHandleRef && PortalHandleRef->bPortalReady)
		{
			// Call the subsystem function to get portal status

			Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef);

			// Do something with the status (log, notify, etc.)
			UE_LOG(LogSkyportalIO, Verbose, TEXT("Portal Status:"));
		}

	}
}