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

namespace Sledgemapper.Api.Controllers
{
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [Route("[controller]")]
    public class CampaignController : ControllerBase
    {
        private readonly IMediator _mediator;
        private string UserId => HttpContext.User.Claims.FirstOrDefault(m => m.Type == "Id").Value;

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

        [HttpPost]
        [Route("{campaignName}")]
        public async Task<ActionResult<bool>> Post(string campaignName)
        {
            var result = await _mediator.Send(new NewCampaignCommand(campaignName, UserId.ToString()));
            if (!result)
            {
                return BadRequest();
            }    
            return result;
        }

        //[HttpPost]
        //[Route("{campaignName}")]
        //public async Task<Guid> Get(string campaignName)
        //{
        //    var result = await _mediator.Send(new GetCampaignCommand(campaignName, UserId.ToString()));
        //    return result;
        //}

        [HttpGet]
        public async Task<List<Campaign>> Get()
        {
            var result = await _mediator.Send(new GetCampaignsCommand(UserId));
            return result;
        }

        [HttpPost]
        [Route("{campaignName}/players/{email}")]
        public async Task<bool> Invite(string campaignName, string email)
        {
            var result = await _mediator.Send(new InvitePlayerToCampaignCommand(campaignName, email, UserId.ToString()));
            return result;
        }

        [HttpGet]
        [Route("{campaignName}/players")]
        public async Task<List<Player>> GetPlayers(string campaignName)
        {
            var result = await _mediator.Send(new GetCampaignPlayersCommand(campaignName, UserId.ToString()));
            return result;
        }


        [HttpGet]
        [Route("{campaignName}/maps")]
        public async Task<List<Session>> GetMaps(string campaignName)
        {
            var result = await _mediator.Send(new GetCampaignMapsCommand(campaignName, UserId.ToString()));
            return result;
        }

    }
}