using System;
using System.Collections.Generic;

namespace MonoGame.FourEngine
{
    public class Entity : IDisposable
    {
        public virtual IList<Component> Components
            => _components ?? (_components = new List<Component>());
        private IList<Component> _components;

        private bool _disposed;

        public virtual void OnAttach(Scene scene)
        { }

        public virtual void OnDetach(Scene scene)
        { }

        public virtual void Dispose()
        {
            _disposed = true;
            if (_components != null)
            {
                foreach (var component in _components)
                {
                    if (component is IDisposable disposable)
                        disposable.Dispose();
                }
                _components = null;
            }
        }

        ~Entity()
        {
            if (!_disposed) Dispose();
        }
    }
}