using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit;

public class GravityHandler : MonoBehaviour
{
    public InputActionReference teleportAction;
    public XRInteractorLineVisual TeleportRayLine;
    public ActionBasedContinuousMoveProvider defaultGravity;
    public bool isInSpace = false;

    private void Awake()
    {
        teleportAction.action.Enable();
        teleportAction.action.performed += OnTeleport;
    }

    private void Update()
    {
        if (isInSpace)
        {
            defaultGravity.useGravity = false; // Disable default gravity
        }
        else
        {
            defaultGravity.useGravity = true; // Enable default gravity
            Physics.gravity = Vector3.down * 9.81f; // Reset to normal gravity
        }
    }

    private void OnTeleport(InputAction.CallbackContext context)
    {
        if (isInSpace)
        {
            Quaternion newRotation = TeleportRayLine.reticle.transform.rotation;
            Debug.Log("Teleport detected! Adjusting gravity... New rotation is: " + newRotation.eulerAngles);
            //AdjustGravity(newRotation);
        }
    }

    public void AdjustGravity(Quaternion rotation)
    {
        Vector3 newGravityDirection = rotation * Vector3.down; // Rotate gravity based on teleport direction
        Physics.gravity = newGravityDirection.normalized * Physics.gravity.magnitude;

        Debug.Log("New Gravity: " + Physics.gravity);
    }

    private void OnDestroy()
    {
        teleportAction.action.Disable();
        teleportAction.action.performed -= OnTeleport;
    }
}