using System;
using System.Collections.Generic;
using Chloe.Server.Data.Contracts;
using Chloe.Server.Dtos;
using Chloe.Server.Services.Contracts;
using System.Data.Entity;
using System.Linq;
using Chloe.Server.Models;

namespace Chloe.Server.Services
{
    public class TextWeightService : ITextWeightService
    {
        public TextWeightService(IChloeUow uow, ICacheProvider cacheProvider)
        {
            this.uow = uow;
            this.repository = uow.TextWeights;
            this.cache = cacheProvider.GetCache();
        }

        public TextWeightAddOrUpdateResponseDto AddOrUpdate(TextWeightAddOrUpdateRequestDto request)
        {
            var entity = repository.GetAll()
                .FirstOrDefault(x => x.Id == request.Id && x.IsDeleted == false);
            if (entity == null) repository.Add(entity = new TextWeight());
            entity.Name = request.Name;
            uow.SaveChanges();
            return new TextWeightAddOrUpdateResponseDto(entity);
        }

        public dynamic Remove(int id)
        {
            var entity = repository.GetById(id);
            entity.IsDeleted = true;
            uow.SaveChanges();
            return id;
        }

        public ICollection<TextWeightDto> Get()
        {
            ICollection<TextWeightDto> response = new HashSet<TextWeightDto>();
            var entities = repository.GetAll().Where(x => x.IsDeleted == false).ToList();
            foreach(var entity in entities) { response.Add(new TextWeightDto(entity)); }    
            return response;
        }


        public TextWeightDto GetById(int id)
        {
            return new TextWeightDto(repository.GetAll().Where(x => x.Id == id && x.IsDeleted == false).FirstOrDefault());
        }

        protected readonly IChloeUow uow;
        protected readonly IRepository<TextWeight> repository;
        protected readonly ICache cache;
    }
}
