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

namespace BingoBrain
{
    public abstract class BingoBrainConfigModel<T> where T : BingoBrainConfigData
    {
        protected internal List<T> dataList;

        public BingoBrainConfigModel()
        {
        }

        public void SetDataList(List<T> data)
        {
            dataList = data;
        }

        public BingoBrainConfigModel(List<T> data)
        {
            dataList = data;
        }

        public int GetCount()
        {
            return dataList?.Count ?? 0;
        }

        public T GetFirst()
        {
            return dataList?.FirstOrDefault();
        }

        public T GetLast()
        {
            return dataList?.LastOrDefault();
        }

        public T GetData(int id)
        {
            foreach (var item in dataList)
            {
                if (item.id == id)    return item;
            }
            return dataList[0];
        }

        public T GetData(string key)
        {
            return dataList?.FirstOrDefault(t => t.key == key);
        }

        public T GetDataByIndex(int index)
        {
            return dataList?.ElementAtOrDefault(index);
        }

        public List<T> GetDataList()
        {
            return dataList;
        }
    }
}