using System.Collections.Generic;
using UnityEngine;

public class fingertip_interaction_manager_tags : MonoBehaviour
{
    private float internal_displacement;
    private HashSet<float> uniqueDistances = new HashSet<float>(); // Store unique distances

    public GameObject cube;
    private float distanceAbovePlane;  // Variable to store the distance above the plane
    [SerializeField]
    private Transform planeTransform;

    private float distanceToPlane;  // Variable to store the distance to the plane

    [SerializeField]
    private Transform sphere;
    public bool isColliding = false;  // Flag to track collision status

    public double getDisplacement()
    {
        return distanceToPlane;
    }

    private void CalculateDistanceAbovePlane(Collider other)
    {
        float planeY = planeTransform.position.y; // The y-position of the plane in world coordinates
        float obstacleTopY = other.bounds.max.y;  // The top y-position of the obstacle in world coordinates

        distanceAbovePlane = obstacleTopY - planeY; // Calculate distance from the plane to the top of the obstacle

        //Debug.Log("Distance above plane: " + distanceAbovePlane);
         //Add to the set if it's a new unique value
        // if (uniqueDistances.Add(distanceAbovePlane))
        // {
        //     //Debug.Log("New unique distance added: " + distanceAbovePlane);
        //     Debug.Log("Max distance: " + uniqueDistances.Min());
        // }
    }

    private void OnTriggerEnter(Collider other)
    {
        isColliding = true;
        CalculateDistanceAbovePlane(other);  // Calculate the distance above the plane for this object
    }

    // Calculate the displacement between the two spheres when they are colliding
    private void OnTriggerStay(Collider other)
    {
        isColliding = true;
        internal_displacement = Vector3.Magnitude(this.transform.position - sphere.position);

        distanceToPlane =  distanceAbovePlane - internal_displacement;  // Calculate the difference between the displacement and the distance above the plane
        // Debug.Log("Difference: " + distanceToPlane);
        // Debug.Log("Displacement: " + internal_displacement);
    }

    private void OnTriggerExit(Collider other)
    {
        isColliding = false;  // Reset when no longer colliding
        internal_displacement = 0f;
        distanceToPlane = 0f;
    }
}
