using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services.Api;
using Timeline.Services.Mapper;
using Timeline.Services.Timeline;
namespace Timeline.Controllers
{
    /// 
    /// Api related to timeline bookmarks.
    /// 
    [ApiController]
    [ProducesErrorResponseType(typeof(CommonResponse))]
    public class BookmarkTimelineController : MyControllerBase
    {
        private readonly IBookmarkTimelineService _service;
        private readonly ITimelineService _timelineService;
        private readonly IGenericMapper _mapper;
        public BookmarkTimelineController(IBookmarkTimelineService service, ITimelineService timelineService, IGenericMapper mapper)
        {
            _service = service;
            _timelineService = timelineService;
            _mapper = mapper;
        }
        private Task> Map(List timelines)
        {
            return _mapper.MapListAsync(timelines, Url, User);
        }
        /// 
        /// Get bookmark list in order.
        /// 
        /// Bookmarks.
        [HttpGet("bookmarks")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(401)]
        public async Task>> List()
        {
            var ids = await _service.GetBookmarksAsync(GetUserId());
            var timelines = await _timelineService.GetTimelineList(ids);
            return await Map(timelines);
        }
        /// 
        /// Add a bookmark.
        /// 
        /// Timeline name.
        [HttpPut("bookmarks/{timeline}")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task> Put([GeneralTimelineName] string timeline)
        {
            var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline);
            var create = await _service.AddBookmarkAsync(GetUserId(), timelineId);
            return CommonPutResponse.Create(create);
        }
        /// 
        /// Remove a bookmark.
        /// 
        /// Timeline name.
        [HttpDelete("bookmarks/{timeline}")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task> Delete([GeneralTimelineName] string timeline)
        {
            var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline);
            var delete = await _service.RemoveBookmarkAsync(GetUserId(), timelineId);
            return CommonDeleteResponse.Create(delete);
        }
        /// 
        /// Move a bookmark to new posisition.
        /// 
        /// Request body.
        [HttpPost("bookmarkop/move")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task Move([FromBody] HttpBookmarkTimelineMoveRequest request)
        {
            var timelineId = await _timelineService.GetTimelineIdByNameAsync(request.Timeline);
            await _service.MoveBookmarkAsync(GetUserId(), timelineId, request.NewPosition!.Value);
            return OkWithCommonResponse();
        }
    }
}