using ProjectFighterJet.CollectionGenericObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectFighterJet.CollectionGenericObjects;

/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
    /// <summary>
    /// Список объектов, которые храним
    /// </summary>
    private readonly List<T?> _collection;
    /// <summary>
    /// Максимально допустимое число объектов в списке
    /// </summary>
    private int _maxCount;
    public int Count => _collection.Count;
    public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
    /// <summary>
    /// Конструктор
    /// </summary>
    public ListGenericObjects()
    {
        _collection = new();
    }
    public T? Get(int position)
    {
        if (position >= 0 && position < Count)
        {
            return _collection[position];
        }
        return null;
    }
    public int Insert(T obj)
    {
        if (Count <= _maxCount)
        {
            _collection.Add(obj);
            return Count;
        }
        return -1;
    }
    public int Insert(T obj, int position)
    {
        if (Count < _maxCount && position >= 0 && position < _maxCount)
        {
            _collection.Insert(position, obj);
            return position;
        }
        return -1;
    }
    public T Remove(int position)
    {
        T temp = _collection[position];
        if (position >= 0 && position < _maxCount)
        {
            _collection.RemoveAt(position);
            return temp;
        }
        return null;
    }

}