using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Rendering;

namespace VOID.Generic.Triggers
{
    [RequireComponent(typeof(Collider))]
    public class HideCeilingTrigger : MonoBehaviour, ICameraTrigger
    {
        public List<GameObject> gameObjectsToHide => _gameObjectsToHide;
        
        [SerializeField, RequiredListLength(MinLength = 1)] private List<GameObject> _gameObjectsToHide = new();
        
        // Cache
        private List<Renderer> _renderers = new();

        public void Awake()
        {
            _renderers = _gameObjectsToHide
                .Where(go => _gameObjectsToHide.All(other => go == other || !go.transform.IsChildOf(other.transform)))
                .SelectMany(go => go.GetComponentsInChildren<Renderer>())
                .ToList();
        }

        public void OnCameraEnter()
        {
            _renderers.ForEach(renderer => renderer.shadowCastingMode = ShadowCastingMode.ShadowsOnly);
        }

        public void OnCameraExit()
        {
            _renderers.ForEach(renderer => renderer.shadowCastingMode = ShadowCastingMode.On);
        }
    }
}