using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.Notice;
using GxPress.Request.NoticeAnalyze;
using GxPress.Result.Notice;
using GxPress.Service.Interface.Notice;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 通知
    /// </summary>
    [Route("api/app/notice")]
    [ApiController]
    [Authorize]
    public class NoticeController : ControllerBase
    {
        private readonly INoticeRepository _noticeRepository;
        private readonly INoticeFolderRepository _noticeFolderRepository;
        private readonly INoticeService _noticeService;
        private readonly ILoginContext _loginContext;

        public NoticeController(INoticeRepository noticeRepository, INoticeFolderRepository noticeFolderRepository,INoticeService noticeService, ILoginContext loginContext)
        {
            _noticeRepository = noticeRepository;
            _noticeFolderRepository = noticeFolderRepository;
            _noticeService = noticeService;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 添加通知
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task<bool> Insert(NoticeInRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            request.UserId = _loginContext.AccountId;
            var boolValue = await _noticeService.InsertAsync(request);
            return boolValue;
        }

        /// <summary>
        /// 自己删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task<bool> Delete(NoticeDeRequest request)
        {
            return await _noticeRepository.DeleteAsync(request);
        }
        /// <summary>
        /// 获取通知详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("app-detail/{id}")]
        public async Task<Result.Notice.NoticeDetailResult> GetNoticeDetailResult(int id)
        {
            return await _noticeService.GetNoticeDetailAsync(id, _loginContext.AccountId);
        }

        /// <summary>
        /// app通知分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("list-page")]
        public async Task<PagedList<NoticeListPageResult>> GetNoticeListPage(NoticePageSearchRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _noticeService.GetNoticeListPageAsync(request);
        }
        /// <summary>
        /// 恢复删除通知
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("recover")]
        public async Task<bool> RecoverDelete(NoticeDeRequest request)
        {
            return await _noticeRepository.RecoverDeleteAsync(request);
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        public async Task<NoticeEditDetailResult> Detail(int id)
        {
            return await _noticeService.NoticeEditDetailAsync(id);
        }

        /// <summary>
        /// 详情 web专属
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("web-detail/{id}")]
        [AllowAnonymous]
        public async Task<NoticeEditDetailResult> WebDetail(int id)
        {
            return await _noticeService.NoticeEditDetailAsync(id);
        }
        /// <summary>
        /// 修改详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task<bool> Edit([FromBody] NoticeUpRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            request.UserId = _loginContext.AccountId;
            return await _noticeService.NoticeUpAsync(request);
        }
        /// <summary>
        /// 查询通知已读未读信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("read")]
        public async Task<IEnumerable<NoticeReadListResult>> NoticeReadList(NoticeReadListRequest request)
        {
            return await _noticeService.NoticeReadListAsync(request);
        }
        /// <summary>
        /// 撤销通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("recall/{id}")]
        public async Task<bool> RecallAsync(int id)
        {
            return await _noticeService.RecallAsync(id);
        }
    }
}