using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface;
using GxPress.Request.Article;
using GxPress.Request.BlacklistArticle;
using GxPress.Result.Article;
using GxPress.Service.Interface;
using GxPress.Service.Interface.Article;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using GxPress.Repository.Interface.WorkFlow;
using GxPress.Request.App.Flow;
using GxPress.Service.Interface.Visit;
using GxPress.EnumConst;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 审批文章/通知
    /// </summary>
    [Route("api/web/article")]
    [ApiController]
    [Authorize]
    public class WebArticleController : ControllerBase
    {
        private readonly IArticleRepository _articleRepository;
        private readonly IBlacklistArticleRepository _blacklistArticleRepository;
        private readonly IArticleService _articleService;
        private readonly ILoginContext _loginContext;
        private readonly IFlowService _flowService;
        private readonly IUserRepository _userRepository;
        private readonly IFlowFieldValueRepository _flowFieldValueRepository;
        private readonly IVisitService _visitService;
        public WebArticleController(IArticleRepository articleRepository, IBlacklistArticleRepository blacklistArticleRepository,
            ILoginContext loginContext, IArticleService articleService, IFlowService flowService, IUserRepository userRepository,
            IFlowFieldValueRepository flowFieldValueRepository, IVisitService visitService)
        {
            _articleRepository = articleRepository;
            _blacklistArticleRepository = blacklistArticleRepository;
            _loginContext = loginContext;
            _articleService = articleService;
            _flowService = flowService;
            _userRepository = userRepository;
            _flowFieldValueRepository = flowFieldValueRepository;
            _visitService = visitService;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("user-article-list")]
        public async Task<PagedList<ArticleResult>> GetUserList([FromBody] ArticleSearchRequest request)
        {
            request.UserId = _loginContext.AccountId;
            //屏蔽文章查询文章
            var blacklistArticleSearchRequest = new BlacklistArticleSearchRequest { UserId = request.UserId };
            var blacklistArticles = await _blacklistArticleRepository.GetListAsync(blacklistArticleSearchRequest);
            var pagedList = await _articleRepository.GetUserListAsync(request, blacklistArticles);
            foreach (var item in pagedList.Items)
                item.ReadCount = await _visitService.GetCountAsync(AllTypeConst.Article.GetHashCode(), item.Id);
            var resultList = new List<Result.Article.ArticleResult>();
            if (!string.IsNullOrWhiteSpace(request.Keyword))
            {
                //获取代办事项
                var userId = _loginContext.AccountId;
                var flowListRequest = new FlowListRequest();
                flowListRequest.Page = request.Page;
                flowListRequest.PerPage = request.PerPage;
                flowListRequest.Keyword = request.Keyword;
                var flowTodoList = await _flowService.ListMyCheckingAsync(userId, flowListRequest, nameof(GxPress.EnumConst.FlowListTypeConst.MyChecking));
                foreach (var flowTodo in flowTodoList.Items)
                {
                    var result = new Result.Article.ArticleResult
                    {
                        Id = flowTodo.Id,
                        ProcessId = flowTodo.ProcessId,
                        FlowId = flowTodo.Id,
                        UserId = flowTodo.UserId,
                        Type = nameof(GxPress.EnumConst.TodoTypeConst.ApproverCheck),
                        State = flowTodo.State,
                        IsDone = false,
                        CreatedDate = flowTodo.CreatedDate
                    };
                    result.Title = flowTodo.Title;
                    result.AvatarUrl = flowTodo.AvatarUrl;
                    result.Summaries = flowTodo.Summaries;
                    result.SourceType = 1;
                    resultList.Add(result);
                }
            }
            resultList.AddRange(pagedList.Items);
            pagedList.Total = resultList.Count();
            pagedList.Items = resultList;
            return pagedList;
        }

        /// <summary>
        /// 文章详情
        /// </summary>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [AllowAnonymous]
        public async Task<ArticleResult> Get(int id)
        {
            var article = await _articleService.GetAsync(id, _loginContext.AccountId);
            return article;
        }


        /// <summary>
        /// 文章详情 web页面
        /// </summary>
        /// <returns></returns>
        [HttpGet("web-detail/{id}")]
        [AllowAnonymous]
        public async Task<ArticleResult> GetWeb(int id)
        {
            var article = await _articleService.GetAsync(id, 0);
            return article;
        }
    }
}