using DocumentFormat.OpenXml.Spreadsheet;
using Nop.Core;
using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Domains;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;

namespace Nop.Plugin.Misc.AuctionPlugin.Services
{
    public class AnnouncementService : IAnnouncementService
    {
        #region Field

        private readonly IRepository<Announcement> _announcementRepository;

        #endregion

        #region Ctr

        public AnnouncementService(IRepository<Announcement> announcementRepository)
        {
            _announcementRepository = announcementRepository;
        }

        #endregion

        #region Methods

        public async Task DeleteAsync(Announcement announcement)
        {
            await _announcementRepository.DeleteAsync(announcement);
        }



        public async Task<bool> UpdateAsync(Announcement announcement)
        {
            if (announcement == null) throw new ArgumentNullException("customer");
            
            await _announcementRepository.UpdateAsync(announcement);
            return true;
        }


        public async Task InsertAsync(Announcement announcement)
        {
            await _announcementRepository.InsertAsync(announcement);
        }


        public async Task<IPagedList<Announcement>> GetAnnouncementsAsync(int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var announcements = _announcementRepository.Table.OrderBy(announcement => announcement.IsActive).ToList();

            return new PagedList<Announcement>(announcements, pageIndex, pageSize);
        }

        public async Task<Announcement> GetAnnouncementDesignFirstAsync()
        {
            return await _announcementRepository.Table.FirstOrDefaultAsync(announcement => announcement.IsActive == true);

            //var result = await _announcementRepository.GetAllAsync(announcements => announcements.Where(record => record.IsActive == true));

            ////var query = from c in _announcementRepository.Table

            ////            where c.IsActive == true

            ////            orderby c.CreateDate descending

            ////            select c;

            //var LatestAnnouncement = result.ToList().FirstOrDefault();

            //return LatestAnnouncement;
        }

        public async Task<Announcement> GetAnnouncementByIdAsync(int id)
        {
            return await _announcementRepository.GetByIdAsync(id);
        }
        #endregion
    }
}
