using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface.Missive;
using GxPress.Request.App.Missive;
using GxPress.Result.App.Missive;
using GxPress.Service.Interface.Missive;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System;
namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 站内信
    /// </summary>
    [Route("api/app/missive")]
    [ApiController]
    [Authorize]
    public class MissiveController : ControllerBase
    {
        private readonly IMissiveRepository _missiveRepository;
        private readonly IMissiveService _missiveService;
        private readonly ILoginContext _loginContext;

        public MissiveController(IMissiveRepository missiveRepository, IMissiveService missiveService,
            ILoginContext loginContext)
        {
            _missiveRepository = missiveRepository;
            _missiveService = missiveService;
            _loginContext = loginContext;
        }


        /// <summary>
        /// 根据站内信ID获取详情
        /// </summary>
        /// <param name="missiveId"></param>
        /// <returns></returns>
        [HttpGet("get/{missiveId}")]
        public async Task<MissiveInfoResult> GetMissiveInfo(int missiveId)
        {
            return await _missiveService.GetMissiveInfoAsync(missiveId, _loginContext.AccountId);
        }

        /// <summary>
        /// 添加/修改站内信
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task<bool> Insert(MissiveInRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            // request.Title = StringUtils.RemoveEmoji(request.Title);
            request.UserId = _loginContext.AccountId;
            request.MissiveAddresseeUserIds.Remove(request.UserId);
            request.MissiveCcUserIds.Remove(request.UserId);
            return await _missiveService.InsertOrUpdateAsync(request);
        }

        /// <summary>
        /// 获取站内信列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<PagedList<MissiveSearchResult>> GetMissiveSearchResult(MissiveSearchRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _missiveService.GetMissiveSearchResultAsync(request);
        }
        /// <summary>
        /// 获取站内信详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MissiveDetailResult> GetMissiveDetail(int id)
        {
            return await _missiveService.GetMissiveDetailAsync(id);
        }
        /// <summary>
        /// 获取站内信详情-web专属
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("web/{id}")]
        [AllowAnonymous]
        public async Task<MissiveInfoResult> GetWebMissiveDetail(int id)
        {
            var missiveInfos = await _missiveService.GetMissiveInfoAsync(id, 0);
            missiveInfos.Items = missiveInfos.Items.OrderByDescending(n => Convert.ToDateTime(n.CreatedDate)).ToList();
            return missiveInfos;
        }
        /// <summary>
        /// 获取站内信已读未读数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("read")]
        public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
        {
            return await _missiveService.GetMissiveReadListAsync(request);
        }

        /// <summary>
        /// 站内信撤回
        /// </summary>
        /// <param name="missiveId"></param>
        /// <returns></returns>
        [HttpPut("recall/{missiveId}")]
        public async Task<bool> RecallAsync(int missiveId)
        {
            return await _missiveService.RecallAsync(missiveId);
        }
    }
}