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

namespace InABox.Core
{
    public class CoreTableAdapter<T> : IEnumerable<T> where T : BaseObject, new()
    {
        private List<T>? _objects;

        private readonly CoreTable _table;

        public CoreTableAdapter(CoreTable table)
        {
            _table = table;
        }

        private List<T> Objects
        {
            get => _objects ??= _table.Rows.Select(row => row.ToObject<T>()).ToList();
        }

        public T this[int index] => Objects[index];


        public IEnumerator<T> GetEnumerator()
        {
            return GetObjects();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetObjects();
        }

        private IEnumerator<T> GetObjects()
        {
            return Objects.GetEnumerator();
        }
    }
}