using AutoMapper;
using MediatR;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UserManagement.Common.UnitOfWork;
using UserManagement.Data;
using UserManagement.Data.Dto;
using UserManagement.Domain;
using UserManagement.Helper;
using UserManagement.MediatR.CommandAndQuery;
using UserManagement.Repository;

namespace UserManagement.MediatR.Handlers
{
    public class AddReminderCommandHandler
        : IRequestHandler<AddReminderCommand, ServiceResponse<ReminderDto>>
    {

        private readonly IReminderRepository _reminderRepository;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork<UserContext> _uow;
        private readonly UserInfoToken _userInfoToken;

        public AddReminderCommandHandler(IReminderRepository reminderRepository,
            IMapper mapper,
            IUnitOfWork<UserContext> uow,
            UserInfoToken userInfoToken)
        {
            _reminderRepository = reminderRepository;
            _mapper = mapper;
            _uow = uow;
            _userInfoToken = userInfoToken;
        }

        public async Task<ServiceResponse<ReminderDto>> Handle(AddReminderCommand request, CancellationToken cancellationToken)
        {
            if (!request.Frequency.HasValue)
            {
                request.Frequency = Frequency.OneTime;
            }
            if (!request.IsRepeated)
            {
                request.Frequency = Frequency.OneTime;
            }

            if (!request.ReminderUsers.Any(c => c.UserId == Guid.Parse(_userInfoToken.Id)))
            {
                request.ReminderUsers.Add(new ReminderUserDto
                {
                    UserId = Guid.Parse(_userInfoToken.Id)
                });
            }
            var reminder = _mapper.Map<Reminder>(request);
            _reminderRepository.Add(reminder);
            if (await _uow.SaveAsync() <= 0)
            {
                return ServiceResponse<ReminderDto>.Return500();
            }

            return ServiceResponse<ReminderDto>.ReturnResultWith201(_mapper.Map<ReminderDto>(reminder));
        }
    }
}
