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

namespace skydiveLogs_api.Infrastructure
{
    public class GearRepository : IGearRepository
    {
        #region Public Constructors

        public GearRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfGear;
        }

        #endregion Public Constructors

        #region Public Methods

        public int Add(Gear newGear)
        {
            int result;

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

            return result;
        }

        public IEnumerable<Gear> GetAll()
        {
            throw new System.NotImplementedException();
        }

        public IEnumerable<Gear> GetAll(User user)
        {
            return _col.Include(x => x.User)
                       .Query()
                       .Where(j => j.User.Id == user.Id)
                       .ToList();
        }

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

        public bool Update(Gear updatedGear)
        {
            return _col.Update(updatedGear);
        }

        #endregion Public Methods

        #region Private Fields

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

        #endregion Private Fields
    }
}