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

namespace Sledgemapper.Api.Controllers
{
    [Authorize]
    [Route("[controller]/{campaign}/{mapName}")]
    public class MapController : ControllerBase
    {
        private readonly IMediator _mediator;
        private string UserId => HttpContext.User.Claims.FirstOrDefault(m => m.Type == "Id").Value;

        public MapController(IMediator mediator) => _mediator = mediator;

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

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

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

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

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

        [HttpPost("note")]
        public async Task Post(string campaign, string mapName, [FromBody] Note note)
        {
            await _mediator.Send(new NewNoteCommand(mapName, note, UserId));
        }

        [HttpPost("room")]
        public async Task Post(string campaign, string mapName, [FromBody] Room room)
        {
            await _mediator.Send(new NewRoomCommand(mapName, room, UserId));
        }

        [HttpPost("line")]
        public async Task Post(string campaign, string mapName, [FromBody] Line line)
        {
            await _mediator.Send(new NewLineCommand(mapName, line, UserId));
        }

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

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

        [HttpDelete("note")]
        public async Task Delete(string campaign, string mapName, [FromBody] Note note)
        {
            await _mediator.Send(new DeleteNoteCommand(mapName, note, UserId));
        }
    }
}