using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Entity.Topic;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Topic;
using GxPress.Request.App.Topic;
using GxPress.Request.App.TopicAnalyze;
using GxPress.Request.App.TopicComment;
using GxPress.Result.App.Topic;
using GxPress.Result.App.TopicComment;
using GxPress.Service.Interface.Middle;
using GxPress.Service.Interface.Topic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// APP话题
    /// </summary>
    [Route("api/app/topic")]
    [ApiController]
    [Authorize]
    public class TopicController : ControllerBase
    {
        private readonly ILogger<TopicController> _logger;
        private readonly ITopicRepository _topicRepository;
        private readonly ILoginContext _loginContext;
        private readonly ITopicService _topicService;
        private readonly ITopicGroupRepository _topicGroupRepository;
        private readonly ITopicGroupUserRepository _topicGroupUserRepository;
        private readonly ITopicAnalyzeRepository _topicAnalyzeRepository;
        private readonly ITopicCommentRepository _topicCommentRepository;
        private readonly IMiddleService _middleService;

        public TopicController(ILogger<TopicController> logger, ITopicRepository repository, ILoginContext loginContext,
            ITopicService topicService, ITopicGroupRepository topicGroupRepository,
            ITopicGroupUserRepository topicGroupUserRepository, ITopicAnalyzeRepository topicAnalyzeRepository,
            ITopicCommentRepository topicCommentRepository, IMiddleService middleService)
        {
            _logger = logger;
            _topicRepository = repository;
            _loginContext = loginContext;
            _topicService = topicService;
            _topicGroupRepository = topicGroupRepository;
            _topicGroupUserRepository = topicGroupUserRepository;
            _topicAnalyzeRepository = topicAnalyzeRepository;
            _topicCommentRepository = topicCommentRepository;
            _middleService = middleService;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<bool> Insert(TopicInRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            // request.Title = StringUtils.RemoveEmoji(request.Title);
            request.UserId = _loginContext.AccountId;
            return await _topicService.InsertTopicAsync(request);
        }
        
        /// <summary>
        /// 获取话题详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("app-detail")]
        public async Task<TopicDetailResult> GetTopicDetailResult(TopicDetailRequest request)
        {
            request.UserId = _loginContext.AccountId;
            var topicDetail = await _topicService.GetTopicDetailResultAsync(request);
            return topicDetail;
        }
        /// <summary>
        /// 获取话题详情 web专属
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("web-app-detail")]
        [AllowAnonymous]
        public async Task<TopicDetailResult> GetWebTopicDetailResult(TopicDetailRequest request)
        {
            var topicDetail = await _topicService.GetTopicDetailResultAsync(request);
            return topicDetail;
        }


        /// <summary>
        /// app话题分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("list-page")]
        public async Task<PagedList<TopicListPageResult>> GetTopicListPage(TopicPageSearchRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _topicService.GetTopicPageAsync(request);
        }

        /// <summary>
        /// 获取用户话题分组
        /// </summary>
        /// <returns></returns>
        [HttpPost("group")]
        public async Task<IEnumerable<TopicGroup>> GetTopicGroups()
        {
            return await _topicGroupRepository.GetTopicGroupsAsync(_loginContext.AccountId);
        }

        /// <summary>
        /// 添加话题分组
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add-group")]
        public async Task<bool> InsertTopicGroup(TopicGroupInRequest request)
        {
            var topicGroup = new TopicGroup { Name = request.Name, UserId = _loginContext.AccountId };
            return await _topicGroupRepository.InsertAsync(topicGroup);
        }

        /// <summary>
        /// 修改分组名称
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("update-group")]
        public async Task<bool> UpdateGroup(TopicGroupInRequest request)
        {
            return await _topicGroupRepository.UpdateAsync(request);
        }

        /// <summary>
        /// 根据话题分组Id删除数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete("delete_group")]
        public async Task<bool> DeleteTopicGroup([FromBody] TopicGroupDeRequest request)
        {
            return await _topicService.DeleteTopicGroupAsync(request.Ids);
        }

        /// <summary>
        /// 根据分组ID查询用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<PagedList<TopicGroupUserResult>> GetTopicGroupUserById(int id)
        {
            return await _topicGroupUserRepository.GetTopicGroupUserById(id);
        }

        /// <summary>
        /// 添加话题小组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add-group-user")]
        public async Task<bool> InsertAsync([FromBody]TopicGroupUserInRequest request)
        {
            return await _topicService.InsertTopicGroupUserAsync(request.UserIds.Distinct().ToList(), request.TopicGroupId);
        }

        /// <summary>
        /// 根据ID删除话题分组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete("group-user")]
        public async Task<bool> DeleteUser(TopicGroupDeRequest request)
        {
            return await _topicService.DeleteTopicGroupUserAsync(request.Ids);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("sort")]
        public async Task<bool> MoveSort(TopicMoveSortRequest request)
        {

            return await _topicGroupRepository.MoveSortAsync(request.FirstId, request.SecondId, _loginContext.AccountId);
        }

        /// <summary>
        /// 移动到
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("move")]
        public async Task<bool> MoveToGroup(MoveToGroupRequest request)
        {
            return await _topicService.MoveToGroupAsync(request);
        }

        /// <summary>
        /// 根据分组ID和姓名查询用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<PagedList<TopicGroupUserResult>> SearchTopicGroupUsers(SearchTopicGroupUsersRequest request)
        {
            return await _topicGroupUserRepository.SearchTopicGroupUsersAsync(request);
        }



        /// <summary>
        /// 删除话题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _topicService.DeleteAsync(id);
        }

        /// <summary>
        /// 修改话题
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<bool> Update(TopicUpdateRequest request)
        {
            // request.HtmlContent = StringUtils.RemoveEmoji(request.HtmlContent);
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            // request.Title = StringUtils.RemoveEmoji(request.Title);
            request.UserId = _loginContext.AccountId;
            return await _topicService.UpdateAsync(request);
        }
    }
}