using FishNet.Object;
using FishNet.Object.Synchronizing;
using UnityEngine;

namespace IStalk.Props
{
    public abstract class PropBase : NetworkBehaviour
    {
        [SerializeField] protected Renderer _renderer;
        [SerializeField] protected Collider _collider;

        private readonly SyncVar<bool> _isActive = new(new SyncTypeSettings(0.1f));

        public bool IsActive => _isActive.Value;

        public override void OnStartClient()
        {
            base.OnStartClient();
            _isActive.OnChange += OnActiveChanged;
            UpdateVisuals(_isActive.Value);
        }

        [Server]
        public virtual void Activate()
        {
            _isActive.Value = true;
        }

        [Server]
        public virtual void Deactivate()
        {
            _isActive.Value = false;
        }

        private void OnActiveChanged(bool prev, bool next, bool asServer)
        {
            UpdateVisuals(next);
        }

        protected virtual void UpdateVisuals(bool active)
        {
            if (_renderer != null)
                _renderer.enabled = active;
            if (_collider != null)
                _collider.enabled = active;
        }
    }
}
