using System.Collections.Generic;
using System.Linq;
using Demo.Core.Enums;
using Demo.Core.Models;

namespace Demo.Data
{
    public class PtItemsRepository : IPtItemsRepository
    {
        private PtInMemoryContext context;

        public PtItemsRepository(PtInMemoryContext context)
        {
            this.context = context;
        }

        public IEnumerable<PtItem> GetAll()
        {
            return context.PtItems;
        }

        public IEnumerable<PtItem> GetClosedItems()
        {
            return context.PtItems.Where(i => i.Status == StatusEnum.Closed &&
            i.DateDeleted == null);
        }

        public PtItem GetItemById(int itemId)
        {
            var item = context.PtItems.SingleOrDefault(i => i.Id == itemId);

            return item;
        }

        public IEnumerable<PtItem> GetOpenItems()
        {
            return context.PtItems.Where(i => (i.Status == StatusEnum.Open ||
                                      i.Status == StatusEnum.ReOpened) &&
                                        i.DateDeleted == null);
        }

        public IEnumerable<PtItem> GetUserItems(int userId)
        {
            return context.PtItems.Where(i => i.Assignee.Id == userId &&
                                    i.DateDeleted == null);
        }
    }
}
