using UnityEngine;
using Script.Gameplay.Interface;

namespace Script.Gameplay.Facility
{
    [RequireComponent(typeof(Rigidbody))]
    public class RigidbodyEditableController : MonoBehaviour, IEditableComponent
    {
        [SerializeField] private bool isActive = true;

        public bool IsActive
        {
            get => isActive;
            set
            {
                isActive = value;
                //具体被编辑的逻辑
                _rigidbody.isKinematic = !isActive;
            }
        }

        public string ComponentName { get; set; } = "Rigidbody";
        public LockLevel LockLevel { get; } = LockLevel.Red;
        private Rigidbody _rigidbody;
        
        private void Awake()
        {
            _rigidbody = GetComponent<Rigidbody>();
            //应用序列化的初始状态
            _rigidbody.isKinematic = !isActive;
        }
        
#if UNITY_EDITOR
        private void OnValidate()
        {
            //在编辑器中即时生效
            _rigidbody = _rigidbody == null ? GetComponent<Rigidbody>() : _rigidbody;
            if (_rigidbody != null)
                _rigidbody.isKinematic = !isActive;
        }
#endif
    }
}