using System;
using System.Collections.Generic;
using Intrepid.Core.Tags;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Core.Entities
{
    public abstract class EntityTrigger : Identification
    {
        [GroupInjections, SerializeField] private Collider mainCollider;
        [Title("Entity Sensors we are looking for")]
        [GroupConfigurations, SerializeField, PropertySpace, PropertyOrder(10)] private List<EntityTag> entitySensors;
        [Title("The behaviour which at least one should be accepted")]
        [GroupConfigurations, SerializeField, PropertySpace, PropertyOrder(11)] private List<BehaviourTag> behaviourTags;

        protected Collider MainCollider => mainCollider;
        protected bool IsEnabled { get; private set; }
        
        protected virtual void Awake()
        {
            MainCollider.isTrigger = true;
            IsEnabled = true;
        }

        protected void OnTriggerEnter(Collider other)
        {
            if (!other.gameObject.TryGetComponent<EntitySensor>(out var entitySensor)) return;
            if (entitySensor.ShouldNotBeTriggered(entitySensors, behaviourTags)) return;
            WhenRaiseEnterEvent(other, entitySensor, behaviourTags);
        }

        protected void OnTriggerExit(Collider other)
        {
            if (!other.gameObject.TryGetComponent<EntitySensor>(out var entitySensor)) return;
            if (entitySensor.ShouldNotBeTriggered(entitySensors, behaviourTags)) return;
            WhenRaiseExitEvent(other, entitySensor);
        }

        public virtual void Enable()
        {
            IsEnabled = true;
            MainCollider.enabled = true;
        }
        
        public virtual void Disable()
        {
            IsEnabled = false;
            MainCollider.enabled = false;
        }
        
        protected abstract void WhenRaiseEnterEvent(Collider other, EntitySensor entitySensor, List<BehaviourTag> behaviourTags);
        protected abstract void WhenRaiseExitEvent(Collider other, EntitySensor entitySensor);
    }
}