using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Interactor : MonoBehaviour
{
	List<GameObject> highlightCandidates = new List<GameObject>();

	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.CompareTag("roomObject"))
		{
			highlightCandidates.Add(other.gameObject);
			Debug.Log("roomObject detected");
		}
	}

	public Interactee GetCandidate()
    {
		Interactee interactee = null;
		int count = highlightCandidates.Count;
		if (count > 0)
			interactee = highlightCandidates[count - 1]?.GetComponent<Interactee>();
		return interactee;
	}

	void OnTriggerExit(Collider other)
	{
		highlightCandidates.Remove(other.gameObject);
	}

	public void OnInteract()
	{
		Interactee interactee = GetCandidate();
		interactee?.OnInteract();
	}
}
