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

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

        protected Collider MainCollider => mainCollider;
        protected bool IsEnabled { get; private set; }

        private void Awake()
        {
            Enable();
        }
        
        private void OnCollisionEnter(Collision collision)
        {
            if (!IsEnabled) return;
            if (!collision.gameObject.TryGetComponent<Entity>(out var entity)) return;
            if (entity.ShouldNotBeTriggered(entities, physicsTags)) return;
            WhenRaiseEnterEvent(collision, entity, physicsTags);
        }

        protected void OnCollisionExit(Collision collision)
        {
            if (!IsEnabled) return;
            if (!collision.gameObject.TryGetComponent<Entity>(out var entity)) return;
            if (entity.ShouldNotBeTriggered(entities, physicsTags)) return;
            WhenRaiseExitEvent(collision, entity);
        }

        public virtual void Enable()
        {
            IsEnabled = true;
            MainCollider.enabled = true;
        }
        
        public virtual void Disable()
        {
            IsEnabled = false;
            MainCollider.enabled = false;
        }
        
        protected abstract void WhenRaiseEnterEvent(Collision collision, Entity entity, List<PhysicsTag> physicsTags);
        protected abstract void WhenRaiseExitEvent(Collision collision, Entity entity);
    }
}