using Hotline.Share.Dtos.File;
using Hotline.Share.Enums.Order;
using Hotline.Share.Enums.Settings;
using Hotline.Share.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using XF.Utility.EnumExtensions;
using Hotline.Share.Enums.FlowEngine;

namespace Hotline.Share.Dtos.Order
{
	public class OrderSpecialAddDto
	{
		public string OrderId { get; set; }

		public string WorkflowId { get; set; }

		/// <summary>
		/// 下一节点code(defineStep.code)
		/// </summary>
		public string NextStepCode { get; set; } = string.Empty;
		public string NextStepName { get; set; } = string.Empty;

		/// <summary>
		/// 申请节点code(defineStep.code)
		/// </summary>
		public string? StepCode { get; set; }
		public string? StepName { get; set; }

		/// <summary>
		/// 根据办理者类型不同，此字段为不同内容
		/// <example>
		/// 部门等级/分类为：depCodes, 角色为：userIds
		/// </example>
		/// </summary>
		public List<StepAssignInfo> NextHandlers { get; set; } = new();

		/// <summary>
		/// 特提原因
		/// </summary>
		public string? Cause { get; set; }

		/// <summary>
		/// 特提理由
		/// </summary>
		public string? Reason { get; set; }

		/// <summary>
		/// 是否审批
		/// </summary>
		public bool Audit { get; set; } = false;

		/// <summary>
		/// 办理时间限制(如：24小时、7个工作日)
		/// </summary>
		public int? TimeLimit { get; set; }

		public ETimeType? TimeLimitUnit { get; set; } = ETimeType.WorkDay;

		/// <summary>
		/// 是否修改期满时间
		/// </summary>
		public bool AlterTime { get; set; }

		/// <summary>
		/// 附件列表
		/// </summary>
		public List<FileDto> Files { get; set; } = new();


		public EFlowDirection? FlowDirection { get; set; }

		/// <summary>
		/// 办理对象类型
		/// </summary>
		public EHandlerType HandlerType { get; set; }
		/// <summary>
		/// 模板配置节点业务类型
		/// </summary>
		public EBusinessType BusinessType { get; set; }

		/// <summary>
		/// 节点类型
		/// </summary>
		public EStepType? StepType { get; set; }

	}
	public class AuditOrderSpecialDto
	{
		public string Id { get; set; }

		public string WorkflowId { get; set; }

		/// <summary>
		/// 下一节点code(defineStep.code)
		/// </summary>
		public string NextStepCode { get; set; } = string.Empty;
		public string NextStepName { get; set; } = string.Empty;

		/// <summary>
		/// 根据办理者类型不同，此字段为不同内容
		/// <example>
		/// 部门等级/分类为：depCodes, 角色为：userIds
		/// </example>
		/// </summary>
		public List<StepAssignInfo> NextHandlers { get; set; } = new();

		//public string? OrgId { get; set; }

		//public string? OrgName { get; set; }

		/// <summary>
		///  审核结果  0  待审核  1 审核通过  2 审核不通过
		/// </summary>
		public int State { get; set; }

		/// <summary>
		/// 办理时间限制(如：24小时、7个工作日)
		/// </summary>
		public int? TimeLimit { get; set; }

		public ETimeType? TimeLimitUnit { get; set; } = ETimeType.WorkDay;

		/// <summary>
		/// 特提原因
		/// </summary>
		public string? Cause { get; set; }

		/// <summary>
		/// 特提意见
		/// </summary>
		public string? Opinion { get; set; }

		/// <summary>
		/// 是否修改期满时间
		/// </summary>
		public bool AlterTime { get; set; }

		/// <summary>
		/// 附件列表
		/// </summary>
		public List<FileDto> Files { get; set; } = new();


		public EFlowDirection? FlowDirection { get; set; }

		/// <summary>
		/// 办理对象类型
		/// </summary>
		public EHandlerType? HandlerType { get; set; }
		/// <summary>
		/// 模板配置节点业务类型
		/// </summary>
		public EBusinessType BusinessType { get; set; } 

	}

	public class BatchAuditOrderSpecialDto :AuditOrderSpecialDto {

		public List<string> ids { get; set; }
	}
	public class OrderSpecialDto : OrderSpecialBaseDto
	{

		public string OrderId { get; set; }

		public string WorkflowId { get; set; }

		/// <summary>
		/// 下一节点code(defineStep.code)
		/// </summary>
		public string NextStepCode { get; set; } = string.Empty;
		public string NextStepName { get; set; } = string.Empty;

		/// <summary>
		/// 申请节点code(defineStep.code)
		/// </summary>
		public string? StepCode { get; set; }
		public string? StepName { get; set; }

		public List<Kv> NextHandlers { get; set; }

		/// <summary>
		/// 特提原因
		/// </summary>
		public string? Cause { get; set; }

		/// <summary>
		/// 特提理由
		/// </summary>
		public string? Reason { get; set; }

		/// <summary>
		/// 发送短信
		/// </summary>
		public bool? AcceptSms { get; set; }

		/// <summary>
		/// 是否修改期满时间
		/// </summary>
		public bool AlterTime { get; set; }

		/// <summary>
		/// 办理时间限制(如：24小时、7个工作日)
		/// </summary>
		public int? TimeLimit { get; set; }

		public ETimeType? TimeLimitUnit { get; set; } = ETimeType.WorkDay;

		public string TimeLimitUnitText => TimeLimitUnit.GetDescription();

		/// <summary>
		/// 特提意见
		/// </summary>
		public string? Opinion { get; set; }

		/// <summary>
		///  审核结果  0  待审核  1 审核通过  2 审核不通过
		/// </summary>
		public int State { get; set; }

		public string StateText => State == 0 ? "待审核" : State == 1 ? "审核通过" : State == 2 ? "审核不通过" : "-";

		public string? OrgId { get; set; }

		public string? OrgName { get; set; }

		/// <summary>
		/// 首次归档意见
		/// </summary>
		public string? FirstFileOpinion { get; set; }

		/// <summary>
		/// 上次归档意见
		/// </summary>
		public string? LastFileOpinion { get; set; }


		/// <summary>
		/// 特提节点
		/// </summary>
		public WorkflowTraceDto Trace { get; set; }

		/// <summary>
		/// 工单
		/// </summary>
		public OrderDto Order { get; set; }

		/// <summary>
		/// 附件列表
		/// </summary>
		public List<FileDto> Files { get; set; } = new();

		public List<FileJson> FileJson { get; set; } = new();
	}
	public class OrderSpecialBaseDto
	{
		public DateTime? LastModificationTime { get; set; }

		public bool IsDeleted { get; set; }

		/// <summary>
		/// 删除时间
		/// </summary>
		public DateTime? DeletionTime { get; set; }


		/// <summary>
		/// 创建时间
		/// </summary>
		public DateTime CreationTime { get; set; }

		public string Id { get; set; }

		/// <summary>
		/// 组织Id
		/// </summary>
		public string? CreatorOrgId { get; set; }


		public string? CreatorOrgName { get; set; }

		/// <summary>
		/// 创建人
		/// </summary>
		public string? CreatorId { get; set; }

		public string? CreatorName { get; set; }
	}
	public record OrderSpecialListDto : PagedKeywordRequest
	{
		public DateTime? StartTime { get; set; }
		public DateTime? EndTime { get; set; }

		/// <summary>
		///  审核结果  0  待审核  1 审核通过  2 审核不通过
		/// </summary>
		public int? State { get; set; }

		public string? Cause { get; set; }
	}
	public class OrderSpecialHandleTime 
	{
		public string OrderId { get; set; }

		/// <summary>
		/// 流转方向
		/// </summary>
		public EFlowDirection? FlowDirection { get; set; }
	}
	public class PublishSpecialDto
	{

		/// <summary>
		/// 工单对象
		/// </summary>
		public OrderDto Order { get; set; }

		public OrderSpecialDto Special { get; set; }
	}


    public record SpecialPagedListDto : PagedRequest
    {
		/// <summary>
		/// 申请开始时间
		/// </summary>
        public DateTime? StartTime { get; set; }

		/// <summary>
		/// 申请结束时间
		/// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        ///  审核结果  0  待审核  1 审核通过  2 审核不通过
        /// </summary>
        public int? State { get; set; }

		/// <summary>
		/// 工单编号
		/// </summary>
        public string? No { get; set; }

        /// <summary>
        /// 特提申请人
        /// </summary>
        public string? CreatorName { get;set; }
    }

    public class SpecialOrderListDto
	{
		public string SpecialId { get; set; }

        public string OrderId { get; set; }

		public string WorkflowId { get; set; }

        /// <summary>
        /// 工单状态
        /// </summary>
        public EOrderStatus Status { get; set; }
        public string StatusText => Status.GetDescription();

        /// <summary>
        /// 过期状态
        /// </summary>
        public EExpiredStatus? ExpiredStatus => CalculateExpiredState();

        /// <summary>
        /// 过期状态
        /// </summary>
        public string ExpiredStatusText => ExpiredStatus?.GetDescription() ?? string.Empty;

        /// <summary>
        /// 受理类型
        /// </summary>
        public string? AcceptType { get; set; }
        public string? AcceptTypeCode { get; set; }

        /// <summary>
        /// 热点
        /// </summary>
        public string? HotspotId { get; set; }
        //public string? HotspotCode { get; set; }
        public string? HotspotName { get; set; }
        public string? HotspotSpliceName { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 即将超期时间
        /// </summary>
        public DateTime? NearlyExpiredTime { get; set; }

        /// <summary>
        /// 实际办理时间
        /// </summary>
        public DateTime? ActualHandleTime { get; set; }

        public string Title { get; set; }

		public string No { get; set; }
        public string NextStepCode { get; set; } = string.Empty;
        public string NextStepName { get; set; } = string.Empty;

        public string? StepCode { get; set; } = string.Empty;
        public string? StepName { get; set; } = string.Empty;

        /// <summary>
        /// 特提意见
        /// </summary>
        public string? Opinion { get; set; }

        /// <summary>
        ///  审核结果  0  待审核  1 审核通过  2 审核不通过
        /// </summary>
        public int State { get; set; }

		public string CreatorName { get; set; }

        public string CreatorOrgName { get; set; }

		public DateTime CreationTime { get; set; }


        public EExpiredStatus? CalculateExpiredState()
        {
            DateTime? dateTime = DateTime.Now;
            if (Status >= EOrderStatus.Filed)
            {
                dateTime = ActualHandleTime;
            }
            //ExpiredStatus
            if (ExpiredTime.HasValue)
            {
                if (dateTime < NearlyExpiredTime)
                {
                    return EExpiredStatus.Normal;
                }
                else if (dateTime > NearlyExpiredTime && dateTime < ExpiredTime)
                {
                    return EExpiredStatus.GoingToExpired;
                }
                else
                {
                    return EExpiredStatus.Expired;
                }
            }
            return null;
        }
    }

	public class OrderReTransactDto : OrderSpecialAddDto
	{
		public ESpecialType? SpecialType { get; set; }

		/// <summary>
		/// 错件类型
		/// </summary>
		public List<OrderSpecialDetailDto> ReTransactError { get; set; }
	}

	public class OrderSpecialDetailDto
	{

		public string OrgId { get; set; }

		public string OrgName { get; set; }

		public string ErrorId { get; set; }

		public string ErrorName { get; set; }

		public OrderSpecialDto OrderSpecial { get; set; }
	}

	public class OrgBatchSpecialDto
	{
		public string OrgId { get; set; }

		public DateTime StartTime { get; set; }

		public DateTime EndTime { get; set; }
	}

	public class OrderBatchSpecialDto
	{
		public List<string>  No { get; set; }

	}
}
