﻿using CommunityEvents.Entities;
using CommunityEvents.Services.Contracts;
using Infrastructure.DataAccess.Contracts;
using Infrastructure.DataAccess.Entities.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommunityEvents.Services
{
    public class SlotService : ISlotService
    {
        private IUnitOfWorkForService unitOfWork;

        public SlotService(IUnitOfWorkForService unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }                                        

        public IEnumerable<Slot> GetSlots()
        {
            return this.unitOfWork.Repository<Slot>().Query().Get().AsEnumerable();
        }

        public Slot GetSlot(Guid id)
        {
            return this.unitOfWork.Repository<Slot>().Find(id);
        }
                
        public void UpdateSlot(Slot slot)
        {
            slot.ObjectState = ObjectState.Modified;
            this.unitOfWork.Repository<Slot>().Update(slot);            
        }
                   
        public Slot AddSlot(Slot slot)
        {
            this.unitOfWork.Repository<Slot>().Insert(slot);

            return slot;
        }
                
        public Slot DeleteSlot(Guid id)
        {
            Slot slot = this.unitOfWork.Repository<Slot>().Find(id);
            
            if (slot != null)
            {
                this.unitOfWork.Repository<Slot>().Delete(slot);
            }

            return slot;
        }

        public bool SlotExists(Guid id)
        {
            var query = this.unitOfWork.Repository<Slot>()
                .Query()
                .Filter(c => c.Id.Equals(id))
                .Get();

            return query.Count() > 0;    
        }
    }
}