using AutoMapper;
using MediatR;
using NetworkResurrector.Api.Application.Extensions;
using NetworkResurrector.Api.Application.Services.Abstractions;
using NetworkResurrector.Api.Domain.Repositories;
using System.Threading;
using System.Threading.Tasks;

namespace NetworkResurrector.Api.Application.Queries
{
    public class GetMachines
    {
        public class Query : IRequest<Model[]> { }

        public class Model
        {
            public int MachineId { get; set; }
            public string MachineName { get; set; }
            public string FullMachineName { get; set; }
            public string MACAddress { get; set; }
            public string IPv4Address { get; set; }
            public string Description { get; set; }
        }

        public class QueryHandler : IRequestHandler<Query, Model[]>
        {
            private readonly INetworkRepository _repository;
            private readonly IMapper _mapper;
            private readonly IUserService _userService;

            public QueryHandler(INetworkRepository repository, IMapper mapper, IUserService userService)
            {
                _repository=repository;
                _mapper=mapper;
                _userService=userService;
            }

            public async Task<Model[]> Handle(Query request, CancellationToken cancellationToken)
            {
                var isGuest = await _userService.GetIsGuest();
                var machines = await _repository.GetMachines();
                var result = _mapper.Map<Model[]>(machines);

                if (isGuest)
                    result.Fake();

                return result;
            }
        }
    }
}
