using System.ComponentModel.DataAnnotations;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Extention;

namespace IRaCIS.Application.Contracts
{
    /// <summary>
    ///后台 工作量审核视图模型
    /// </summary>
    public class WorkLoadDetailDTO : WorkloadDTO
    {
        public DateTime CreateTime { get; set; }
        public string ChineseName { get; set; } = String.Empty;

        public string FirstName { get; set; } = String.Empty;
        public string LastName { get; set; } = String.Empty;
        public string Code { get; set; } = String.Empty;
        public string Indication { get; set; } = String.Empty;
        public int RowIntId { get; set; }
        public Guid RowGuid { get; set; }
        public string YearMonthStr { get; set; } = String.Empty;

        public string WorkTimeStr => YearMonthStr;

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

    public class WorkLoadDetailViewModel : WorkloadDTO
    {
        public DateTime CreateTime { get; set; }
        public string ChineseName { get; set; } = String.Empty;
        public string FirstName { get; set; } = String.Empty;
        public string LastName { get; set; } = String.Empty;
        public string Code { get; set; } = String.Empty;
        public string Indication { get; set; } = String.Empty;

        public Guid RowGuid { get; set; }
        public string YearMonthStr { get; set; } = String.Empty;

        public bool IsLock { get; set; } = false;
        public string DeclareTimeStr => CreateTime.ToString("yyyy-MM-dd");

        public string WorkTimeStr => WorkTime.ToString("yyyy-MM-dd");
    }



    public class CalculatePaymentDTO : WorkloadDTO
    {
        public string TrialCode { get; set; } = String.Empty;
        public decimal? TrialAdditional { get; set; }
        public decimal AdjustmentMultiple { get; set; }
        //public double RankPrice { get; set; }
        public decimal PersonalAdditional { get; set; }
        public decimal TimepointPrice { get; set; }
        public decimal TimepointIn24HPrice { get; set; }
        public decimal TimepointIn48HPrice { get; set; }
        public decimal AdjudicationPrice { get; set; }
        public decimal AdjudicationIn24HPrice { get; set; }
        public decimal AdjudicationIn48HPrice { get; set; }
        public decimal GlobalPrice { get; set; }
        public decimal TrainingPrice { get; set; }
        public decimal DowntimePrice { get; set; }
        public decimal RefresherTrainingPrice { get; set; }
    }

    /// <summary>
    /// 工作量审核数据库查询模型
    /// </summary>
    public class WorkloadDTO
    {
        public Guid? Id { get; set; }

        public Guid TrialId { get; set; }

        public Guid DoctorId { get; set; }

        public int DataFrom { get; set; }

        public DateTime WorkTime { get; set; }

        public int Training { get; set; }

        public int Downtime { get; set; }

        public int Timepoint { get; set; }

        public int TimepointIn24H { get; set; }

        public int TimepointIn48H { get; set; }

        public int Adjudication { get; set; }

        public int AdjudicationIn24H { get; set; }

        public int AdjudicationIn48H { get; set; }

        public int Global { get; set; }
        public int RefresherTraining { get; set; }


    }

    /// <summary>
    /// 工作量分页列表模型 医生端查询模型 
    /// </summary>
    public class WorkLoadQueryDTO : PageInput
    {
        public Guid TrialId { get; set; }
        public int WorkLoadFromStatus { get; set; }  // 关联枚举
        public DateTime? SearchBeginDateTime { get; set; }
        public DateTime? SearchEndDateTime { get; set; }
    }

    /// <summary>
    /// 后台查询模型
    /// </summary>
    public class WorkLoadStatsQueryDTO : PageInput
    {
        public Guid? TrialId { get; set; }
        public Guid? DoctorId { get; set; } = Guid.Empty;
        public List<int> WorkLoadFromStatus { get; set; } = new List<int>(); // 关联枚举
        public DateTime? SearchBeginDateTime { get; set; }
        public DateTime? SearchEndDateTime { get; set; }
    }


    public class WorkLoadDetailQueryDTO
    {
        public Guid TrialId { get; set; } = Guid.Empty;
        public Guid DoctorId { get; set; } = Guid.Empty;
        public string YearMonthStr { get; set; } = String.Empty;
    }

    public class CalculateDoctorAndMonthDTO
    {
        [Required(ErrorMessage = "需要有效的时间")]
        public DateTime CalculateMonth { get; set; }

        [Required(ErrorMessage = "需要有效的医生列表")]
        public List<Guid> NeedCalculateReviewers { get; set; } = new List<Guid>();
    }
    public class SetEnrollReadingCategoryInDto
    {
        [NotDefault]
        public Guid EnrollId { get; set; }

        public Guid TrialId { get; set; }

        [NotDefault]
        public Guid TrialReadingCriterionId { get; set; }

        public List<ReadingCategory> ReadingCategorys { get; set; }
    }


    public class SetEnrollEnableCommand
    {
        [NotDefault]
        public Guid EnrollId { get; set; }


        public Guid TrialId { get; set; }

        public bool IsEnable { get; set; }

    }

    public class WorkLoadDoctorQueryDTO : PageInput
    {
        public Guid TrialId { get; set; } = Guid.Empty;
    }

    public class WorkLoadCommand : WorkloadDTO
    {
        public Guid CreateUserId { get; set; }
        public int CreateUserType { get; set; }
        public Guid UpdateUserId { get; set; }
    }


    public class ExistWorkloadViewModel
    {
        public bool IsExist { get; set; }
        public WorkloadDTO WorkLoad { get; set; } = new WorkloadDTO();
    }

    public class WorkloadCommand : WorkloadDTO
    {
    }

    public class WorkloadExistQueryDTO
    {
        public Guid TrialId { get; set; }
        public Guid DoctorId { get; set; }
        public DateTime WorkDate { get; set; }
    }



    public class WorkLoadAndTrainingDTO
    {

        public Guid DoctorId { get; set; }/*=Guid.Empty;*/
        public string Code { get; set; } = String.Empty;
        public string FirstName { get; set; } = String.Empty;
        public string LastName { get; set; } = String.Empty;

        public string FullName { get; set; } = String.Empty;
        public string ChineseName { get; set; } = String.Empty;

        public DateTime? EnrollTime { get; set; }

        public int ReviewerReadingType { get; set; }

        public string? EnrollTimeStr => EnrollTime?.ToString("yyyy-MM-dd HH:mm:ss");

        public DateTime? UpdateTime { get; set; }

        public string? UpdateTimeStr => UpdateTime?.ToString("yyyy-MM-dd HH:mm:ss");





        //public int? TrainingTimes { get; set; }
        //public int? Downtime { get; set; }

        ////读片点
        //public int? Timepoint { get; set; }
        //public int? TimepointIn24H { get; set; }
        //public int? TimepointIn48H { get; set; }

        ////裁判阅片
        //public int? Adjudication { get; set; }
        //public int? AdjudicationIn24H { get; set; }
        //public int? AdjudicationIn48H { get; set; }

        ////全局阅片
        //public int? Global { get; set; }

        //public int? RefresherTraining { get; set; }



    }


    public class WorkLoadAndAgreementDTO : WorkLoadAndTrainingDTO
    {
        public bool IsEnable { get; set; }

        public Guid EnrollId { get; set; }
        public DateTime? OutEnrollTime { get; set; }
        public Guid AgreementId { get; set; }

        public string AgreementPath { get; set; } = String.Empty;

        public string? AgreementFileName => AgreementPath?.Split('/').Last();

        public string AgreementFullPath => AgreementPath;


        //public List<ReadingCategory> ReadingCategorys { get; set; }

        public string UserName { get; set; }


        public List<TrialReadingCriterionDto> TrialReadingCriterionList { get; set; }

        public List<TrialCriterionReadingCategory> CriterionReadingCategoryList { get; set; }

        //任务阅片状态
        public List<DoctorUserTask> ReadingTaskStateList { get; set; }

        public List<CriterionFile> CriterionFileList { get; set; }


        public List<CriterionReadingCategory> CriterionCategoryList =>
              TrialReadingCriterionList.Select(t =>
            new CriterionReadingCategory()
            {
                EnrollId = EnrollId,
                PendingCount= ReadingTaskStateList.Where(x=>x.ReadingTaskState != ReadingTaskState.HaveSigned&& x.TrialReadingCriterionId == t.TrialReadingCriterionId).Count(),

                ComplectedCount = ReadingTaskStateList.Where(x => x.ReadingTaskState == ReadingTaskState.HaveSigned && x.TrialReadingCriterionId == t.TrialReadingCriterionId).Count(),

                TotalCount = ReadingTaskStateList.Where(x => x.TrialReadingCriterionId == t.TrialReadingCriterionId) .Count(),

                StatementCriterionFileList = CriterionFileList.Where(x=>x.CriterionType==t.CriterionType&&x.FileType==CriterionFileType.Statement)
                 .WhereIf(t.CriterionType == CriterionType.SelfDefine, x => x.TrialReadingCriterionId == t.TrialReadingCriterionId).ToList(),
                AcknowledgementCriterionFileList = CriterionFileList.Where(x => x.CriterionType == t.CriterionType && x.FileType == CriterionFileType.Acknowledgement)
                 .WhereIf(t.CriterionType == CriterionType.SelfDefine, x => x.TrialReadingCriterionId == t.TrialReadingCriterionId).ToList(),
                TrialReadingCriterionId = t.TrialReadingCriterionId,
                ReadingCategorys = CriterionReadingCategoryList.Where(c => c.TrialReadingCriterionId == t.TrialReadingCriterionId).Select(t => t.ReadingCategory).OrderBy(c => c).ToList()
            }).ToList();


        //CriterionReadingCategoryList.Count==0? TrialReadingCriterionList.Select(t=>new CriterionReadingCategory() { EnrollId= EnrollId ,TrialReadingCriterionId=t.TrialReadingCriterionId}).ToList():
        // CriterionReadingCategoryList
        //.GroupBy(t => new { t.TrialReadingCriterionId, t.EnrollId })
        //.Select(g => new CriterionReadingCategory() { EnrollId = g.Key.EnrollId, TrialReadingCriterionId = g.Key.TrialReadingCriterionId, ReadingCategorys = g.Select(t => t.ReadingCategory).OrderBy(t=>t).ToList() }).ToList();


    }

    public class SetTaskUrgentInDto
    {
        public Guid VisitTaskId { get; set; }

        public bool IsUrgent { get; set; }

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


        /// <summary>
        /// 任务加急备注
        /// </summary>
        public string TaskUrgentRemake { get; set; } = string.Empty;
    }
    public class DoctorUserTask
    {
        public ReadingTaskState ReadingTaskState { get; set; }

        public CriterionType? CriterionType { get; set; }

        public Guid TrialReadingCriterionId { get; set; }
    }

    public class TrialReadingCriterionDto
    {
        public Guid TrialReadingCriterionId { get; set; }
        public string TrialReadingCriterionName { get; set; }

        public ReadingMethod ReadingType { get; set; }

        public CriterionType? CriterionType { get; set; }

        public DateTime? ReadingInfoSignTime { get; set; }

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

        //是否附加评估
        public bool IsAdditionalAssessment { get; set; }


        //自动  手动生成任务
        public bool IsAutoCreate { get; set; }

        public bool IsArbitrationReading { get; set; } = true;


        public bool IsOncologyReading { get; set; }
    }

    public class TrialCriterionReadingCategory
    {
        public Guid EnrollId { get; set; }
        public Guid TrialReadingCriterionId { get; set; }
        public ReadingCategory ReadingCategory { get; set; }
    }


    public class CriterionFile
    {

        public Guid Id { get; set; }

        public Guid? TrialReadingCriterionId { get; set; }
        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// 标准类型
        /// </summary>
        public CriterionType CriterionType { get; set; }

        /// <summary>
        /// 医生Id
        /// </summary>
        public Guid DoctorId { get; set; }

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

        /// <summary>
        /// 文件类型
        /// </summary>
        public CriterionFileType FileType { get; set; }
    }


    public class CriterionReadingCategory
    {
        public Guid EnrollId { get; set; }
        public Guid TrialReadingCriterionId { get; set; }

        /// <summary>
        /// 待办数量
        /// </summary>
        public int PendingCount { get; set; }

        public int ComplectedCount { get; set; }

        public int TotalCount { get; set; }

        public List<CriterionFile> StatementCriterionFileList { get; set; }

        public List<CriterionFile> AcknowledgementCriterionFileList { get; set; }


        public List<ReadingCategory> ReadingCategorys { get; set; } = new List<ReadingCategory>();
    }


}