using System.Collections.Generic;

using LiteDB;

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


namespace skydiveLogs_api.Infrastructure
{
    public class UserImageRepository : IUserImageRepository
    {
        public UserImageRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfImage;
        }

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

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

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

        public bool Update(UserImage image)
        {
            return _col.Update(image);
        }

        public bool Add(UserImage newImage)
        {
            var result = true;

            try
            {
                _col.Insert(newImage);
            }
            catch
            {
                result = false;
            }

            return result;
        }

        private readonly IDataProvider _dataProvider;

        private readonly ILiteCollection<UserImage> _col;
    }
}
