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

namespace Sledgemapper.Api.Controllers
{
    [Authorize]
    [Route("[controller]/{campaign}/{mapName}")]
    public class MapController : ControllerBase
    {
        private readonly IMediator _mediator;

        public MapController(IMediator mediator)
        {
            _mediator = mediator;
        }

        private string UserId => HttpContext.User.Claims.FirstOrDefault(m => m.Type == "Id").Value;

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

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

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

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

        [HttpPost]
        public async Task<Guid> Post(string campaign, string mapName)
        {
            var result = await _mediator.Send(new NewSessionCommand(campaign, mapName, UserId));
            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(Guid campaign, Guid mapName, [FromBody] Overlay overlay)
        {
            await _mediator.Send(new NewOverlayCommand(campaign, mapName, overlay, UserId));
        }

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

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

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

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