using UnityEngine;
using VOID.Generic.Objects.Abstract;
using VOID.Generic.Objects.Unit.Events;
using VOID.Generic.Triggers;

namespace VOID.Generic.Objects.Unit.AI
{
    public class UnitHear : MonoBehaviour, IDynamicTrigger
    {
        private UnitObject _forUnit;

        public static void Create(UnitObject forUnit)
        {
            var sightGameObject = new GameObject("_hear");
            sightGameObject.transform.parent = forUnit.transform;
            sightGameObject.transform.position = forUnit.transform.position;
            sightGameObject.layer = LayerManager.TriggerIndex;

            var sightCollider = sightGameObject.AddComponent<SphereCollider>();
            sightCollider.radius = forUnit.unitData.subAttributes.hear;
            sightCollider.includeLayers = LayerManager.Dynamic;

            var unitSight = sightGameObject.AddComponent<UnitHear>();
            unitSight._forUnit = forUnit;
        }

        public void OnObjectEnter(AbstractObject obj)
        {
            if (_forUnit == obj) return;
            _forUnit.unitEvents.onHearEnter.Invoke(new SenseHearEvent { unit = _forUnit, hearObject = obj });
        }

        public void OnObjectExit(AbstractObject obj)
        {
            if (_forUnit == obj) return;
            _forUnit.unitEvents.onHearExit.Invoke(new SenseHearEvent { unit = _forUnit, hearObject = obj });
        }
    }
}