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

namespace IRaCIS.Core.Application.Service.Reading.Dto
{



    #region 获取计算问题

    public class GetTrialClinicalQuestionCalculateRelationInDto
    {
        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }
    }


    public class GetTrialClinicalQuestionCalculateRelationOutDto
    {
        public Guid QuestionId { get; set; }

        public int? DigitPlaces { get; set; }

        /// <summary>
        /// 问题名称
        /// </summary>
        public string QuestionName { get; set; }

        /// <summary>
        /// 自定义计算标记
        /// </summary>
        public ClinicalCalculateMark? CustomCalculateMark { get; set; }

        /// <summary>
        /// 数值类型
        /// </summary>
        public ValueOfType? ValueType { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public ValueUnit? Unit { get; set; }

        /// <summary>
        /// 自定义单位
        /// </summary>
        public string CustomUnit { get; set; } = string.Empty;

        public List<CalculateInfo> CalculateQuestionList { get; set; }
    }


    public class GetClinicalCalculateQuestionsOutDto
    {
        public Guid QuestionId { get; set; }

        public string QuestionName { get; set; }

        public List<CalculateQuestion> TableQuestions { get; set; }
    }


    public class GetClinicalCalculateQuestionsInDto
    {
        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }

        /// <summary>
        /// 临床问题类型(分组，单选。)
        /// </summary>
        public string ClinicalQuestionType { get; set; } = string.Empty;
    }
    #endregion

    #region 预览


    public class GetSystemClinicalQuestionPreviewDto
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }

    public class GetTrialClinicalQuestionPreviewDto
    {
        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }
    }

    /// <summary>
    /// 项目预览问题Dto
    /// </summary>
    public class ClinicalQuestionPreviewDto : TrialClinicalQuestionDto
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }

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

        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 子问题
        /// </summary>
        public List<ClinicalQuestionPreviewDto> Childrens { get; set; } = new List<ClinicalQuestionPreviewDto>();


        /// <summary>
        /// 关联问题
        /// </summary>
        public List<ClinicalQuestionPreviewDto> RelationQuestions { get; set; } = new List<ClinicalQuestionPreviewDto>();

        /// <summary>
        /// 表格问题
        /// </summary>
        public List<ClinicalTablePreviewDto> TableQuestions { get; set; } = new List<ClinicalTablePreviewDto>();

        public List<Dictionary<Guid, string>> TableAnswer { get; set; }
    }


    /// <summary>
    /// 项目临床数据问题
    /// </summary>
    public class ClinicalTablePreviewDto : TrialClinicalTableQuestionDto
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }

    #endregion


    #region 外层问题
    /// <summary>
    /// 临床问题基本信息
    /// </summary>
    public class ClinicalQuestionBase
    {
        /// <summary>
        /// Id
        /// </summary>
        public Guid? Id { get; set; }


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

        /// <summary>
        /// 问题名称
        /// </summary>
        public string QuestionName { get; set; } = string.Empty;

        /// <summary>
        /// 问题英文名称
        /// </summary>
        public string QuestionEnName { get; set; } = string.Empty;

        /// <summary>
        /// 分组名称
        /// </summary>
        public string? GroupName { get; set; } = string.Empty;

        /// <summary>
        /// 分组英文名称
        /// </summary>
        public string? GroupEnName { get; set; } = string.Empty;

        /// <summary>
        /// 临床问题类型(分组，单选。)
        /// </summary>
        public string ClinicalQuestionType { get; set; }

        /// <summary>
        /// 问题标识
        /// </summary>
        public ClinicalQuestionMark? ClinicalQuestionMarkEnum { get; set; }

        /// <summary>
        /// 最大长度
        /// </summary>
        public int? MaxAnswerLength { get; set; }

        /// <summary>
        /// 最大行数
        /// </summary>
        public int? MaxQuestionCount { get; set; }

        /// <summary>
        /// 临床数据选项类型(无，自定义)
        /// </summary>
        public ClinicalOptionType ClinicalOptionTypeEnum { get; set; }

        /// <summary>
        /// 分组Id
        /// </summary>
        public Guid? GroupId { get; set; }

        /// <summary>
        /// 自定义选项
        /// </summary>
        public string TypeValue { get; set; } = string.Empty;

        /// <summary>
        /// 字典Code
        /// </summary>
        public string DictionaryCode { get; set; } = string.Empty;

        /// <summary>
        /// 排序
        /// </summary>
        public int ShowOrder { get; set; } = 0;

        /// <summary>
        /// 是否必填
        /// </summary>
        public IsRequired IsRequired { get; set; }

        /// <summary>
        /// 关联Value
        /// </summary>
        public string RelevanceValue { get; set; } = string.Empty;

        /// <summary>
        /// 关联ID
        /// </summary>
        public Guid? RelevanceId { get; set; }

        /// <summary>
        /// 父问题Id
        /// </summary>
        public Guid? ParentId { get; set; }

        /// <summary>
        /// 父问题触发值
        /// </summary>
        public string ParentTriggerValue { get; set; } = string.Empty;

        /// <summary>
        /// 显示类型
        /// </summary>
        public ClinicalQuestionShow ClinicalQuestionShowEnum { get; set; } = ClinicalQuestionShow.Show;

        /// <summary>
        /// 是否是检查日期
        /// </summary>
        public bool IsCheckDate { get; set; }

        /// <summary>
        /// 小数点位数
        /// </summary>
        public int? DigitPlaces { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; } = string.Empty;

        /// <summary>
        /// 自定义计算标记
        /// </summary>
        public ClinicalCalculateMark? CustomCalculateMark { get; set; }

        /// <summary>
        /// 自定义计算问题
        /// </summary>
        public string CalculateQuestions { get; set; } = "[]";


        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue { get; set; } = string.Empty;

        [NotMapped]
        public List<CalculateInfo> CalculateQuestionList
        {
            get
            {

                try
                {
                    var result = JsonConvert.DeserializeObject<List<CalculateInfo>>(this.CalculateQuestions);
                    return result == null ? new List<CalculateInfo>() : result;
                }
                catch (Exception)
                {

                    return new List<CalculateInfo>();
                }

            }
        }
    }

    /// <summary>
    /// 查询临床数据基类
    /// </summary>
    public class ClinicalQuestionQueryBase : PageInput
    {
        /// <summary>
        /// 问题名称
        /// </summary>
        public string QuestionName { get; set; } = string.Empty;
    }

    /// <summary>
    /// 获取项目临床数据
    /// </summary>
    public class TrialClinicalQuestionQuery : ClinicalQuestionQueryBase
    {
        public Guid TrialClinicalId { get; set; }
    }

    /// <summary>
    /// 获取项目分组
    /// </summary>
    public class GetTrialGroupDto
    {
        public Guid TrialClinicalId { get; set; }
    }



    /// <summary>
    /// 项目临床数据问题
    /// </summary>
    public class TrialClinicalQuestionDto : ClinicalQuestionBase
    {
        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }
    }

    /// <summary>
    /// 获取系统临床数据
    /// </summary>
    public class SystemClinicalQuestionQuery : ClinicalQuestionQueryBase
    {
        public Guid SystemClinicalId { get; set; }
    }



    /// <summary>
    /// 系统临床数据问题
    /// </summary>
    public class SystemClinicalQuestionDto : ClinicalQuestionBase
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }

    /// <summary>
    /// 获取系统分组
    /// </summary>
    public class GetSystemGroupDto
    {
        public Guid SystemClinicalId { get; set; }
    }

    public class GetClinicalOtherTableQuestionListInDto
    {
        public Guid QuestionId { get; set; }

        public Guid TableQuestionId { get; set; }
    }
    #endregion


    #region 表格问题
    public class ClinicalTableQuestionBase
    {

        public Guid? Id { get; set; }

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

        /// <summary>
        /// 问题名称
        /// </summary>
        public string QuestionName { get; set; } = string.Empty;

        /// <summary>
        /// 问题英文名称
        /// </summary>
        public string QuestionEnName { get; set; } = string.Empty;

        /// <summary>
        /// 临床问题类型(分组，单选。)
        /// </summary>
        public string ClinicalTableQuestionType { get; set; }

        /// <summary>
        /// 问题标识
        /// </summary>
        public ClinicalTableQuestionMark? ClinicalTableQuestionMarkEnum { get; set; }

        /// <summary>
        /// 最大长度
        /// </summary>
        public int? MaxAnswerLength { get; set; }

        /// <summary>
        /// 临床数据选项类型(无，自定义)
        /// </summary>
        public ClinicalOptionType ClinicalOptionTypeEnum { get; set; }

        /// <summary>
        /// 自定义选项
        /// </summary>
        public string TypeValue { get; set; }

        /// <summary>
        /// 字典Code
        /// </summary>
        public string DictionaryCode { get; set; } = string.Empty;

        /// <summary>
        /// 排序
        /// </summary>
        public int ShowOrder { get; set; }

        /// <summary>
        /// 是否必填
        /// </summary>
        public IsRequired IsRequired { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public Guid CreateUserId { get; set; }

        /// <summary>
        /// 外层问题Id
        /// </summary>
        public Guid QuestionId { get; set; }

        /// <summary>
        /// 小数点位数
        /// </summary>
        public int? DigitPlaces { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; } = string.Empty;

        /// <summary>
        /// 关联ID
        /// </summary>
        public Guid? RelevanceId { get; set; }

        /// <summary>
        /// 关联Value
        /// </summary>
        public string RelevanceValue
        {
            get
            {
                return string.Join(',', this.RelevanceValueList);
            }
        }

        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue { get; set; } = string.Empty;

        public List<string> RelevanceValueList { get; set; } = new List<string>();
    }



    /// <summary>
    /// 项目临床数据问题
    /// </summary>
    public class TrialClinicalTableQuestionDto : ClinicalTableQuestionBase
    {
        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }

        /// <summary>
        /// 自定义计算标记
        /// </summary>
        public ClinicalCalculateMark? ClinicalCalculateMarkEnum { get; set; }

        /// <summary>
        /// 自定义计算问题
        /// </summary>
        public string CalculateQuestions { get; set; } = "[]";
    }



    /// <summary>
    /// 系统临床数据问题
    /// </summary>
    public class SystemClinicalTableQuestionDto : ClinicalTableQuestionBase
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }




    /// <summary>
    /// 查询临床数据基类
    /// </summary>
    public class ClinicalTableQuestionQueryBase : PageInput
    {
        /// <summary>
        /// 问题名称
        /// </summary>
        public string QuestionName { get; set; } = string.Empty;

        /// <summary>
        /// 外层问题Id
        /// </summary>
        public Guid QuestionId { get; set; }
    }

    /// <summary>
    /// 获取项目临床数据
    /// </summary>
    public class TrialClinicalTableQuestionQuery : ClinicalTableQuestionQueryBase
    {

    }

    /// <summary>
    /// 获取系统临床数据
    /// </summary>
    public class SystemClinicalTableQuestionQuery : ClinicalTableQuestionQueryBase
    {

    }
    #endregion


    #region 同步临床数据


    public class TrialClinicalTableQuestionSynchronize : TrialClinicalTableQuestion
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }

    public class TrialClinicalQuestionSynchronize : TrialClinicalQuestion
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }
    }


    public class SynchronizationQuestionDto
    {
        /// <summary>
        /// 系统临床数据Id
        /// </summary>
        public Guid SystemClinicalId { get; set; }



        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }
    }


    public class QuestionDic
    {
        public Guid SystemQuestionId { get; set; }

        public Guid TrialQuestionId { get; set; }

        /// <summary>
        /// 项目临床数据Id
        /// </summary>
        public Guid TrialClinicalId { get; set; }
    }

    #endregion
}
