using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Request.App.Note;
using GxPress.Result.App.Note;
using GxPress.Service.Interface.Note;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 个人笔记
    /// </summary>
    [Route("api/app/note")]
    [ApiController]
    [Authorize]
    public class NoteController : ControllerBase
    {
        private readonly ILogger<NoteController> _logger;
        private readonly INoteService _noteService;
        private readonly ILoginContext _loginContext;

        public NoteController(ILogger<NoteController> logger, INoteService noteService, ILoginContext loginContext)
        {
            _logger = logger;
            _noteService = noteService;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 添加笔记
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task<bool> InsertNote(NoteInRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            request.UserId = _loginContext.AccountId;
            return await _noteService.InsertNoteAsync(request);
        }

        /// <summary>
        /// 获取笔记分页
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<PagedList<NotePageListRequest>> NotePageList(NoteSearchPageListRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _noteService.NotePageListAsync(request);
        }
        /// <summary>
        /// 获取笔记详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<NoteDetailResult> GetNoteDetail(int id)
        {
            var note = await _noteService.GetNoteDetailAsync(id, _loginContext.AccountId);
            return note;
        }
        /// <summary>
        /// 获取笔记详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("web/{id}")]
        [AllowAnonymous]
        public async Task<NoteDetailResult> GetWebNoteDetail(int id)
        {
            var note = await _noteService.GetNoteDetailAsync(id, 0);
            return note;
        }

        /// <summary>
        /// 修改笔记
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<bool> UpdateNote(Entity.Note.Note note)
        {
            // note.HtmlContent = StringUtils.RemoveEmoji(note.HtmlContent);
            // note.Content = StringUtils.RemoveEmoji(note.Content);
            // note.Title = StringUtils.RemoveEmoji(note.Title);
            return await _noteService.UpdateNoteAsync(note);
        }
        /// <summary>
        /// 删除笔记
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> DeleteAsync(int id) => await _noteService.DeleteAsync(id, _loginContext.AccountId);
    }
}