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 FavoriteDropZoneRepository : IFavoriteDropZoneRepository
    {
        #region Public Constructors

        public FavoriteDropZoneRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfFavoriteDropZone;
        }

        #endregion Public Constructors

        #region Public Methods

        public int Add(FavoriteDropZone favoriteToAdd)
        {
            int result;

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

            return result;
        }

        public int Delete(int dropZoneId, int userId)
        {
            return _col.DeleteMany(d => d.DropZone.Id == dropZoneId && d.User.Id == userId);
        }

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

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

        public FavoriteDropZone GetById(int id)
        {
            throw new System.NotImplementedException();
        }

        public bool Update(FavoriteDropZone updated)
        {
            throw new System.NotImplementedException();
        }

        #endregion Public Methods

        #region Private Fields

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

        #endregion Private Fields
    }
}