using EntityFrameworkCore.Projectables;
using IRaCIS.Core.Domain.Share;
using System.Linq;

namespace IRaCIS.Core.Domain.Models;

[Comment("项目阅片 - 阅片任务表")]
[Table("VisitTask")]
public class VisitTask : BaseFullAuditEntity
{
    #region 导航属性
    [JsonIgnore]
    public List<TaskStudy> TaskStudyList { get; set; }

    public List<NoneDicomStudyFile> TaskNoneDicomStudyFileList { get; set; }

    [JsonIgnore]
    [ForeignKey("TrialReadingCriterionId")]
    public ReadingQuestionCriterionTrial TrialReadingCriterion { get; set; }
    [ForeignKey("DoctorUserId")]
    public User DoctorUser { get; set; }

    [ForeignKey("SouceReadModuleId")]
    public ReadModule ReadModule { get; set; }

    [ForeignKey("SourceSubjectVisitId")]
    public SubjectVisit SourceSubjectVisit { get; set; }
    [JsonIgnore]
    [ForeignKey("JudgeVisitTaskId")]
    public VisitTask JudgeVisitTask { get; set; }
    [JsonIgnore]
    public Subject Subject { get; set; }
    [JsonIgnore]
    public List<TaskMedicalReview> TaskMedicalReviewList { get; set; }

    [ForeignKey("JudgeResultTaskId")]
    public VisitTask JudgeResultTask { get; set; }
    [Comment("影像质量  等等第一层级问题答案")]
    [JsonIgnore]
    public List<ReadingTaskQuestionAnswer> ReadingTaskQuestionAnswerList { get; set; } = new List<ReadingTaskQuestionAnswer>();
    [Comment("病灶表")]
    [JsonIgnore]
    public List<ReadingTableAnswerRowInfo> LesionList { get; set; } = new List<ReadingTableAnswerRowInfo>();

    [Comment("病灶答案表")]
    [JsonIgnore]
    public List<ReadingTableQuestionAnswer> LesionAnswerList { get; set; } = new List<ReadingTableQuestionAnswer>();
    [Comment("重阅或者退回影像的记录中间表")]
    [JsonIgnore]
    public List<TaskInfluence> TaskInfluenceList { get; set; } = new List<TaskInfluence>();
    [JsonIgnore]
    [Comment("对于全局任务而言 才可以用的  关联的访视阅片结果")]
    public List<ReadingGlobalTaskInfo> GlobalVisitResultList { get; set; } = new List<ReadingGlobalTaskInfo>();
    [Comment("裁判结果图片地址")]
    [NotMapped]
    public List<string> JudgeResultImagePathList
    {
        get
        {

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

                return new List<string>();
            }

        }
    }
    [JsonIgnore]

    public Trial Trial { get; set; }
    [JsonIgnore]
    public List<UserFeedBack> UserFeedBackList { get; set; }

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

            try
            {

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

                return new List<Guid>();
            }

        }
    }
    [Comment("报告任务关系 包含冻结")]
    [NotMapped]
    public List<Guid> ReportRelatedTaskIdList
    {
        get
        {

            try
            {

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

                return new List<Guid>();
            }

        }
    }
    [Comment("既往任务Id  不包括自己集合")]
    [NotMapped]
    public List<Guid> PastResultTaskIdList
    {
        get
        {

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

                return new List<Guid>();
            }

        }
    }
    #endregion
    public string TaskName { get; set; } = string.Empty;
    public string TaskBlindName { get; set; } = string.Empty;

    [Comment("任务来源访视Id  方便回更访视读片状态")]
    public Guid? SourceSubjectVisitId { get; set; }
    public Guid? SouceReadModuleId { get; set; }

    [Comment("任务类型")]
    public ReadingCategory ReadingCategory { get; set; }

    public Guid TrialId { get; set; }
    [Comment("分配时间")]
    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; }

    [Comment("加急类型")]
    public TaskUrgentType? TaskUrgentType { get; set; }

    [Comment("任务加急类型")]
    public string TaskUrgentRemake { get; set; } = string.Empty;

    [Comment("是否和编辑加急状态")]
    public bool IsCanEditUrgentState { get; set; } = true;

    [Comment("0 代表 单重阅片 产生的任务    否则就是双重  任务 1 任务  2")]
    
    public Arm ArmEnum { get; set; }

    [Comment("分配状态")]
    public TaskAllocationState TaskAllocationState { get; set; }
    public TaskState TaskState { get; set; }
    [Comment("重阅状态")]
    public ReReadingApplyState ReReadingApplyState { get; set; }

    public Guid? DoctorUserId { get; set; }
    public Guid TrialReadingCriterionId { get; set; }
    [Comment("裁判任务的Id")]
    public Guid? JudgeVisitTaskId { get; set; }

    [Comment("任务阅片状态")]
    public ReadingTaskState ReadingTaskState { get; set; }

    [Comment("签名时间")]
    public DateTime? SignTime { get; set; }

    public DateTime? SuggesteFinishedTime { get; set; }
    [Comment("是否是重阅产生的，方便过滤数据")]
    public bool IsReReadingCreate { get; set; }

    [Comment("PM  对该任务进行了回退  影响的任务不设置")]
    public bool IsPMSetBack { get; set; }

    [Comment("裁判结果的任务ID（访视或者全局）")]
    public Guid? JudgeResultTaskId { get; set; }
    [Comment("随访任务号   取访视的号  计划外是 访视+0.1   裁判任务在访视任务上+0.002   全局任务在截止访视号上+0.03   肿瘤待定")]
    public decimal VisitTaskNum { get; set; }
    [Comment("首次阅片时间")]
    public DateTime? FirstReadingTime { get; set; }

    [Comment("全局是否有更新")]
    public bool? IsGlobalHaveUpdate { get; set; }

    [Comment("IR是否阅读临床数据")]
    public bool IsReadClinicalData { get; set; } = false;

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

    [Comment("报告任务关系 包含冻结")]
    [StringLength(2000)]
    public string ReportRelatedTaskIds { get; set; } = "[]";

    [Comment("既往任务Id  不包括自己")]
    [StringLength(2000)]
    public string PastResultTaskIds { get; set; } = "[]";
    #region 裁判任务特有

    [JsonIgnore]
    [Comment("对于裁判任务而言，触发裁判的列表")]
    public List<VisitTask> JudgeVisitList { get; set; }

    [Comment("裁判结果的备注")]
    public string JudgeResultRemark { get; set; } = string.Empty;

    [Comment("裁判结果的图片路径")]
    [StringLength(1000)]
    public string JudgeResultImagePath { get; set; } = string.Empty;

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

    [Comment("阅片结果是否和原数据有差异")]
    public bool? IsAnalysisDiffToOriginalData { get; set; }

    [Comment("组件一致性和原Arm1是否有差异")]
    public bool? IsGroupDiffArm1 { get; set; }

    [Comment("组件一致性和原Arm2是否有差异")]
    public bool? IsGroupDiffArm2 { get; set; }

    [Comment("是否是一致性分析产生")]
    public bool IsAnalysisCreate { get; set; }
    [Comment("转换之前的任务Id(转化的任务才有该值)")]
    public Guid? BeforeConvertedTaskId { get; set; }
    [Projectable]
    [JsonIgnore]
    public bool IsConvertedTask => BeforeConvertedTaskId != null || IsHistoryConvertedTask || Subject.SubjectVisitTaskList.Where(t => t.TrialReadingCriterionId == TrialReadingCriterionId
     && t.DoctorUserId == DoctorUserId && t.BeforeConvertedTaskId != null
     && t.TaskState == TaskState.Effect
     && t.VisitTaskNum < VisitTaskNum
     && t.IsSelfAnalysis == IsSelfAnalysis
     && t.ArmEnum == ArmEnum
     ).Any();
    [Comment("是否修改了整体肿瘤评估")]
    public bool IsChangeTumorEvaluate { get; set; } = false;
    [Comment("重阅重置任务的 标注是转化之前的 还是转化之后的")]
    public bool IsHistoryConvertedTask { get; set; } = false;

    [Comment("是否是自身一致性")]
    public bool? IsSelfAnalysis { get; set; }

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


    [Comment("针对产生的一致性任务而言，这个字段存储的是原始任务")]
    public Guid? ConsistentAnalysisOriginalTaskId { get; set; }

    #endregion
    [Comment("临床数据")]
    public bool IsNeedClinicalDataSign { get; set; }

    [Comment("临床数据是否签名")]
    public bool IsClinicalDataSign { get; set; }

    [Comment("前序任务需要签名 但是未签名")]
    public bool IsFrontTaskNeedSignButNotSign { get; set; }
    public int ImageStudyState { get; set; }
}
