using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.WorkFlow;
using GxPress.Repository.Interface.WorkProcess;
using GxPress.Result.App;
using GxPress.Result.App.Flow;
using GxPress.Service.Interface;
using GxPress.Service.Interface.Doc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 通讯录用户信息
    /// </summary>
    [Route("api/admin/flow")]
    [ApiController]
    [Authorize]
    public partial class AdminFlowController : ControllerBase
    {
        private readonly ILogger<AppControllers.AppVersionController> _logger;
        private readonly ILoginContext _loginContext;
        private readonly IProcessGroupRepository _processGroupRepository;
        private readonly IProcessRepository _processRepository;
        private readonly IProcessService _processService;
        private readonly IFlowService _flowService;
        private readonly IUserRepository _userRepository;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IFlowTodoRepository _flowTodoRepository;
        private readonly IFlowRepository _flowRepository;
        private readonly IDocService docService;

        public AdminFlowController(ILogger<AppControllers.AppVersionController> logger, ILoginContext loginContext, IProcessService processService, IFlowService flowService, IUserRepository userRepository, IDepartmentRepository departmentRepository, IProcessGroupRepository processGroupRepository, IProcessRepository processRepository, IFlowTodoRepository flowTodoRepository, IFlowRepository flowRepository, IDocService docService)
        {
            _logger = logger;
            _loginContext = loginContext;
            _processService = processService;
            _flowService = flowService;
            _userRepository = userRepository;
            _departmentRepository = departmentRepository;
            _processGroupRepository = processGroupRepository;
            _processRepository = processRepository;
            _flowTodoRepository = flowTodoRepository;
            _flowRepository = flowRepository;
            this.docService = docService;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [AllowAnonymous]
        public async Task<FlowResult> Get(int id)
        {
            return await _flowService.GetFlowResult(id);
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<List<Option>> List()
        {
            var options = new List<Option>();

            var groups = await _processGroupRepository.GetListAsync();
            foreach (var processGroup in groups)
            {
                var children = new List<Option>();
                var processes = await _processRepository.GetListByGroupIdAsync(processGroup.Id);
                foreach (var process in processes)
                {
                    children.Add(new Option
                    {
                        Label = process.Name,
                        Value = process.Id
                    });
                }
                options.Add(new Option
                {
                    Label = processGroup.Name,
                    Value = 0,
                    Options = children
                });
            }

            return options;
        }

        /// <summary>
        /// 获取工作项
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ListResult> List([FromBody] ListRequest request)
        {
            return await _flowService.ListAllAsync(request.Page, request.PerPage, request.ProcessId, request.State, request.StartDate, request.EndDate);
        }
    }
}