using System;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.Tools;
using GxPress.EnumConst;
using GxPress.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    public partial class AppFlowController
    {

        /// <summary>
        /// 动作 - 审批
        /// </summary>
        /// <param name="request">审批意见</param>
        /// <returns></returns>
        [HttpPost("action/check")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsCheck([FromBody] CheckRequest request)
        {
            if (!string.IsNullOrEmpty(request.Message) && request.Message.Length > 500)
            {
                throw new Exception("审批意见不能操作500字，请重新输入");
            }
            await _flowService.CheckAsync(request.TodoId, request.IsChecked, request.Message);

            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 下载
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/download")]
        [AllowAnonymous]
        public async Task<DownloadResult> ActionsDownload([FromBody] ActionRequest request)
        {
            // var url = await _flowService.DownloadAsync(request.FlowId);
            // url = Request.Scheme + "://" + Request.Host.Host +
            //           (Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +
            //           "/" + url;
            var url = await docService.MakeDoc(request.FlowId, _loginContext.AccountId);
            return new DownloadResult
            {
                DownloadUrl = StringUtils.AddDomain(url),
                PreviewUrl = StringUtils.GetPreviewDocUrl(url)
            };
        }

        /// <summary>
        /// 动作 - 催办
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/remind")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsRemind([FromBody] ActionRequest request)
        {
            return new DefaultResult
            {
                Value = await _flowService.RemindAsync(request.FlowId, _loginContext.AccountId)
            };
        }

        /// <summary>
        /// 动作 - 上会
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/meeting")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsMeeting([FromBody] ActionRequest request)
        {
            //await _flowRepository.UpdateStateAsync(request.FlowId, nameof(FlowState.Meeting));
            //修改当前用户的审核状态
            await _flowTodoRepository.UpdateAsync(Q.Set(nameof(Entity.WorkFlow.FlowTodo.IsDone), true).Set(nameof(Entity.WorkFlow.FlowTodo.IsChecked), false).Set(nameof(Entity.WorkFlow.FlowTodo.DoneType), 1).Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), request.FlowId).Where(nameof(Entity.WorkFlow.FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck)).Where(nameof(Entity.WorkFlow.FlowTodo.UserId), _loginContext.AccountId));
            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 撤销上会
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/cancelMeeting")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsCancelMeeting([FromBody] ActionRequest request)
        {
            //await _flowRepository.UpdateStateAsync(request.FlowId, nameof(FlowState.Checking));
            //修改当前操作人为带审核
            await _flowTodoRepository.UpdateAsync(Q.Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), request.FlowId).Where(nameof(Entity.WorkFlow.FlowTodo.UserId), _loginContext.AccountId).Set(nameof(Entity.WorkFlow.FlowTodo.IsDone), false).Set(nameof(Entity.WorkFlow.FlowTodo.IsChecked), false).Set(nameof(Entity.WorkFlow.FlowTodo.DoneType), 0).Set(nameof(Entity.WorkFlow.FlowTodo.IsOperate), true));
            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 转审
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/transferCheck")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsTransferCheck([FromBody] ActionTransferCheckRequest request)
        {
            await _flowService.TransferCheckAsync(request.TodoId, request.TransferUserIds, request.Message, request.FileIds);
            // //修改当前用户的审核状态
            // await _flowTodoRepository.UpdateAsync(Q.Set(nameof(Entity.WorkFlow.FlowTodo.IsDone), true).Where(nameof(Entity.WorkFlow.FlowTodo.Id), request.TodoId).Where(nameof(Entity.WorkFlow.FlowTodo.UserId), _loginContext.AccountId));
            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 重新提交
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/reAdd")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsReAddCheck([FromBody] ActionRequest request)
        {
            await _flowService.ReAddAsync(request.FlowId);

            return new DefaultResult
            {
                Value = true
            };
        }
        /// <summary>
        /// 上会待定
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("action/wait")]
        public async Task<DefaultResult> ActionsWait(ActionRequest request)
        {
            return new DefaultResult
            {
                Value = await _flowService.ActionsWait(request.FlowId, _loginContext.AccountId)
            };
        }
    }
}
