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

using LiteDB;

using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;


namespace skydiveLogs_api.Infrastructure
{
    public class JumpTypeRepository : IJumpTypeRepository
    {
        public JumpTypeRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfJumpType;
        }

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

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

        public bool Update(JumpType updatedJumpType)
        {
            return _col.Update(updatedJumpType);
        }

        public int Add(JumpType newJumpType)
        {
            int result;

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

            return result;
        }

        private readonly IDataProvider _dataProvider;

        private readonly ILiteCollection<JumpType> _col;
    }
}
