using System;
using System.Collections;
using System.Collections.Generic;

namespace MonoGame.FourEngine.Collections
{
    public class SimpleObservableCollection<T> : ICollection<T>
    {
        public int Count => _impl.Count;

        public bool IsReadOnly => false;

        private List<T> _impl = new List<T>();
        private Action<T> _onAdd;
        private Action<T> _onRemove;

        public SimpleObservableCollection(Action<T> onAdd, Action<T> onRemove)
        {
            _onAdd = onAdd;
            _onRemove = onRemove;
        }

        public void Add(T item)
        {
            _onAdd?.Invoke(item);
            _impl.Add(item);
        }

        public bool Remove(T item)
        {
            _onRemove?.Invoke(item);
            return _impl.Remove(item);
        }

        public void Clear()
        {
            foreach (var item in _impl)
                _onRemove?.Invoke(item);
            _impl.Clear();
        }

        public bool Contains(T item)
            => _impl.Contains(item);

        public void CopyTo(T[] array, int arrayIndex)
            => _impl.CopyTo(array, arrayIndex);

        public IEnumerator<T> GetEnumerator()
            => _impl.GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator()
            => _impl.GetEnumerator();
    }
}
