using IRaCIS.Core.Domain.Share;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Service.Reading.Dto
{
    public class VisitTaskDto
    {
        public Guid Id { get; set; }
        public string TaskName { get; set; } = string.Empty;
        public string TaskBlindName { get; set; } = string.Empty;


        //任务来源访视Id  方便回更访视读片状态
        public Guid? SourceSubjectVisitId { get; set; }
        public Guid? SouceReadModuleId { get; set; }


        /// <summary>
        /// 任务类型
        /// </summary>
        public ReadingCategory ReadingCategory { get; set; }



        public Guid CreateUserId { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public Guid UpdateUserId { get; set; }
        public Guid TrialId { get; set; }


        /// <summary>
        /// 分配时间
        /// </summary>
        public DateTime? AllocateTime { get; set; }

        public Guid SubjectId { get; set; }


        public int Code { get; set; }

        public string TaskCode { get; set; } = string.Empty;

        public bool IsUrgent { get; set; }

        /// <summary>
        /// 加急类型
        /// </summary>
        public TaskUrgentType? TaskUrgentType { get; set; }

        /// <summary>
        /// 任务加急类型
        /// </summary>
        public string TaskUrgentRemake { get; set; } = string.Empty;

        /// <summary>
        /// 是否和编辑加急状态
        /// </summary>
        public bool IsCanEditUrgentState { get; set; } = true;

        /// <summary>
        /// 0 代表 单重阅片 产生的任务    否则就是双重  任务 1 任务  2 
        /// </summary>
        [Required]
        public Arm ArmEnum { get; set; }

        /// <summary>
        /// 分配状态
        /// </summary>
        public TaskAllocationState TaskAllocationState { get; set; }


        public TaskState TaskState { get; set; }


        /// <summary>
        /// 重阅状态
        /// </summary>
        public ReReadingApplyState ReReadingApplyState { get; set; }

        public Guid? DoctorUserId { get; set; }


        public Guid TrialReadingCriterionId { get; set; }

        //裁判任务的Id
        public Guid? JudgeVisitTaskId { get; set; }

        //任务阅片状态
        public ReadingTaskState ReadingTaskState { get; set; }

        //签名时间
        public DateTime? SignTime { get; set; }

        public DateTime? SuggesteFinishedTime { get; set; }


        /// <summary>
        /// 是否是重阅产生的，方便过滤数据
        /// </summary>
        public bool IsReReadingCreate { get; set; }

        /// <summary>
        /// PM  对该任务进行了回退  影响的任务不设置
        /// </summary>
        public bool IsPMSetBack { get; set; }


        /// <summary> 裁判结果的任务ID </summary>
        public Guid? JudgeResultTaskId { get; set; }

        //随访任务号   取访视的号  计划外是 访视+0.1   裁判任务在访视任务上+0.002   全局任务在截止访视号上+0.03   肿瘤待定   
        public decimal VisitTaskNum { get; set; }



        /// <summary>
        /// 首次阅片时间
        /// </summary>
        public DateTime? FirstReadingTime { get; set; }

        /// <summary>
        /// 全局是否有更新
        /// </summary>
        public bool? IsGlobalHaveUpdate { get; set; }

        /// <summary>
        /// IR是否阅读临床数据
        /// </summary>
        public bool IsReadClinicalData { get; set; } = false;

        /// <summary>
        /// 关联的访视任务ID (当前任务是访视任务的话会有自己)
        /// </summary>
        public string RelatedVisitTaskIds { get; set; } = "[]";

        /// <summary>
        /// 关联的访视任务ID (当前任务是访视任务的话会有自己)集合
        /// </summary>
        [NotMapped]
        public List<Guid> RelatedVisitTaskIdList
        {
            get
            {

                try
                {

                    return JsonConvert.DeserializeObject<List<Guid>>(this.RelatedVisitTaskIds);
                }
                catch (Exception)
                {

                    return new List<Guid>();
                }

            }
        }

        /// <summary>
        /// 既往任务Id  不包括自己
        /// </summary>

        public string PastResultTaskIds { get; set; } = "[]";



        /// <summary>
        /// 既往任务Id  不包括自己集合
        /// </summary>
        [NotMapped]
        public List<Guid> PastResultTaskIdList
        {
            get
            {

                try
                {
                    return JsonConvert.DeserializeObject<List<Guid>>(this.PastResultTaskIds);
                }
                catch (Exception)
                {

                    return new List<Guid>();
                }

            }
        }

        #region 裁判任务特有

        /// <summary>
        /// 裁判结果的备注
        /// </summary>
        public string JudgeResultRemark { get; set; } = string.Empty;
        /// <summary>
        /// 裁判结果的图片路径
        /// </summary>
        public string JudgeResultImagePath { get; set; } = string.Empty;

        #endregion


        #region  一致性分析的任务特有数据

        /// <summary>
        /// 阅片结果是否和原数据有差异
        /// </summary>
        public bool? IsAnalysisDiffToOriginalData { get; set; }

        /// <summary>
        /// 组件一致性和原Arm1是否有差异
        /// </summary>
        public bool? IsGroupDiffArm1 { get; set; }

        /// <summary>
        /// 组件一致性和原Arm2是否有差异
        /// </summary>
        public bool? IsGroupDiffArm2 { get; set; }

        /// <summary>
        /// 是否是一致性分析产生
        /// </summary>
        public bool IsAnalysisCreate { get; set; }


      

        /// <summary>
        /// 转换之前的任务Id(转化的任务才有该值)
        /// </summary>
        public Guid? BeforeConvertedTaskId { get; set; }


        // [Projectable]

         public bool IsConvertedTask { get; set; }

        //[NotMapped]
        //public List<CrterionDictionaryGroup> CrterionDictionaryGroup
        //{
        //    get
        //    {

        //        try
        //        {
        //            if (this.IsConvertedTask)
        //            {
        //                return new List<CrterionDictionaryGroup>() { Share.CrterionDictionaryGroup.General, Share.CrterionDictionaryGroup.Converted };
        //            }
        //            else
        //            {
        //                return new List<CrterionDictionaryGroup>() { Share.CrterionDictionaryGroup.General, Share.CrterionDictionaryGroup.BeforeConvert };
        //            }
        //        }
        //        catch (Exception)
        //        {

        //            return new List<CrterionDictionaryGroup>();
        //        }

        //    }
        //  }



        public bool? IsSelfAnalysis { get; set; }

        public string BlindSubjectCode { get; set; } = string.Empty;
        public string BlindTrialSiteCode { get; set; } = string.Empty;

        //一致性分析规则Id  用于最后统计
        //public Guid? TaskConsistentRuleId { get; set; }

        /// <summary>
        /// 针对产生的一致性任务而言，这个字段存储的是原始任务
        /// </summary>
        public Guid? ConsistentAnalysisOriginalTaskId { get; set; }

        #endregion


        //临床数据

        public bool IsNeedClinicalDataSign { get; set; }

        public bool IsClinicalDataSign { get; set; }

        //前序任务需要签名 但是未签名
        public bool IsFrontTaskNeedSignButNotSign { get; set; }

     
        /// <summary>
        /// 裁判结果图片地址
        /// </summary>
        [NotMapped]
        public List<string> JudgeResultImagePathList
        {
            get
            {

                try
                {
                    return this.JudgeResultImagePath.Trim().Split(',').ToList();
                }
                catch (Exception)
                {

                    return new List<string>();
                }

            }
        }
    }
}
