using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Helper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Service.Common;

public class TumorCommonQustionInfo
{
    //问题标识，肿瘤评估用于区分是什么问题
    public QuestionType? QuestionType { get; set; }

    public OptionType OptionTypeEnum { get; set; }

    public Guid QuestionId { get; set; }
    public string QuestionName { get; set; }

    public string QuestionValue { get; set; }

    public string TranslateDicName { get; set; }

    public ValueUnit? Unit { get; set; }

    public ValueOfType? ValueType { get; set; }
}

public class TumorLessionInfo
{
    public Guid Id { get; set; }

    public Guid? OrganInfoId { get; set; }
    //病灶编号
    public string LessionCode { get; set; }

    public LesionType? LessionType { get; set; }

    public Guid? SplitRowId { get; set; }

    public string? DicomModality { get; set; }

    public string? NoneDicomModality { get; set; }

    //病灶答案
    public List<TumorLessionAnswerInfo> LessionAnswerList { get; set; }
}

public class TumorLessionAnswerInfo
{
    public QuestionMark? QuestionMark { get; set; }

    public OptionType OptionTypeEnum { get; set; }

    //病灶Id
    public Guid RowId { get; set; }

    //如果是4 就取CustomUnit  否则就是字典翻译
    [Comment("单位")]
    public ValueUnit? Unit { get; set; }


    public int ShowOrder { get; set; }

    public Guid TableQuesionId { get; set; }

    public string QuestionName { get; set; }

    public string QuestionValue { get; set; }

    public string TranslateDicName { get; set; }
}

public class TumorGlobalQuestionAnserInfo
{
    [Comment("原任务ID")]
    public Guid TaskId { get; set; }

    public GlobalAnswerType GlobalAnswerType { get; set; }

    [Comment("问题答案")]
    public string Answer { get; set; } = string.Empty;
}

public class TumorExportBaseModel : TU_TR_RSBaseModel
{
    public List<TumorLessionInfo> LesionList { get; set; } = new List<TumorLessionInfo>();

    public List<TumorCommonQustionInfo> QuestionAnswerList { get; set; }

    public List<TumorGlobalQuestionAnserInfo> GlobalResultList { get; set; }

    #region 后续处理额外添加字段



    public DateTime? JudgeSignTime { get; set; }

    public Guid? SourceSubjectVisitId { get; set; }

    public List<decimal> SubjectCriterionReadingPeriodVisitNumList { get; set; }

    public decimal VisitTaskNum { get; set; }

    public ReadingTaskState ReadingTaskState { get; set; }

    public ReadingCategory ReadingCategory { get; set; }

    //裁判结果选择的访视或者全局任务Id
    public Arm? JudgeArmEnum { get; set; }




    //在当前访视触发裁判，或者在截止日期小于等于当前访视的阅片期触发裁判
    [DictionaryTranslateAttribute("YesOrNoAudit")]
    public bool? IsTrigerJudge { get; set; }

    //（如果是访视点裁判，则仅在所选阅片人对应访视 显示；如果是阅片期裁判，则在所选阅片人 阅片期内的所有访视 显示此原因）
    public string JudgeNote { get; set; } = string.Empty;

    public string VisitNote { get; set; }
    #endregion

    #region 肿瘤学结果

    
    public List<OncologyExportInfo> OncologyResultList { get; set; }

    //public string OncologyUserName { get; set; }

    #endregion
}

public class OncologyExportInfo
{
    public decimal? VisitTaskNum { get; set; }
    public string VisitName { get; set; }
    public string OncologyResult { get; set; } = string.Empty;

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


public class TU_TR_RSBaseModel
{
    /// <summary>
    /// 方案编号 STUDYID
    /// </summary>
    public string ResearchProgramNo { get; set; }



    /// <summary>
    /// 域 DOMAIN TU  TR RS
    /// </summary>
    public string Domain { get; set; }


    /// <summary>
    /// 取值类型 TUSPID  TRSPID RSSPID
    /// </summary>
    public string ValueType { get; set; }


    /// <summary>
    /// 受试者编号 USUBJID 实际展示TrialSiteSubjectCode
    /// </summary>

    public string SubjectCode { get; set; }


    /// <summary>
    /// 供应商 TUNAM (Extensive Imaging)
    /// </summary>
    public string Vendor { get; set; } = "Extensive Imaging";


    /// <summary>
    /// 阅片人 TUEVAL TREVAL  RSEVAL
    /// </summary>
    public string UserName { get; set; }

    /// <summary>
    /// 阅片人标识 TUEVALID  TREVALID    RSEVALID
    /// </summary>
    public Arm ArmEnum { get; set; }

    /// <summary>
    /// 访视编号 VISITNUM
    /// </summary>
    public decimal? VisitNum { get; set; }

    /// <summary>
    /// 访视名称 VISIT
    /// </summary>
    public string? VisitName { get; set; }

    /// <summary>
    /// 拍片日期 TUDTC  TRDTC  RSDTC
    /// </summary>
    public DateTime? LatestScanDate { get; set; }

    public DateTime? EarliestScanDate { get; set; }

    /// <summary>
    /// eCRF标识 TUREFID  TRREFID  RSREFID
    /// </summary>
    public Guid VisitTaskId { get; set; }


    #region 移动位置

    /// <summary>
    /// RSCAT  阅片标准
    /// </summary>
    public string CriterionName { get; set; }

    /// <summary>
    /// RSACPTFL  裁定标记 TUACPTFL
    /// </summary>    
    //裁判选择标记
    //根据裁判的任务结果 设置访视任务的这个字段  该字段表示  裁判认同该任务的结果
    public bool? IsJudgeSelect { get; set; }

    #endregion

    public DateTime? SignTime { get; set; }

    public string TaskName { get; set; }

    #region 额外翻译字段



    public string IsJudgeSelectStr => IsJudgeSelect == true ? "Y" : "";

    public bool IsTargetPD { get; set; } = false;

    public bool IsOverallResponsePD { get; set; } = false;

    //TR表  靶病灶PD了，访视层级的都是最早拍片日期  RS表 整体肿瘤评估PD了，那么疗效评估的拍片日期都给最早的
    public bool IsPD => Domain == "TR" ? IsTargetPD : IsOverallResponsePD;

    public string ArmEnumStr { get; set; }
    public string LatestScanDateStr
    {
        get
        {
            var date = IsPD ? EarliestScanDate : LatestScanDate;
            return date?.ToString("yyyy-MM-dd") ?? "";
        }
    }
    public string TrialSiteSubjectCode => ResearchProgramNo + SubjectCode;
    #endregion
}


public class TU_Export : TU_TR_RSBaseModel
{

    ///// <summary>
    ///// 取值类型 TUSPID
    ///// </summary>
    //public string TUValueType { get; set; }


    /// <summary>
    /// 序号 TUSEQ   (同一个访视，所有阅片人选择病灶给个顺序号)
    /// </summary>
    public int No { get; set; }



    /// <summary>
    /// 链接ID TULNKID (阅片人角色_病灶编号)不同访视可以重复
    /// </summary>
    public string ARM_TumorNo { get; set; }

    /// <summary>
    /// 肿瘤识别简称 TUTESTCD
    /// </summary>
    public string TumorIdentificationSimple { get; set; }

    /// <summary>
    /// 肿瘤识别全称 TUTEST
    /// </summary>
    public string TumorIdentificationFullName { get; set; }

    /// <summary>
    /// 肿瘤鉴定结果 TUORRES
    /// </summary>
    public string TumorIdentificationResult { get; set; }

    /// <summary>
    /// 肿瘤识别结果类型 TUSTRESC 
    /// </summary>
    public string TumorIdentificationResultType { get; set; }


    /// <summary>
    /// 部位 TULOC (对应病灶表的部位，需要国际化)
    /// </summary>
    public string BodyPart { get; set; }

    /// <summary>
    /// 鉴定方法 TUMETHOD (Modality?)
    /// </summary>
    public string IdentificationMethod { get; set; }



    ///// <summary>
    ///// 裁定标记 TUACPTFL
    ///// </summary>
    //public bool? IsJudgeSelect { get; set; }



    /// <summary>
    /// 部位描述 LOCTEXT
    /// </summary>
    public string BodyPartDes { get; set; }


}


public class TR_Export : TU_TR_RSBaseModel
{

    ///// <summary>
    ///// 取值类型 TRSPID
    ///// </summary>
    //public string TRValueType { get; set; }


    /// <summary>
    /// 每个subject 按照顺序编号  TRSEQ
    /// </summary>
    public int TRSEQ { get; set; }


    /// <summary>
    ///TRGRPID  组ID  对应TU表肿瘤鉴定结果 TumorIdentificationResult
    /// </summary>
    public string TRGRPID { get; set; }

    /// <summary>
    /// TRLNKID  链接ID  对应TU表的链接ID  TumorNo(阅片人角色_病灶编号)
    /// </summary>
    public string ARM_TumorNo { get; set; }

    /// <summary>
    ///TRLNKGRP  链接组  ARM-任务名（访视名） 对应RS的链接组
    /// </summary>
    public string ARM_VisitName { get; set; }


    /// <summary>
    /// 肿瘤评估简称 TRTESTCD
    /// </summary>
    public string TumorAssessmentSimpleName { get; set; }

    /// <summary>
    /// 肿瘤评估全称 TRTEST
    /// </summary>
    public string TumorAssessmentFullName { get; set; }


    /// <summary>
    /// 原始测量 TRORRES
    /// </summary>
    public string OriginalMeasurements { get; set; }

    /// <summary>
    /// 原始单位 TRORRESU
    /// </summary>
    public string OriginalUnit { get; set; }

    /// <summary>
    /// 标准结果（字符） TRSTRESC
    /// </summary>
    public string StandardResult_Character => OriginalMeasurements;

    /// <summary>
    /// 标准结果（数值） TRORRESU
    /// </summary>
    public string StandardResult_Numeric => double.TryParse(OriginalMeasurements, out _) ||
    (OriginalMeasurements?.EndsWith("%") == true &&
     double.TryParse(OriginalMeasurements.TrimEnd('%'), out _))
        ? OriginalMeasurements
        : "";

    /// <summary>
    /// 标准单位 TRSTRESU
    /// </summary>
    public string StandardUnit => OriginalUnit;


    /// <summary>
    /// 完成状态 TRSTAT
    /// </summary>
    public string CompletionStatus { get; set; }


    /// <summary>
    /// 完成状态 TRMETHOD
    /// </summary>
    public string IdentificationMethod { get; set; }


    /// <summary>
    /// 无法测量原因 TRREASND
    /// </summary>
    public string NotMeasuredReason { get; set; }



}


public class RS_Export : TU_TR_RSBaseModel
{
    /// <summary>
    /// RSSEQ  按照subject 的数据顺序编号
    /// </summary>
    public int RSSEQ { get; set; }

    /// <summary>
    /// RSLNKGRP 链接组  ARM_任务名(访视名)
    /// </summary>
    public string ARM_VisitName { get; set; }

    /// <summary>
    /// RSTESTCD 疗效评估简称
    /// </summary>
    public string EfficacyEvaluationSimpleName { get; set; }

    /// <summary>
    ///  RSTEST   疗效评估全称
    /// </summary>
    public string EfficacyEvaluationName { get; set; }

    /// <summary>
    /// RSORRES  响应评估原始结果
    /// </summary>
    public string RespondEfficacyAssessment { get; set; }
    /// <summary>
    /// RSSTRESC   标准疗效评估
    /// </summary>
    public string StandardEfficacyAssessment => RespondEfficacyAssessment;

    /// <summary>
    /// RSSTAT 完成状态
    /// </summary>
    public string CompletionStatus { get; set; }
    /// <summary>
    /// RSREASND  无法评估原因
    /// </summary>
    public string NotAssessmentReason { get; set; }



    ///// <summary>
    ///// 裁定标记 RSACPTFL
    ///// </summary>
    //public bool? IsJudgeSelect { get; set; }

    /// <summary>
    /// REASASM  评估原因
    /// </summary>
    public string AssessmentReason { get; set; }
    /// <summary>
    /// REASOVR  重新评估原因
    /// </summary>
    public string ReAssessmentReason { get; set; }

    /// <summary>
    /// REASUPD 更新评估原因
    /// </summary>
    public string UpdateAssessmentReason { get; set; }


    [JsonIgnore]
    public bool? IsOveralResponse { get; set; }

}


public class CO_Export : TU_TR_RSBaseModel
{
    /// <summary>
    /// 关联域  RS：(访视点备注）  空：裁判选择原因
    /// </summary>
    public string RDOMAIN { get; set; }

    /// <summary>
    /// COSEQ  序号
    /// </summary>
    public int COSEQ { get; set; }

    /// <summary>
    /// IDVAR  标识变量    RSSEQ    空：裁判选择原因
    /// </summary>
    public string IdentificationVariable { get; set; }

    /// <summary>
    /// 标识  IDVARVAL  RSSEQ具体的值  空：裁判选择原因
    /// </summary>
    public string Identification { get; set; }

    /// <summary>
    /// COREF 备注引用
    /// </summary>
    public string RemarksQuote { get; set; }

    /// <summary>
    /// 备注 COVAL
    /// </summary>
    public string Remarks { get; set; }

    /// <summary>
    /// 裁决日期 CODTC
    /// </summary>
    public string CODTC { get; set; }



}
