using HighlightPlus;
using Oculus.Interaction;
using Oculus.Interaction.HandGrab;
using UnityEngine;
using UnityEngine.Events;

public class SampleTriggerHandler : MonoBehaviour
{
    //public string targetTag = "Player";
    public Transform spawnPosition;
    public UnityEvent OnObjectEntered;
    public bool turnOffHighlight =false;
    public bool disableTrigger = true;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        if (spawnPosition == null)
            spawnPosition = transform;
    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out TriggerObject Tobj))
        {
            if (Tobj && Tobj.enabled)
            {
                OnTagObjectentered(other);
                OnObjectEntered?.Invoke();
            }
            // Debug.LogError("Called");
        }
    }


    public void OnTagObjectentered(Collider other)
    {
        other.enabled = false;
        other.GetComponent<Grabbable>().enabled = false;
        other.GetComponentInChildren<HandGrabInteractable>().enabled = false;
        other.transform.SetParent(null);
        other.transform.SetPositionAndRotation(spawnPosition.position, spawnPosition.rotation);
        if(turnOffHighlight)
        {
            HighlightManager.DisableHighlight(other.GetComponentInChildren<HighlightEffect>().gameObject);
        }
        gameObject.SetActive(!disableTrigger);
    }

}
