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 UnitSight : MonoBehaviour, IDynamicTrigger
    {
        private UnitObject _forUnit;

        public static void Create(UnitObject forUnit)
        {
            var sightGameObject = new GameObject("_sight");
            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.sight;
            sightCollider.includeLayers = LayerManager.Dynamic;

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

        public void OnObjectEnter(AbstractObject obj)
        {
            if (_forUnit == obj) return;
            _forUnit.unitEvents.onSightEnter.Invoke(new SenseSightEvent { unit = _forUnit, sightObject = obj });
        }

        public void OnObjectExit(AbstractObject obj)
        {
            if (_forUnit == obj) return;
            _forUnit.unitEvents.onSightExit.Invoke(new SenseSightEvent { unit = _forUnit, sightObject = obj });
        }
    }
}