using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;
using System.Collections.Generic;
using System.Linq;

namespace skydiveLogs_api.Infrastructure
{
    public class AircraftRepository : IAircraftRepository
    {
        #region Public Constructors

        public AircraftRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfAircraft;
        }

        #endregion Public Constructors

        #region Public Methods

        public int Add(Aircraft newAircraft)
        {
            int result;

            try
            {
                var tmp = _col.Insert(newAircraft);
                result = tmp.AsInt32;
            }
            catch
            {
                result = 0;
            }

            return result;
        }

        public IEnumerable<Aircraft> GetAll()
        {
            return _col.FindAll().ToList();
        }

        public Aircraft GetById(int id)
        {
            return _col.FindById(new BsonValue(id));
        }

        public bool Update(Aircraft aircraft)
        {
            return _col.Update(aircraft);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ILiteCollection<Aircraft> _col;
        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}