using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface.WorkProcess;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using GxPress.Entity.WorkProcess;
using GxPress.Result.App.Process;
using GxPress.Service.Interface;
using System.Linq;
using GxPress.Common.Tools;

namespace GxPress.Api.AppControllers
{
    [Route("api/app/process")]
    [ApiController]
    [Authorize]
    public class AppProcessController : ControllerBase
    {
        private readonly IProcessGroupRepository _processGroupRepository;
        private readonly IProcessRepository _processRepository;
        private readonly IProcessService _processService;
        private readonly ILoginContext _loginContext;

        public AppProcessController(IProcessGroupRepository processGroupRepository, IProcessRepository processRepository, IProcessService processService, ILoginContext loginContext)
        {
            _processGroupRepository = processGroupRepository;
            _processRepository = processRepository;
            _processService = processService;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 获取工作流程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IEnumerable<Process>> GetProcessList()
        {
            return await _processRepository.GetListByDepartmentIdAsync(_loginContext.DepartmentId);
        }

        /// <summary>
        /// 获取工作流程
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<GetProcessResult> GetProcess(int id)
        {
            var processDto = await _processService.GetAsync(id);
            var result = new GetProcessResult
            {
                Id = processDto.Id,
                Name = processDto.Name,
                IconUrl =StringUtils.AddDomain(processDto.IconUrl),
                Description = processDto.Description,
                FormFields = processDto.FormFields,
                AllowApproverCheck = processDto.AllowApproverCheck,
                AllowCarbonCopy = processDto.AllowCarbonCopy
            };
            result.ApproverCheckUserIds = new List<int>();
            //获取流程接点
            foreach (var item in processDto.ProcessNodes)
                //添加系统审核人
                result.ApproverCheckUserIds.AddRange(item.ApproverChecks.Select(n => n.UserId));
            result.ApproverCheckUserIds.Add(_loginContext.AccountId);
            return result;
        }

        /// <summary>
        /// 获取流程组与流程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("groups")]
        [AllowAnonymous]
        public async Task<IEnumerable<ProcessGroup>> GetProcessGroupList()
        {
            var groups = await _processGroupRepository.GetListAsync();
            foreach (var processGroup in groups)
            {
                var processes = await _processRepository.GetListByGroupIdAsync(processGroup.Id);
                processGroup.Processes = processes;
            }

            return groups;
        }
    }
}