using System.Linq;
using Sirenix.OdinInspector.Editor.Validation;
using UnityEngine;
using VOID.Editor.AttributeValidator;
using VOID.Generic;
using VOID.Generic.Triggers;

[assembly: RegisterValidator(typeof(CameraTriggerValidator))]

namespace VOID.Editor.AttributeValidator
{
    public class CameraTriggerValidator : ValueValidator<ICameraTrigger>
    {
        private Component _component;
        private GameObject _gameObject;
        
        protected override void Validate(ValidationResult result)
        {
            if (Value is not Component component) return;
            _component = component;
            _gameObject = _component.gameObject;
            
            ValidateLayer(result);
            ValidateIsTrigger(result);
        }

        private void ValidateLayer(ValidationResult result)
        {
            if (LayerManager.CodeInMask(_component.gameObject.layer, LayerManager.Camera)) return;
            
            result.AddError($"Layer is not <u>{nameof(LayerManager.Camera)}</u>!");
        }

        private void ValidateIsTrigger(ValidationResult result)
        {
            var isTrigger = _gameObject.GetComponents<Collider>().All(c => c.isTrigger);
            if (isTrigger) return;
            
            result.AddError("All <u>Is Trigger</u> in colliders must be set to <u>true</u>!");
        }
    }
}