#include <OpenXrBoundaryAdapter.h>
#include "BoundaryAdapterFactory.h"

#include "HeadMountedDisplay.h"
#include <HeadMountedDisplayFunctionLibrary.h>

#include "unreal.grpc.h"


/// <summary>
/// Gets the name of the native Quest Guardian boundary.
/// </summary>
/// <returns>The name of the native Quest Guardian boundary.</returns>
FString AOpenXrBoundaryAdapter::GetBoundaryName() const { return "OpenXR Guardian"; }

/// <summary>
/// Gets the points of the outer boundary of the Wave Guardian system.
/// </summary>
/// <returns>
/// An array of FVector points representing the outer boundary of the Wave Guardian system.
/// </returns>
TArray<FVector> AOpenXrBoundaryAdapter::GetPoints() const {
	if (!UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()) {
		LOG("Warning: Cannot provide boundaries while not in VR/HMD enabled");
		return TArray<FVector>();
	}

	FVector2D cornerPpoint = UHeadMountedDisplayFunctionLibrary::GetPlayAreaBounds(EHMDTrackingOrigin::Stage);
	TArray<FVector> points;
	if (!cornerPpoint.IsNearlyZero())
		points = { {cornerPpoint.X, 0, cornerPpoint.Y}, { -cornerPpoint.X, 0 , cornerPpoint.Y }, { -cornerPpoint.X, 0 , -cornerPpoint.Y }, { cornerPpoint.X, 0 , -cornerPpoint.Y } };

	return points;
}

/// <summary>
/// Constructor for AOculusQuestBoundaryAdapter class. Sets PrimaryActorTick to call Tick() every frame. 
/// </summary>
AOpenXrBoundaryAdapter::AOpenXrBoundaryAdapter()
{
	USceneComponent* SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
	SetRootComponent(SceneComponent); // Set this component as the root

	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

/// <summary>
/// Called when the game starts or when spawned, registers the Wave boundary adapter with the BoundaryAdapterFactory.
/// </summary>
void AOpenXrBoundaryAdapter::BeginPlay()
{
	Super::BeginPlay();
	//BoundaryAdapterFactory::Register(AR51::PlatformType::HtcVive, this);
	//BoundaryAdapterFactory::Register(AR51::PlatformType::OpenXrTethered, this);
	BoundaryAdapterFactory::RegisterAllPlatforms(this);


}

/// <summary>
/// Called every frame to update the Wave boundary adapter.
/// </summary>
/// <param name="DeltaTime">The amount of time that has passed since the last frame.</param>
void AOpenXrBoundaryAdapter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

