using Cysharp.Threading.Tasks;
using GhostSystem;
using NUnit.Framework;
using UnityEngine;

public class EyeTrackingCollisionPoints : MonoBehaviour
{
    [SerializeField]
    private float rayDistance = 1.0f;

    [SerializeField]
    private LayerMask gazableLayers;
    [SerializeField]
    private LayerMask targetLayers;

    public GameObject hitPointPrefab;

    public GameObject hitPoint;

    public bool heatIsActive = false;

    private void FixedUpdate()
    {
        RaycastHit hit;

        Vector3 rayCastDirection = transform.TransformDirection(Vector3.forward) * rayDistance;

        if ( hitPoint != null && Physics.Raycast(transform.position, rayCastDirection, out hit, rayDistance, gazableLayers))
        {
            hitPoint.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
            if (heatIsActive)
            {
                FindVisibleTargets(hitPoint.transform.position);
            }
            
        }
    }

    public Transform SpawnHitPoint() // Wird durch LocalPlayerReferences aufgerufen, um nicht zu oft den GhostRecorder zu suchen.
    {
        hitPoint = Instantiate(hitPointPrefab);
        return hitPoint.transform;
    }

    void FindVisibleTargets(Vector3 hitPosition) // Sucht in einem Radius von 20 cm um den HitPoint nach Targets
    {
        Collider[] visibleTargets = Physics.OverlapSphere(hitPosition, 0.2f, targetLayers);
        foreach (Collider col in visibleTargets)
        {
            GameObject hitObject = col.gameObject;
            if (hitObject.GetComponent<RecieveHeat>() != null)
            {
                hitObject.GetComponent<RecieveHeat>().AddHeat(0.02f);
            }
            else
            {
                Debug.Log("Hit " + hitObject.name);
            }
        }
    }

    public void IsHeatActive(bool heatActive)
    {
        heatIsActive = heatActive;
    }
}
  