using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Infrastructure.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Sledgemapper.Api.Handlers
{
    public class DeleteTileCommandHandler : BaseCommandHandler<DeleteTileCommand, bool>
    {
        public DeleteTileCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) : base(mediator, dbcontext)
        {
        }

        public override async Task<bool> Handle(DeleteTileCommand command, CancellationToken cancellationToken)
        {
            await CheckAuthorization(command);
            var jsonString = JsonSerializer.Serialize(command.Tile);
            var session = await SaveLog(command, "D", "T", jsonString, cancellationToken);
            await Mediator.Publish(new DeleteTileNotification(session, command.Tile, command.UserId), cancellationToken);
            return true;
        }
    }
}
