using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wierszowki.Core.Interfaces;
using Wierszowki.Core.Linq;
using xVal.ServerSide;

namespace Wierszowki.Services
{
    public class MagazineItemService
    {
        private readonly IRepository<MagazineItem> _repository;
        private readonly IRepository<User> _userRepository;

        public MagazineItemService()
        {
            _repository = new LinqRepository<MagazineItem>();
            _userRepository = new LinqRepository<User>();
        }

        public MagazineItemService(IRepository<MagazineItem> repository, IRepository<User> userRepository)
        {
            _repository = repository;
            _userRepository = userRepository;
        }

        public MagazineItem Find(int id)
        {
            return _repository.Find(id).SingleOrDefault();            
        }

        public void Create(MagazineItem magazineItem, string currentUser)
        {

            var errors = DataAnnotationsValidationRunner.GetErrors(magazineItem);
            if (errors.Any())
            {
                throw new RulesException(errors);
            }
            var user = _userRepository.FindOne(u => u.Login == currentUser);
            var now = DateTime.Now;
            magazineItem.CreatedBy = user.Id;
            magazineItem.CreatedOn = now;
            magazineItem.UpdatedBy = user.Id;
            magazineItem.UpdatedOn = now;
            
            _repository.Insert(magazineItem);
            
        }
        public void Update(MagazineItem magazineItem, string currentUser)
        {
            var errors = DataAnnotationsValidationRunner.GetErrors(magazineItem);
            if (errors.Any())
                throw new RulesException(errors);

            var user = _userRepository.FindOne(u => u.Login == currentUser);
            var now = DateTime.Now;

            var magazineItemToUpdate = _repository.FindOne(magazineItem.Id);           
            magazineItemToUpdate.AuthorId = magazineItem.AuthorId;
            magazineItemToUpdate.Bonus = magazineItem.Bonus;
            magazineItemToUpdate.Caption = magazineItem.Caption;
            magazineItemToUpdate.Date = magazineItem.Date;
            magazineItemToUpdate.Description = magazineItem.Description;
            magazineItemToUpdate.IssueId = magazineItem.IssueId;
            magazineItemToUpdate.ItemTypeId = magazineItem.ItemTypeId;
            magazineItemToUpdate.Price = magazineItem.Price;
            magazineItemToUpdate.UpdatedBy = user.Id;
            magazineItemToUpdate.UpdatedOn = now;
            _repository.Update(magazineItemToUpdate);
        }
        public MagazineItem Delete(MagazineItem magazineItem)
        {
            var magazineItemToDelete = _repository.FindOne(magazineItem.Id);
            if (magazineItemToDelete != null)
            {
                _repository.Delete(magazineItemToDelete);
            }
            return magazineItemToDelete;
        }
    }
}
