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 UserImageRepository : IUserImageRepository
    {
        #region Public Constructors

        public UserImageRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfImage;
        }

        #endregion Public Constructors

        #region Public Methods

        public int Add(UserImage newImage)
        {
            int result;

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

            return result;
        }

        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.Id == user.Id)
                       .ToList();
        }

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

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

        #endregion Public Methods

        #region Private Fields

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

        #endregion Private Fields
    }
}