#include <PicoBoundaryAdapter.h>
#include "BoundaryAdapterFactory.h"
#include "PXR_HMDFunctionLibrary.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 APicoBoundaryAdapter::GetBoundaryName() const { return "Pico 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>

// "UPxr_GetGeometry"

TArray<FVector> APicoBoundaryAdapter::GetPoints() const {
	//if (!UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()) {
	//	LOG("Warning: Cannot provide boundaries while not in VR/HMD enabled");
	//	return TArray<FVector>();
	//}
	
	// https://developer.picoxr.com/reference/unreal/client-api/PXR_Boundary/#PXR_GetBoundaryGeometry

	TArray<FVector> points = UPICOXRHMDFunctionLibrary::PXR_GetBoundaryGeometry(EPICOXRBoundaryType::Outer);
	//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>
APicoBoundaryAdapter::APicoBoundaryAdapter()
{
	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 APicoBoundaryAdapter::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 APicoBoundaryAdapter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

