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

namespace Sledgemapper.Api.Handlers
{
    public class NewTileCommandHandler : BaseCommandHandler<NewTileCommand, bool>
    {
        public NewTileCommandHandler(IMediator mediator, SledgemapperDbContext dbContext) : base(mediator, dbContext)
        {
        }

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