using NUnit.Framework;
using UnityEngine;

public class HandCalibrationMathTests
{
    private static readonly Quaternion[] Poses =
    {
        Quaternion.identity,
        Quaternion.Euler(0f, 90f, 0f),
        Quaternion.Euler(10f, 200f, 0f),
        Quaternion.Euler(-20f, 35f, 40f),
        Quaternion.Euler(15f, -70f, 120f),
    };

    [Test]
    public void SolveHandOffset_RoundTripsThroughHandPoint()
    {
        Vector3 hand = new Vector3(0.4f, 1.1f, -0.3f);
        Vector3 target = new Vector3(0.45f, 1.02f, -0.22f);
        foreach (Quaternion rotation in Poses)
        {
            Vector3 offset;
            Assert.IsTrue(PhysicalRaiseSelectionMath.SolveHandOffset(hand, rotation, target, out offset), rotation.ToString());
            Vector3 point = PhysicalRaiseSelectionMath.HandPoint(hand, rotation, offset);
            Assert.AreEqual(0f, (point - target).magnitude, 0.0001f, rotation.ToString());
        }
    }

    [Test]
    public void SolveHandOffset_IdentityPoseIsTheWorldDelta()
    {
        Vector3 offset;
        Assert.IsTrue(PhysicalRaiseSelectionMath.SolveHandOffset(
            new Vector3(1f, 2f, 3f), Quaternion.identity, new Vector3(1.01f, 2.09f, 3.05f), out offset));
        Assert.AreEqual(0.01f, offset.x, 0.0001f);
        Assert.AreEqual(0.09f, offset.y, 0.0001f);
        Assert.AreEqual(0.05f, offset.z, 0.0001f);
    }

    [Test]
    public void SolveHandOffset_RejectsNearVerticalArm()
    {
        Vector3 offset;
        Assert.IsFalse(PhysicalRaiseSelectionMath.SolveHandOffset(
            Vector3.zero, Quaternion.Euler(-80f, 0f, 0f), Vector3.up, out offset));
        Assert.IsFalse(PhysicalRaiseSelectionMath.SolveHandOffset(
            Vector3.zero, Quaternion.Euler(90f, 0f, 0f), Vector3.up, out offset));
    }

    [Test]
    public void HandPoint_IsUnchangedForTheDefaultOffset()
    {
        Vector3 offset = new Vector3(0f, 0.09f, 0.05f);
        Assert.AreEqual(new Vector3(1f, 2.09f, 3.05f),
            PhysicalRaiseSelectionMath.HandPoint(new Vector3(1f, 2f, 3f), Quaternion.identity, offset));
    }

    [Test]
    public void ScaleFactor_IsRelativeToTheReferenceEyeHeight()
    {
        Assert.AreEqual(1f, PhysicalRaiseSelectionMath.ScaleFactor(0.8f), 0.0001f);
        Assert.AreEqual(2f, PhysicalRaiseSelectionMath.ScaleFactor(1.6f), 0.0001f);
        Assert.AreEqual(0.5f, PhysicalRaiseSelectionMath.ScaleFactor(0.4f), 0.0001f);
    }

    [Test]
    public void ScaleFactor_ClampsAndFallsBackOnBadInput()
    {
        Assert.AreEqual(PhysicalRaiseSelectionMath.MaxScale, PhysicalRaiseSelectionMath.ScaleFactor(100f), 0.0001f);
        Assert.AreEqual(PhysicalRaiseSelectionMath.MinScale, PhysicalRaiseSelectionMath.ScaleFactor(0.001f), 0.0001f);
        Assert.AreEqual(1f, PhysicalRaiseSelectionMath.ScaleFactor(0f));
        Assert.AreEqual(1f, PhysicalRaiseSelectionMath.ScaleFactor(-1f));
        Assert.AreEqual(1f, PhysicalRaiseSelectionMath.ScaleFactor(float.NaN));
    }

    [Test]
    public void CalibrateAtLargeScale_AppliesHalvedAtReferenceScale()
    {
        float calibrationScale = PhysicalRaiseSelectionMath.ScaleFactor(1.6f);
        Vector3 measured = new Vector3(0.04f, 0.18f, 0.10f);
        Vector3 stored = measured / calibrationScale;
        Vector3 applied = stored * PhysicalRaiseSelectionMath.ScaleFactor(0.8f);
        Assert.AreEqual(0f, (applied - measured * 0.5f).magnitude, 0.0001f);
    }

    [Test]
    public void IsPlausibleOffset_RejectsHugeAndNonFiniteValues()
    {
        Assert.IsTrue(PhysicalRaiseSelectionMath.IsPlausibleOffset(new Vector3(0f, 0.09f, 0.05f)));
        Assert.IsFalse(PhysicalRaiseSelectionMath.IsPlausibleOffset(new Vector3(0.5f, 0f, 0f)));
        Assert.IsFalse(PhysicalRaiseSelectionMath.IsPlausibleOffset(new Vector3(0f, float.NaN, 0f)));
        Assert.IsFalse(PhysicalRaiseSelectionMath.IsPlausibleOffset(new Vector3(0f, 0f, float.PositiveInfinity)));
    }

    [Test]
    public void RightHandOffsetIsMirroredAcrossTheArm()
    {
        Vector3 offset = new Vector3(0.04f, 0.09f, 0.05f);
        Quaternion rotation = Quaternion.LookRotation(new Vector3(0.3f, 0f, 1f));
        Vector3 hand = new Vector3(1f, 1.2f, 2f);

        Vector3 left = PhysicalRaiseSelectionMath.HandPoint(hand, rotation, offset);
        Vector3 right = PhysicalRaiseSelectionMath.HandPoint(
            hand, rotation, PhysicalRaiseSelectionMath.MirrorOffsetForHand(offset, true));

        Quaternion frame;
        Assert.IsTrue(PhysicalRaiseSelectionMath.TryArmFrame(rotation, out frame));
        Vector3 leftLocal = Quaternion.Inverse(frame) * (left - hand);
        Vector3 rightLocal = Quaternion.Inverse(frame) * (right - hand);
        Assert.AreEqual(-leftLocal.x, rightLocal.x, 1e-5f);
        Assert.AreEqual(leftLocal.y, rightLocal.y, 1e-5f);
        Assert.AreEqual(leftLocal.z, rightLocal.z, 1e-5f);
    }

    [Test]
    public void RightHandCalibrationRoundTripsThroughTheSharedLeftSpaceValue()
    {
        Quaternion rotation = Quaternion.LookRotation(new Vector3(0.4f, 0.1f, 1f));
        Vector3 hand = new Vector3(0.2f, 1.1f, -0.3f);
        Vector3 target = new Vector3(0.25f, 1.0f, 0.1f);

        Vector3 solved;
        Assert.IsTrue(PhysicalRaiseSelectionMath.SolveHandOffset(hand, rotation, target, out solved));
        Vector3 stored = PhysicalRaiseSelectionMath.MirrorOffsetForHand(solved, true);
        Vector3 applied = PhysicalRaiseSelectionMath.MirrorOffsetForHand(stored, true);

        Assert.AreEqual(0f, (PhysicalRaiseSelectionMath.HandPoint(hand, rotation, applied) - target).magnitude, 1e-4f);
    }

    [Test]
    public void SharedValueChangesTheWayTheHeadsetShowsItWhenBothArmsMove()
    {
        // Both arms point along +Z (VR forward axes follow the arm). Target stays put while the hands move.
        Quaternion rotation = Quaternion.identity;
        Vector3 target = new Vector3(0f, 1f, 1f);
        Vector3 hand = new Vector3(0f, 1.1f, 0.8f);

        Vector3 baseLeft = Stored(hand, rotation, target, false);
        Vector3 baseRight = Stored(hand, rotation, target, true);

        Vector3 forwardLeft = Stored(hand + Vector3.forward * 0.1f, rotation, target, false) - baseLeft;
        Vector3 forwardRight = Stored(hand + Vector3.forward * 0.1f, rotation, target, true) - baseRight;
        Assert.AreEqual(forwardLeft.z, forwardRight.z, 1e-5f, "forward moves Z the same way for both hands");

        Vector3 leftwardLeft = Stored(hand + Vector3.left * 0.1f, rotation, target, false) - baseLeft;
        Vector3 leftwardRight = Stored(hand + Vector3.left * 0.1f, rotation, target, true) - baseRight;
        Assert.AreEqual(-leftwardLeft.x, leftwardRight.x, 1e-5f, "the same sideways move mirrors X between hands");
        Assert.AreEqual(leftwardLeft.y, leftwardRight.y, 1e-5f);
    }

    private static Vector3 Stored(Vector3 hand, Quaternion rotation, Vector3 target, bool rightHand)
    {
        Vector3 solved;
        Assert.IsTrue(PhysicalRaiseSelectionMath.SolveHandOffset(hand, rotation, target, out solved));
        return PhysicalRaiseSelectionMath.MirrorOffsetForHand(solved, rightHand);
    }
}
