using System.Collections.Generic;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Request.Analyze;
using GxPress.Request.App.Topic;
using GxPress.Result;
using GxPress.Result.App.Topic;
using Newtonsoft.Json;

namespace GxPress.Service.Implement.Topic
{
    public partial class TopicService
    {
        /// <summary>
        /// 获取话题详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<TopicDetailResult> GetTopicDetailResultAsync(TopicDetailRequest request)
        {
            var topic = await _topicRepository.GetAsync(request.Id);
            if (topic == null)
                throw new BusinessException("通知不存在");
            var result = _mapper.Map<TopicDetailResult>(topic);

            if (string.IsNullOrWhiteSpace(result.Content))
                result.Content = "[]";
            var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
            foreach (var item in contentJsonData)
                item.File = StringUtils.AddDomain(item.File);
            result.Data = contentJsonData;
            if (request.UserId == 0)
                return result;
            //是否是创建人
            result.IsAdmin = topic.UserId == request.UserId;
            //获取用户
            var user = await _userRepository.GetAsync(result.UserId);
            result.UserName = user.Name;
            //访问
            if (await _visitService.AddVisit(request.UserId, 2, request.Id))
                result.ReadCount++;
            //访问量
            result.ReadCount = await _visitService.GetCountAsync(2, request.Id);
            var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
            analyzeRequest.TypeValue = 1;
            analyzeRequest.AnalyzeType = 1;
            analyzeRequest.SourceId = request.Id;
            analyzeRequest.UserId = request.UserId;
            //点赞数量
            result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
            //获取话题的评论数量
            var commentCount =
                await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), request.Id).Where(nameof(Entity.Comment.TypeValue), 1).Where(nameof(Entity.Comment.Pid), 0));
            result.CommentCount = commentCount;
            //获取话题的转发数量
            analyzeRequest.AnalyzeType = 4;
            var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
            result.RetransmissionCount = retransmissionCount;
            result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的收藏数量
            analyzeRequest.AnalyzeType = 3;
            var collectCount = await _analyzeService.CountAsync(analyzeRequest);
            result.CollectCount = collectCount;
            //是否收藏
            result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的点赞数量
            analyzeRequest.AnalyzeType = 1;
            var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
            result.PraiseCount = praiseCount;
            //是否点赞
            result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
            var praisePageSearchRequest = new PraisePageSearchRequest { SourceId = request.Id, TypeValue = 1, Page = 1, PerPage = 3 };
            result.TopicPraisePagedList = await _analyzeService.GetPraisePageAsync(praisePageSearchRequest);
            return result;
        }


    }
}