#pragma once 

#include "Core.h"
#include "HAL/Runnable.h"
#include "SkyPortalFigure.h"

#include "SkyPortalRunner.generated.h"

USTRUCT(BlueprintType)
struct FPortalStatusData
{
	GENERATED_BODY()

	// Array of statuses
	UPROPERTY(BlueprintReadOnly, EditFixedSize, Category = "SkyPortal|Figure", meta = (EditFixedOrder))
	TArray<EFigureStatus> StatusArray;

	// timestamp.
	// only one byte long. This means that after the value 0xFF, it overflows back to 0x00.
	// Since these are so far apart, it can be assumed that 0x00 is newer than anything in the range 0xF0 - 0xFF.
	UPROPERTY(BlueprintReadOnly, Category = "SkyPortal|Figure")
	uint8 Counter;

	// Should always be true
	UPROPERTY(BlueprintReadOnly, Category = "SkyPortal|Figure")
	bool bIsReady;

	explicit FPortalStatusData(uint8 ArraySize = 16, EFigureStatus DefaultStatus = EFigureStatus::NOT_PRESENT) :
		Counter(0),   // Utilisation correcte de l'initialisation directe
		bIsReady(true) // Utilisation correcte de l'initialisation directe
	{
		// Initialisation du tableau StatusArray avec 16 éléments par défaut
		StatusArray.Init(DefaultStatus, ArraySize);
	}

	// Overload the == operator
	bool operator==(const FPortalStatusData& Other) const
	{
		if (bIsReady == Other.bIsReady && Counter == Other.Counter) {
			if (StatusArray == Other.StatusArray) {
				return true;
			}
		}
		return false;
	}

	// Overload the != operator
	bool operator!=(const FPortalStatusData& Other) const
	{
		return !(*this == Other);
	}
};


class FPortalStatusChecker : public FRunnable {
public:
	/* Constructor : pass the subsystem and desired check interval(in seconds)
	*
	* @param InSubsystem Should always be the SkyPortalSubsystem
	*/
	FPortalStatusChecker(UEngineSubsystem* InSubsystem, float InCheckInterval);

	// FRunnable interface
	virtual bool Init() override;
	virtual uint32 Run() override;
	virtual void Stop() override;
	virtual void Exit() override;



private:
	// Pointer to the subsystem that contains PortalStatus()
	UEngineSubsystem* SkyPortalSubsystemRef;

	// Time interval (in seconds) for status checking
	float CheckInterval;

	// Thread control variables
	FThreadSafeBool bShouldRun;

	// Helper function to check portal status
	void CheckPortalStatus();
};

