using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Enums.Order;
using Hotline.Share.Requests;
using XF.Utility.EnumExtensions;

namespace Hotline.Share.Dtos.IndustryClassification
{
    /// <summary>
    /// 新增行业
    /// </summary>
    public class IndustryAddDto
    {
        /// <summary>
        /// 行业名称
        /// </summary>
        public string IndustryName { get; set; }

        /// <summary>
        /// 关联部门
        /// </summary>
        public List<string> OrgsIds { get; set; }
    }

    /// <summary>
    /// 编辑行业
    /// </summary>
    public class IndustryUpdateDto : IndustryAddDto
    {
        /// <summary>
        /// 行业Id
        /// </summary>
        public string Id { get; set; }

    }

    /// <summary>
    /// 行业关联部门
    /// </summary>
    public class IndustryAssociationsDto
    {
        public string Id { get; set; }

        /// <summary>
        /// 行业ID
        /// </summary>
        public string IndustrialId { get; set; }

        /// <summary>
        /// 行业名称
        /// </summary>
        public string IndustrialName { get; set; }

        /// <summary>
        /// 部门Id
        /// </summary>
        public string SystemOrganizeId { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string SystemOrganizeName { get; set; }
    }

    /// <summary>
    /// 行业列表
    /// </summary>
    public class IndustrialManagementDto
    {
        /// <summary>
        /// 行业ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 行业名称
        /// </summary>
        public string IndustrialName { get; set; }

        /// <summary>
        /// 部门数量
        /// </summary>
        public int OrgCount { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
    }

    /// <summary>
    /// 行业工单返回参数
    /// </summary>
    public class IndustrialManagementOrderDto
    {
        /// <summary>
        /// 工单Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 工单状态
        /// </summary>
        public EOrderStatus Status { get; set; }

        public string StatusText => Status.GetDescription();

        /// <summary>
        /// 行业ID
        /// </summary>
        public string IndustrialId { get; set; }

        /// <summary>
        /// 行业名称
        /// </summary>
        public string IndustrialName { get; set; }

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

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

        /// <summary>
        /// 工单标题
        /// </summary>
        public string Title { get; set; }

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

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

        public string AcceptTypeCode { get; set; }

        /// <summary>
        /// 热点Id
        /// </summary>
        public string? HotspotId { get; set; }

        /// <summary>
        /// 热点名称
        /// </summary>
        public string? HotspotName { get; set; }

        /// <summary>
        /// 热点全称
        /// </summary>
        public string? HotspotSpliceName { get; set; }

        /// <summary>
        /// 外部数据（为前端提供级联功能）
        /// </summary>
        public string? HotspotExternal { get; set; }

        /// <summary>
        /// 受理时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 工单开始时间(受理/接办时间=流程开启时间)
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 一级部门code
        /// </summary>
        public string? OrgLevelOneCode { get; set; }

        /// <summary>
        /// 一级部门名称
        /// </summary>
        public string? OrgLevelOneName { get; set; }

        /// <summary>
        /// 实际办理部门名称
        /// </summary>
        public string? ActualHandleOrgName { get; set; }

        /// <summary>
        /// 实际办理部门编码
        /// </summary>
        public string? ActualHandleOrgCode { get; set; }

        /// <summary>
        /// 会签类型
        /// </summary>
        public ECounterSignType? CounterSignType { get; set; }

        public string? CounterSignTypeText => CounterSignType?.GetDescription() ?? "未会签";

        /// <summary>
        /// 归档时间（暂为流程结束时间，因流程结束自动归档）
        /// </summary>
        public DateTime? FiledTime { get; set; }

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

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

        /// <summary>
        /// 即将超期第一级
        /// </summary>
        public DateTime? NearlyExpiredTimeOne { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public EExpiredStatus? CalculateExpiredState()
        {
            DateTime? dateTime = DateTime.Now;
            if (Status >= EOrderStatus.Filed)
            {
                dateTime = FiledTime;
            }

            if (ExpiredTime.HasValue)
            {
                if (NearlyExpiredTimeOne.HasValue && dateTime < NearlyExpiredTimeOne)
                {
                    return EExpiredStatus.Normal;
                }
                else if (!NearlyExpiredTimeOne.HasValue && dateTime < NearlyExpiredTime)
                {
                    return EExpiredStatus.Normal;
                }
                else if (NearlyExpiredTimeOne.HasValue && dateTime > NearlyExpiredTimeOne && dateTime < NearlyExpiredTime)
                {
                    return EExpiredStatus.GoingToExpiredOne;
                }
                else if (dateTime > NearlyExpiredTime && dateTime < ExpiredTime)
                {
                    return EExpiredStatus.GoingToExpired;
                }
                else
                {
                    return EExpiredStatus.Expired;
                }
            }

            return null;
        }
    }

    /// <summary>
    /// 行业工单查询条件
    /// </summary>
    public record IndustrialManagementRequestDto : PagedKeywordRequest
    {
        /// <summary>
        /// 行业名称
        /// </summary>
        public string IndustrialName { get; set; }

        /// <summary>
        /// 工单标题
        /// </summary>
        public string Title { get; set; }

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

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

        /// <summary>
        /// 受理时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 热点全称
        /// </summary>
        public string? HotspotSpliceName { get; set; }

        /// <summary>
        /// 一级部门名称
        /// </summary>
        public string? OrgLevelOneName { get; set; }

        /// <summary>
        /// 实际办理部门名称
        /// </summary>
        public string? ActualHandleOrgName { get; set; }
    }

    
}
