using Nop.Core.Data;
using Nop.Core.Domain.BlogsB2C;
using Nop.Services.Events;
using System;
using System.Linq;

namespace Nop.Services.Blogs
{
    public partial class BlogsB2CService : IBlogsB2CService
    {
        private readonly IEventPublisher _eventPublisher;
        private readonly IRepository<BlogPostPictureB2C> _blogPostPictureB2CRepository;

        public BlogsB2CService(IEventPublisher eventPublisher,
            IRepository<BlogPostPictureB2C> blogPostPictureB2CRepository)
        {
            _eventPublisher = eventPublisher;
            _blogPostPictureB2CRepository = blogPostPictureB2CRepository;
        }

        public void InsertBlogPostPicture(BlogPostPictureB2C blogPostPicture)
        {
            if (blogPostPicture == null)
                throw new ArgumentNullException(nameof(blogPostPicture));

            _blogPostPictureB2CRepository.Insert(blogPostPicture);

            //event notification
            _eventPublisher.EntityInserted(blogPostPicture);
        }

        public virtual void UpdateBlogPostPicture(BlogPostPictureB2C blogPostPicture)
        {
            if (blogPostPicture == null)
                throw new ArgumentNullException(nameof(blogPostPicture));

            var model = GetBlogPostPictureById(blogPostPicture.BlogPostId);
            model.BlogPostId = blogPostPicture.BlogPostId;
            model.PictureId = blogPostPicture.PictureId;

            _blogPostPictureB2CRepository.Update(model);

            //event notification
            _eventPublisher.EntityUpdated(blogPostPicture);
        }

        public virtual BlogPostPictureB2C GetBlogPostPictureById(int blogPostId)
        {
            if (blogPostId == 0)
                return null;

            var result = _blogPostPictureB2CRepository.Table.Where(x => x.BlogPostId == blogPostId).FirstOrDefault();
            return result;
        }

        public void DeleteBlogPostPicture(BlogPostPictureB2C blogPostPicture)
        {
            if (blogPostPicture == null)
                throw new ArgumentNullException(nameof(blogPostPicture));

            _blogPostPictureB2CRepository.Delete(blogPostPicture);
        }
    }
}
