using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Entity.WorkProcess;
using GxPress.Entity.WorkProcessDto;
using GxPress.Repository.Interface.WorkProcess;
using GxPress.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 审批流程
    /// </summary>
    [Route("api/admin/process")]
    [ApiController]
    [Authorize]
    public partial class AdminProcessController : ControllerBase
    {
        private readonly IProcessRepository _processRepository;
        private readonly IProcessService _processService;

        public AdminProcessController(IProcessRepository processRepository, IProcessService processService)
        {
            _processRepository = processRepository;
            _processService = processService;
        }

        /// <summary>
        /// 获取流程定义
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [AllowAnonymous]
        public async Task<ProcessDto> Get(int id)
        {
            return await _processService.GetAsync(id);
        }

        /// <summary>
        /// 添加流程定义
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Process> Add([FromBody] ProcessDto request)
        {
            return await _processService.AddAsync(request);
        }

        /// <summary>
        /// 编辑流程定义
        /// </summary>
        /// <param name="id">流程Id</param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("{id}")]
        [AllowAnonymous]
        public async Task<Process> Edit([FromRoute] int id, [FromBody] ProcessDto request)
        {
            request.Id = id;
            return await _processService.EditAsync(request);
        }

        //获取流程定义,分步获取

        ///// <summary>
        ///// 获取流程基本设置
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpGet("baseInfo")]
        //public async Task<ProcessBaseInfoResult> GetBaseInfo(int id)
        //{
        //    return await _processRepository.GetBaseInfoAsync(id);
        //}

        ///// <summary>
        ///// 获取流程表单设置
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpGet("form")]
        //public async Task<List<ProcessFormSettingResult>> GetFormSetting(int id)
        //{
        //    return await _processRepository.GetFormSettingAsync(id);
        //}

        ///// <summary>
        ///// 获取流程节点树
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpGet("nodeTree")]
        //public async Task<ProcessNodeTreeResult> GetNodeTree(int id)
        //{
        //    return await _processRepository.GetNodeTreeAsync(id);
        //}

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("")]
        [AllowAnonymous]
        public async Task<IEnumerable<Process>> GetList([FromQuery] string keyword)
        {
            return await _processRepository.GetListAsync(keyword);
        }

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

        /// <summary>
        /// 复制流程
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("actions/clone")]
        public async Task<Process> Clone([FromBody] CloneRequest request)
        {
            var process = await _processService.GetAsync(request.ProcessId);
            process.Name = request.Name;
            return await _processService.AddAsync(process);
        }
    }
}