using Hotline.FlowEngine;
using Hotline.FlowEngine.Definitions;
using Hotline.FlowEngine.WorkflowModules;
using Hotline.FlowEngine.Workflows;
using Hotline.Orders;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.File;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Definition;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Order.Handle;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Enums.FlowEngine;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using XF.Domain.Authentications;

namespace Hotline.Application.FlowEngine
{
    public interface IWorkflowApplication
    {
        // /// <summary>
        // /// 开始流程
        // /// </summary>
        // Task<string> StartWorkflowAsync(StartWorkflowDto dto, string externalId, DateTime? expiredTime = null,
        //     CancellationToken cancellationToken = default);

        /// <summary>
        /// 开始流程并停留在开始节点（开始节点作为待办节点）
        /// </summary>
        Task<string> StartWorkflowToStartStepAsync(StartWorkflowDto dto, string externalId, DateTime? expiredTime = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 查询下一节点办理对象类型（user or org）及实际办理对象
        /// </summary>
        Task<FlowAssignInfo> GetNextStepFlowAssignInfoAsync(Workflow workflow, WorkflowStep currentStep,
            BasicWorkflowDto dto, StepDefine nextStepDefine, bool isNextDynamic, CancellationToken cancellationToken);

        ///// <summary>
        ///// 流转至下一节点（节点办理）
        ///// </summary>
        //Task<Workflow> NextAsync(NextWorkflowDto dto, DateTime? expiredTime = null, CancellationToken cancellationToken = default);

        ///// <summary>
        ///// 退回（返回前一节点）
        ///// </summary>
        //Task<(Workflow workflow, WorkflowStep currentStep, StepDefine prevDefine,
        //        WorkflowStep prevStep, WorkflowStep newStep, EFlowDirection flowDirection)>
        //    PreviousAsync(PreviousWorkflowDto dto, 
        //        EHandleMode handleMode = EHandleMode.Previous,
        //        Action<Workflow, WorkflowStep, StepDefine, WorkflowStep, WorkflowStep>? newStepConfig = null,
        //        CancellationToken cancellationToken = default);

        /// <summary>
        /// 工单退回（返回前一节点）
        /// </summary>
        Task<(Workflow workflow, WorkflowStep currentStep, StepDefine prevDefine,
                WorkflowStep prevStep, WorkflowStep newStep, EFlowDirection flowDirection)>
            PreviousAsync(PreviousWorkflowDto dto, string applicantId, string applicantOrgId, string[] applicantRoleIds,
                EHandleMode handleMode = EHandleMode.Previous,
                Action<Workflow, WorkflowStep, StepDefine, WorkflowStep, WorkflowStep>? newStepConfig = null,
                CancellationToken cancellationToken = default);

        ///// <summary>
        ///// 撤回至任意节点
        ///// </summary>
        //Task RecallAsync(RecallDto dto, FlowAssignInfo flowAssignInfo, DateTime? expiredTime, bool isOrderFiled, EWorkflowTraceType traceType, CancellationToken cancellationToken);

        /// <summary>
        /// 办理至结束节点(无视流程模板配置,操作人需是当前节点办理对象)
        /// </summary>
        Task HandleToEndAsync(string workflowId, string opinion, List<FileDto> files, DateTime? expiredTime = null,
            EReviewResult reviewResult = EReviewResult.Unknown, CancellationToken cancellationToken = default);

        ////////

        /// <summary>
        /// 查询开始流程的下一步待选节点
        /// </summary>
        Task<NextStepsDto<NextStepOption>> GetStartStepsAsync(string moduleCode, CancellationToken cancellationToken);

        /// <summary>
        /// 查询办理流程的下一步待选节点
        /// </summary>
        Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(string workflowId, CancellationToken cancellationToken);

        /// <summary>
        /// 查询指定节点的下一步待选节点
        /// </summary>
        Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(string workflowId, string stepId, CancellationToken cancellationToken);
        Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(Workflow workflow, CancellationToken cancellationToken);

        /// <summary>
        /// 跨级指派查询下一步可选节点及办理对象参数
        /// </summary>
        Task<NextStepsDto<NextStepOption>> GetCrossLevelStepsAsync(GetCrossLevelStepsDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 查询撤回可选节点
        /// </summary>
        Task<NextStepsDto<RecallStepOption>> GetRecallStepsAsync(string workflowId, CancellationToken cancellationToken);

        /// <summary>
        /// 否决
        /// </summary>
        Task RejectAsync(RejectDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 获取流程模板信息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<WorkflowModule> GetWorkflowModuleAsync(string code, CancellationToken cancellationToken);

        /// <summary>
        /// 检查退回节点信息
        /// </summary>
        Task<(WorkflowStep currentStep, WorkflowStep prevStep,List<WorkflowStep> steps, bool isOrgToCenter, bool isSecondToFirstOrgLevel)>
            GetPreviousInformationAsync(string workflowId, string operatorId, string operatorOrgId, string[] roles, string? assignStepId,
				CancellationToken cancellationToken);

        /// <summary>
        /// 更新省平台办理结果节点附件
        /// </summary>
        Task UpdateProvinceHandleResultFilesAsync(string workflowId, List<FileDto> files, CancellationToken cancellationToken);

		/// <summary>
		/// 查询会签信息
		/// </summary>
		/// <param name="dto"></param>
		/// <param name="_sessionContext"></param>
		/// <returns></returns>
		ISugarQueryable<WorkflowCountersign, Workflow, Order> QueryOrderCountersigns(QueryOrderCountersignDto dto,
	        ISessionContext _sessionContext);

    }
}