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

namespace IRaCIS.Core.Domain.Models
{
    [Table("Trial")]
    public partial class Trial : Entity, IAuditUpdate, IAuditAdd, ISoftDelete
    {
        public Trial()
        {
            ClinicalTrialProjectDetails = new HashSet<TrialStatusDetail>();
            TrialDicList = new List<TrialDictionary>();
        }

        [JsonIgnore]
        public List<TaskMedicalReview> TaskMedicalReviewList { get; set; }
        
        [JsonIgnore]
        public List<VisitTask> VisitTaskList { get; set; } = new List<VisitTask>() { };

        [JsonIgnore]
        public List<VisitTaskReReading> VisitTaskReReadingList { get; set; } 
        [JsonIgnore]
        public List<TrialSiteSurvey> TrialSiteSurveyList { get; set; } = new List<TrialSiteSurvey>();

        [JsonIgnore]
        public List<TrialDocument> TrialDocumentList { get; set; }
        [JsonIgnore]
        public List<TrialUser> TrialUserList { get; set; } = new List<TrialUser>();
        [JsonIgnore]
        public List<ReadingQuestionCriterionTrial> ReadingQuestionCriterionTrialList { get; set; } = new List<ReadingQuestionCriterionTrial>();
        [JsonIgnore]
        public List<Subject> SubjectList { get; set; } = new List<Subject>();
        [JsonIgnore]
        public List<SubjectVisit> SubjectVisitList { get; set; } = new List<SubjectVisit>();
        [JsonIgnore]
        public List<DicomStudy> StudyList { get; set; } = new List<DicomStudy>();
        [JsonIgnore]
        public List<TrialSite> TrialSiteList { get; set; } = new List<TrialSite>();
        [JsonIgnore]
        public List<TrialSiteUser> TrialSiteUserList { get; set; } = new List<TrialSiteUser>();
        [JsonIgnore]
        public List<ReadModule> ReadModuleList { get; set; } = new List<ReadModule>();


        public Guid IndicationTypeId { get; set; } = Guid.Empty;
        public Guid? PhaseId { get; set; } = Guid.Empty;

        [JsonIgnore]
        [ForeignKey("IndicationTypeId")]
        public Dictionary IndicationType { get; set; }

        [JsonIgnore]
        [ForeignKey("PhaseId")]
        public Dictionary Phase { get; set; }

        [JsonIgnore]
        [ForeignKey("SponsorId")]
        public Sponsor Sponsor { get; set; }
        [JsonIgnore]
        [ForeignKey("CROId")]
        public CRO CRO { get; set; }
        [JsonIgnore]
        [ForeignKey("ReviewModeId")]
        public Dictionary ReviewMode { get; set; }

        [JsonIgnore]

        public List<ClinicalDataTrialSet> clinicalDataTrialSets { get; set; } = new List<ClinicalDataTrialSet> { };
        [JsonIgnore]
        public virtual ICollection<TrialStatusDetail> ClinicalTrialProjectDetails { get; set; }
        [JsonIgnore]
        public virtual ICollection<TrialDictionary> TrialDicList { get; set; }
        [JsonIgnore]
        public List<ReadingClinicalData> ReadingClinicalDataList { get; set; }

        [StringLength(100)]
        public string TrialCode { get; set; } = string.Empty;

        public int Code { get; set; }

        [StringLength(512)]
        public string Indication { get; set; } = string.Empty;

        //入组那一块的状态
        //[ConcurrencyCheck]
        public int TrialEnrollStatus { get; set; }


        //启动状态
        [StringLength(500)]
        public string TrialStatusStr { get; set; } = StaticData.TrialState.TrialInitializing;

        [JsonIgnore]
        public List<TrialStateChange> TrialStateChangeList { get; set; } = new List<TrialStateChange>();



        public Guid? CROId { get; set; } = Guid.Empty;

        public Guid? SponsorId { get; set; } = Guid.Empty;
        public Guid? ReviewModeId { get; set; } = Guid.Empty;


        [StringLength(500)]
        public string ProjectCycle { get; set; } = string.Empty;


        public int ExpectedPatients { get; set; }

        public decimal TimePointsPerPatient { get; set; }

        public int GRRReviewers { get; set; }

        public int TotalReviewers { get; set; }

        [StringLength(500)]
        public string ReviewProtocol { get; set; } = string.Empty;

        [StringLength(500)]
        public string MessageFromClient { get; set; } = string.Empty;

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


        public string ReviewProtocolName { get; set; } = string.Empty;
        public string MessageFromClientName { get; set; } = string.Empty;
        public int Expedited { get; set; }

        public DateTime CreateTime { get; set; }

        public Guid CreateUserId { get; set; }

        public DateTime UpdateTime { get; set; }

        public Guid UpdateUserId { get; set; }



        //废弃
        //public AttendedReviewerType AttendedReviewerType { get; set; } = AttendedReviewerType.CN;//0全部中国医生 1美国医生  2既有中国医生，也有美国医生

        ////废弃 
        //[JsonIgnore]
        //[ForeignKey("DeclarationTypeId")]
        //public Dictionary DeclarationType { get; set; }

        //废弃
        //public Guid DeclarationTypeId { get; set; } = Guid.Empty;



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

        [NotMapped]
        public List<DeclarationType> DeclarationTypeEnumList => DeclarationTypes.Split('|', StringSplitOptions.RemoveEmptyEntries)
           .Select(s => Enum.Parse(typeof(DeclarationType), s)).Cast<DeclarationType>().ToList();

        [NotMapped]
        public  List<AttendedReviewerType> AttendedReviewerTypeEnumList=> AttendedReviewerTypes.Split('|',StringSplitOptions.RemoveEmptyEntries)
            .Select(s => Enum.Parse(typeof(AttendedReviewerType), s)).Cast<AttendedReviewerType>().ToList();

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




        public bool VisitPlanConfirmed { get; set; }

        /// <summary>
        /// 受试者编号具体规则
        /// </summary>
        public string SubjectCodeRule { get; set; } = StaticData.International("Trial_number");
        /// <summary>
        ///  是否 提醒受试者编号规则  
        /// </summary>
        public bool IsNoticeSubjectCodeRule { get; set; } = true;

        /// <summary>
        /// 是否 有基准时间（首次给药时间）
        /// </summary>
        public bool IsHaveFirstGiveMedicineDate { get; set; } = true;

        /// <summary>
        /// 是否有  受试者年龄
        /// </summary>
        public bool IsHaveSubjectAge { get; set; } = false;


        /// <summary>
        /// 是否有 入组性确认
        /// </summary>
        public bool IsEnrollementQualificationConfirm { get; set; } = false;


        /// <summary>
        /// 出组后计划外访视名称
        /// </summary>
        public string OutEnrollmentVisitName { get; set; } = "EOT";



        /// <summary>
        /// 是否 验证拍片日期  
        /// </summary>
        public bool IsVerifyVisitImageDate { get; set; } = true;


        /// <summary>
        /// 临床信息传输 1：系统录入  2：系统录入+PDF  0：无
        /// </summary>
        public int ClinicalInformationTransmissionEnum { get; set; } = 1;

        /// <summary>
        /// 是否审核 临床信息
        /// </summary>
        public bool IsCRAAuditClinicalInformation { get; set; } = false;

        /// <summary>
        /// QC流程  0 不审，1 单审，2双审  
        /// </summary>
        public TrialQCProcess QCProcessEnum { get; set; } = TrialQCProcess.NotAudit;

        /// <summary>
        /// 影像一致性核查
        /// </summary>
        public bool IsImageConsistencyVerification { get; set; } = false;

        /// <summary>
        /// 影像导出
        /// </summary>
        public bool IsImageExport { get; set; } = false;


        public bool IsSubjectSecondCodeView { get; set; }



        //阅片方式
        public int ReadingMode { get; set; } = 1;



        public int ChangeDefalutDays { get; set; } = 5;

        /// <summary>
        /// 跨项目复制
        /// </summary>
        public bool IsImageReplicationAcrossTrial { get; set; } = false;


        public string BodyPartTypes { get; set; } = "鼻咽部|脑部|颈部|胸部|上/下腹部|盆腔|全身|其他";



        public string Modalitys { get; set; } = "CT|MRI|BoneScan|Photograph|PET|X-ray|US";

        [NotMapped]
        public List<string> ModalityList => Modalitys.Split('|', StringSplitOptions.RemoveEmptyEntries).Where(t => !string.IsNullOrEmpty(t)).ToList();


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

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



        //PD 进展是否显示   配置访视 是否显示 PD进展 (从而可以设置状态)
        public bool IsPDProgressView { get; set; }

        //研究方案号
        public string ResearchProgramNo { get; set; }

        //实验名称
        public string ExperimentName { get; set; }

        //主研单位
        public string MainResearchUnit { get; set; }

        // 负责人PI
        public string HeadPI { get; set; }

        public bool IsUrgent { get; set; }

        /// <summary>
        /// 项目类型 1 正式项目、0 非正式项目  2是培训项目
        /// </summary>time
        public TrialType TrialType { get; set; }

        //public string TempCode { get; set; }


        public int PlanSiteCount { get; set; }

        public int PlanVisitCount { get; set; }



        public DateTime? TrialFinishedTime { get; set; }

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

        //配置Suject  Edit页面 是否显示 加急 
        public bool IsSubjectExpeditedView { get; set; } = false;


        public bool IsTrialStart { get; set; } = false;
        public bool IsDeleted { get; set; }


        //QC 问题流程

        public User QCQuestionConfirmedUser { get; set; }
        public Guid? QCQuestionConfirmedUserId { get; set; }
        public DateTime? QCQuestionConfirmedTime { get; set; }

        /// <summary>
        /// 项目完成时间
        /// </summary>
        public DateTime? TrialFinishTime { get; set; }


        public int? DigitPlaces { get; set; } = 1;


        public bool IsTrialProcessConfirmed { get; set; }
        public bool IsTrialBasicLogicConfirmed { get; set; }
        public bool IsTrialUrgentConfirmed { get; set; }

        public bool IsQCQuestionConfirmed { get; set; }


        public DateTime? DeletedTime { get; set; }
        public Guid? DeleteUserId { get; set; }

        /// <summary>
        /// 同步临床数据时间
        /// </summary>
        public DateTime? SyncClinicalDataTime { get; set; }

        
        public string BlindBaseLineName { get; set; } = "Baseline";

        public string BlindFollowUpPrefix { get; set; } = "Follow-up";

        public int IndicationEnum { get; set; } = -1;

        public bool IsDeclaration { get; set; }

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

        public UserTypeEnum? EnrollConfirmDefaultUserType { get; set; }

        public UserTypeEnum? PDProgressDefaultUserType { get; set; }

        #region 邮件配置
        /// <summary>
        /// 发件箱账号
        /// </summary>
        public string EmailFromEmail { get; set; } = string.Empty;

        /// <summary>
        /// 发件人
        /// </summary>
        public string EmailFromName { get; set; } = string.Empty;

        /// <summary>
        /// 密码/授权码
        /// </summary>
        public string EmailAuthorizationCode { get; set; } = string.Empty;

        /// <summary>
        /// SMTP服务器
        /// </summary>
        public string EmailSMTPServerAddress { get; set; } = string.Empty;

        /// <summary>
        /// SMTP端口
        /// </summary>
        public int EmailSMTPServerPort { get; set; }

        /// <summary>
        /// 是否配置过邮箱
        /// </summary>
        public bool IsConfigureEmail { get; set; } = false;
        #endregion




        ///// <summary>
        ///// 图像是否有标注
        ///// </summary>
        //public bool IsImageIabeled { get; set; }



        ////阅片类型 
        //public ReadingMethod ReadingType { get; set; } = ReadingMethod.Double;


        //public bool IsGlobalReading { get; set; } = true;

        ///// <summary>
        ///// 仲裁阅片
        ///// </summary>
        //public bool? IsArbitrationReading { get; set; } = true;

        //public bool IsClinicalReading { get; set; }


        ///// <summary>
        ///// 1 Mint、2 PACS  
        ///// </summary>

        //public int ImagePlatform { get; set; } = 1;


        ///// <summary>
        ///// 仲裁规则
        ///// </summary>
        //public ArbitrationRule ArbitrationRule { get; set; } = ArbitrationRule.None;

        ////任务分配对象
        //public TaskAllocateObj TaskAllocateObjEnum { get; set; }

        ////后续访视任务自动分配
        //public bool IsFollowVisitAutoAssign { get; set; } = true;

        ////后续全局自动分配
        //public bool IsFollowGlobalVisitAutoAssign { get; set; } = true;

        //public bool IsFollowJudgeTaskAutoAssign { get; set; } = true;

        //public TaskAllocateDefaultState FollowJudgeTaskAutoAssignDefaultState { get; set; } = TaskAllocateDefaultState.Allocated;

        ////后续访视自动分配默认状态
        //public TaskAllocateDefaultState FollowVisitAutoAssignDefaultState { get; set; } = TaskAllocateDefaultState.Allocated;

        ////后续全局自动分配默认状态
        //public TaskAllocateDefaultState FollowGlobalVisitAutoAssignDefaultState { get; set; } = TaskAllocateDefaultState.Allocated;

        ////读片任务显示规则

        //public ReadingTaskViewMethod ReadingTaskViewEnum { get; set; }

        ////读片任务显示是否顺序
        //public bool IsReadingTaskViewInOrder { get; set; } = true;

        ///// <summary>
        ///// 阅片是否显示受试者信息
        ///// </summary>
        //public bool IsReadingShowSubjectInfo { get; set; } = false;

        ///// <summary>
        ///// 阅片是否显示既往结果
        ///// </summary>
        //public bool IsReadingShowPreviousResults { get; set; } = false;




        ///// <summary>
        ///// 是确认医学审核问题
        ///// </summary>

        //public bool IsConfirmMedicineQuestion { get; set; } = false;




        ///// <summary>
        ///// 阅片信息签名时间
        ///// </summary>
        //public DateTime? ReadingInfoSignTime { get; set; }

        ///// <summary>
        ///// 阅片工具
        ///// </summary>
        //public ReadingTool? ReadingTool { get; set; }

        //public Guid? ReviewTypeId { get; set; } = Guid.Empty;

        //[ForeignKey("ReviewTypeId")]
        //public Dictionary ReviewType { get; set; }


        //public Guid? QCSecondConfirmedUserId { get; set; }
        //public DateTime? QCSecondConfirmedTime { get; set; }

        //public int QCQuestionConfirmState { get; set; }
    }
}
