<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/webxr_util.js"></script>
<script src="../resources/webxr_math_utils.js"></script>
<script src="../resources/webxr_test_asserts.js"></script>
<script src="../resources/webxr_test_constants.js"></script>
<script src="../resources/webxr_test_constants_fake_world.js"></script>

<script>

// 1m above world origin.
const VIEWER_ORIGIN_TRANSFORM = {
  position: [0, 1, 0],
  orientation: [0, 0, 0, 1],
};

// 0.25m above world origin.
const FLOOR_ORIGIN_TRANSFORM = {
  position: [0, 0.25, 0],
  orientation: [0, 0, 0, 1],
};

const fakeDeviceInitParams = {
  supportedModes: ["immersive-ar"],
  views: VALID_VIEWS,
  floorOrigin: FLOOR_ORIGIN_TRANSFORM,    // aka mojo_from_floor
  viewerOrigin: VIEWER_ORIGIN_TRANSFORM,  // aka mojo_from_viewer
  supportedFeatures: ALL_FEATURES,
  world: createFakeWorld(5.0, 2.0, 5.0),  // webxr_test_constants_fake_world.js has detailed description of the fake world
};

// Generates a test function given the parameters for the hit test.
// |ray| - ray that will be used to subscribe to hit test.
// |expectedPoses| - array of expected pose objects. The poses are expected to be expressed in local space.
//                   Null entries in the array mean that the given entry will not be validated.
// |refSpaceName| - XRReferenceSpaceType - either 'local', 'local-floor' or 'viewer'.
let testFunctionGenerator = function(ray, expectedPoses, refSpaceName) {
  const testFunction = function(session, fakeDeviceController, t) {
    return Promise.all([
      session.requestReferenceSpace('local'),
      session.requestReferenceSpace('viewer'),
      session.requestReferenceSpace('local-floor'),
    ]).then(([localRefSpace, viewerRefSpace, localFloorRefSpace]) => {

      const refSpaceNameToSpace = {
        'local' : localRefSpace,
        'viewer' : viewerRefSpace,
        'local-floor' : localFloorRefSpace
      };

      const hitTestOptionsInit = {
        space: refSpaceNameToSpace[refSpaceName],
        offsetRay: ray,
      };

      return session.requestHitTestSource(hitTestOptionsInit).then(
        (hitTestSource) => new Promise((resolve, reject) => {

        const requestAnimationFrameCallback = function(time, frame) {
          const hitTestResults = frame.getHitTestResults(hitTestSource);

          t.step(() => {
            assert_equals(hitTestResults.length, expectedPoses.length, "Results length should match expected results length");
            for(const [index, expectedPose] of expectedPoses.entries()) {
              const pose = hitTestResults[index].getPose(localRefSpace);
              assert_true(pose != null, "Each hit test result should have a pose in local space");
              if(expectedPose != null) {
                assert_transform_approx_equals(pose.transform, expectedPose);
              }
            }
          });

          resolve();
        };

        t.step(() => {
          assert_true(hitTestSource != null, "Hit test source should not be null");
        });

        session.requestAnimationFrame(requestAnimationFrameCallback);
      }));
    });
  };

  return testFunction;
};

// Generates a test function that will use local space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const localBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'local');
};

// Generates a test function that will use viewer space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const viewerBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'viewer');
};

// Generates a test function that will use local-floor space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const localFloorBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'local-floor');
};

// All test cases require local-floor and hit-test.
const sessionInit = { 'requiredFeatures': ['local-floor', 'hit-test'] };

// Pose of the first expected hit test result - straight ahead of the viewer, viewer-facing.
const pose_1 = {
  position: {x: 0.0, y: 1.0, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // Hit test API will set Y axis to the surface normal at the intersection point,
    // Z axis towards the ray origin and X axis to cross product of Y axis & Z axis.
    // If the surface normal and Z axis would be parallel, the hit test API
    // will attempt to use `up` vector ([0, 1, 0]) as the Z axis, and if it so happens that Z axis
    // and the surface normal would still be parallel, it will use the `right` vector ([1, 0, 0]) as the Z axis.
    // In this particular case, `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test(
  "Ensures subscription to hit test works with viewer space - straight ahead - plane",
  viewerBasedTestFunctionGenerator(new XRRay(), [pose_1]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

xr_session_promise_test("Ensures subscription to hit test works with viewer space - straight up - no results",
  viewerBasedTestFunctionGenerator(new XRRay({}, {x: 0.0, y: 1.0, z : 0.0}), []),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

const pose_2 = {
  position: {x: 0.0, y: 0.0, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // See comment for pose_1.orientation for details.
    // In this case, the hit test pose will have Y and Z axis towards the ray origin so it won't be used,
    // but `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test("Ensures subscription to hit test works with local space",
  localBasedTestFunctionGenerator(new XRRay(), [pose_2]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

const pose_3 = {
  position: {x: 0.0, y: 0.25, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // See comment for pose_1.orientation for details.
    // In this case, the hit test pose will have Y and Z axis towards the ray origin so it won't be used,
    // but `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test("Ensures subscription to hit test works with local-floor space",
  localFloorBasedTestFunctionGenerator(new XRRay(), [pose_3]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

</script>
