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

namespace Intrepid.Core.Entities
{
    [RequireComponent(typeof(SphereCollider))]
    public class EntitySensor : AbstractTagBehaviour<BehaviourTag>
    {
        [Title("I am a...")]
        [GroupConfigurations, SerializeField] private EntityTag entityTag;
        
        public Entity Entity { get; set; }
        public SphereCollider Collider { get; private set; }
        public EntityTag EntityTag => entityTag;
        public bool IsEnabled { get; private set; }

        private void Awake()
        {
            Entity = GetComponentInParent<Entity>();
            Collider = GetComponent<SphereCollider>();
            Collider.isTrigger = true;
            IsEnabled = true;
        }

        public bool ShouldBeTriggered(IEnumerable<EntityTag> entitySensors, IEnumerable<BehaviourTag> behaviourTags)
        {
            return entitySensors.Contains(EntityTag) && HasAnyTags(behaviourTags);
        }

        public bool ShouldNotBeTriggered(IEnumerable<EntityTag> entitySensors, IEnumerable<BehaviourTag> behaviourTags)
        {
            return !ShouldBeTriggered(entitySensors, behaviourTags);
        }

        public void Enable()
        {
            IsEnabled = true;
            Collider.enabled = true;
        }
        
        public void Disable()
        {
            IsEnabled = false;
            Collider.enabled = false;
        }

        public void ClearFilteringTags()
        {
            FilteringTags.Clear();
        }

        public void ActivateBehavioursOnly(IEnumerable<BehaviourTag> tags)
        {
            FilteringTags.RemoveAll(tags.Contains);
        }
        
        public void DeactivateBehavioursOnly(IEnumerable<BehaviourTag> tags)
        {
            FilteringTags.AddRange(tags);
        }
    }
}