using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sledgemapper.Api.Commands;
using Sledgemapper.Shared.Entities;
using System.Threading.Tasks;

namespace Sledgemapper.Api.Controllers
{
    [Authorize]
    [Route("[controller]/{sessionName}")]
    public class SessionController : ControllerBase
    {
        private readonly IMediator _mediator;
        private int UserId => int.Parse(HttpContext.User.Identity.Name);
        
        public SessionController(IMediator mediator) => _mediator = mediator;

        [HttpPost]
        public async Task<bool> Post(string sessionName)
        {
            var result = await _mediator.Send(new NewSessionCommand(sessionName, UserId));
            return result;
        }

        [HttpGet]
        public async Task<Session> Get(string sessionName)
        {
            var result = await _mediator.Send(new GetMapSnapshotCommand(sessionName));
            return result;
        }

        [HttpPost("snapshot")]
        public async Task Post(string sessionName, [FromBody] Session session)
        {
            await _mediator.Send(new NewSnapshotCommand(sessionName, session, UserId));
        }

        [HttpPost("tile")]
        public async Task Post(string sessionName, [FromBody] Tile tile)
        {
            await _mediator.Send(new NewTileCommand(sessionName, tile, UserId));
        }

        [HttpPost("overlay")]
        public async Task Post(string sessionName, [FromBody] Overlay overlay)
        {
            await _mediator.Send(new NewOverlayCommand(sessionName, overlay, UserId));
        }

        [HttpPost("wall")]
        public async Task Post(string sessionName, [FromBody] Wall wall)
        {
            await _mediator.Send(new NewWallCommand(sessionName, wall, UserId));
        }

        [HttpDelete("tile")]
        public async Task Delete(string sessionName, [FromBody] Tile tile)
        {
            await _mediator.Send(new DeleteTileCommand(sessionName, tile, UserId));
        }

        [HttpDelete("overlay")]
        public async Task Delete(string sessionName, [FromBody] Overlay overlay)
        {
            await _mediator.Send(new DeleteOverlayCommand(sessionName, overlay, UserId));
        }

        [HttpDelete("wall")]
        public async Task Delete(string sessionName, [FromBody] Wall wall)
        {
            await _mediator.Send(new DeleteWallCommand(sessionName, wall, UserId));
        }
    }
}