using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface;
using GxPress.Request.Comment;
using GxPress.Result.Comment;
using Microsoft.AspNetCore.Mvc;
using GxPress.Common.Exceptions;
using Microsoft.AspNetCore.Authorization;

namespace GxPress.Api.WebControllers
{
    /// <summary>
    /// 评论
    /// </summary>
    [Route("api/web/comment")]
    [ApiController]
    [Authorize]
    public class WebCommentController : Controller
    {
        private readonly ICommentRepository _commentRepository;
        private readonly ILoginContext _loginContext;
        public WebCommentController(ICommentRepository commentRepository, ILoginContext loginContext)
        {
            _commentRepository = commentRepository;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<bool> Add([FromBody] CommentInRequest request)
        {
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            request.UserId = _loginContext.AccountId;
            if (string.IsNullOrWhiteSpace(request.Content) && string.IsNullOrWhiteSpace(request.JsonContent) && string.IsNullOrWhiteSpace(request.HtmlContent))
                throw new BusinessException("评论内容为空");
            return await _commentRepository.CommentInAsync(request) > 0;
        }
        /// <summary>
        /// 分页显示数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<PagedList<CommentPageResult>> GetPagedList([FromBody] CommentSearchPageRequest request)
        {
            return await _commentRepository.GetPagedList(request);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task<bool> Delete([FromBody] CommentDeleteRequest request)
        {
            return await _commentRepository.DeleteCommentAsync(request);
        }

        /// <summary>
        /// 修改评论
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<bool> UpdateComment([FromBody] CommentUpdateRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _commentRepository.UpdateCommentAsync(request);
        }
    }
}