using AutoMapper;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Contracts.Dicom.DTO;
using IRaCIS.Core.Domain.Share;

namespace IRaCIS.Core.Application.Service
{
    public class VisitMapConfig : Profile
    {
        public VisitMapConfig()
        {
            CreateMap<VisitPlanCommand, VisitStage>();
            CreateMap<VisitPlanCommand, SubjectVisit>();
            CreateMap<SubjectCommand, Subject>();

            CreateMap<VisitStage, VisitStageDTO>();

            CreateMap<VisitStage, VisitStageSelectDTO>();
            CreateMap<VisitStage, SubjectVisit>()
                .ForMember(d => d.Id, t => t.Ignore())
                .ForMember(d => d.VisitStageId, t => t.MapFrom(u => u.Id));





            CreateMap<VisitPlanInfluenceStat, VisitPlanInfluenceSubjectVisitStatDTO>()
                 .ForMember(d => d.CreateUser, u => u.MapFrom(g => g.CreateUserRole.IdentityUser.FullName));



            CreateMap<SubjectStatusChangeCommand, Subject>()
                .ForMember(d => d.Id, t => t.MapFrom(u => u.SubjectId));
            CreateMap<VisitStage, VisitStageDTO>();

            CreateMap<Subject, SubjectQueryView>()
                .ForMember(d => d.SiteName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
                .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                .ForMember(d => d.LatestBlindName, u => u.MapFrom(s => s.LatestSubjectVisit.BlindName))
                .ForMember(d => d.LatestVisitName, u => u.MapFrom(s => s.LatestSubjectVisit.VisitName))
                 .ForMember(d => d.IsHaveTaskFinished, u => u.MapFrom(s => s.SubjectVisitTaskList.Any(t => t.TaskState == TaskState.Effect && t.ReadingTaskState == ReadingTaskState.HaveSigned)))


                //.ForMember(d => d.FinalSubjectVisitId, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.IsFinalVisit).Select(c => (Guid?)c.Id).FirstOrDefault()))
                //.ForMember(d => d.FinalSubjectVisitName, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.IsFinalVisit).Select(c => c.VisitName).FirstOrDefault()))

                .ForMember(d => d.FinalSubjectVisitId, u => u.MapFrom(s => s.FinalSubjectVisitId))
                .ForMember(d => d.FinalSubjectVisitName, u => u.MapFrom(s => s.FinalSubjectVisit.VisitName))

                //.ForMember(d => d.IsSubjectSexView, u => u.MapFrom(s => s.Trial.IsSubjectSexView))
                //.ForMember(d => d.IsSubjectExpeditedView, u => u.MapFrom(s => s.Trial.IsSubjectExpeditedView))

                //不能对包含聚合或子查询的表达式执行聚合函数
                //.ForMember(d => d.InPlanStudyCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan).Sum(k => k.StudyList.Count())))
                //.ForMember(d => d.OutPlanStudyCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan==false).Sum(k => k.StudyList.Count())))

                //.ForMember(d => d.InPlanDicomStudyUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan).SelectMany(k => k.StudyList).Count()))
                //.ForMember(d => d.OutPlanDicomStudyUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan == false).SelectMany(k => k.StudyList).Count()))

                //.ForMember(d => d.InPlanNoneDicomStudyUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan).SelectMany(k => k.NoneDicomStudyList).Count()))
                //.ForMember(d => d.OutPlanNoneDicomStudyUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.InPlan == false).SelectMany(k => k.NoneDicomStudyList).Count()))

                .ForMember(d => d.InPlanVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.Subject.FinalSubjectVisitId != null ? t.VisitNum <= t.Subject.FinalSubjectVisit.VisitNum : true).Count(t => t.InPlan)))
                .ForMember(d => d.OutPlanVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Where(t => t.Subject.FinalSubjectVisitId != null ? t.VisitNum <= t.Subject.FinalSubjectVisit.VisitNum : true).Count(t => t.InPlan == false)))
                //执行不一定上传了   可能是失访  实际执行过了


                .ForMember(d => d.MissingSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.VisitNum < s.LatestSubjectVisit.VisitNum && t.SubmitState != SubmitStateEnum.Submitted && t.IsLostVisit == false)))
                 .ForMember(d => d.IsMissingImages, u => u.MapFrom(s => s.SubjectVisitList.Any(t => t.VisitNum < s.LatestSubjectVisit.VisitNum && t.SubmitState != SubmitStateEnum.Submitted && t.IsLostVisit == false)))
                .ForMember(d => d.InPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState == SubmitStateEnum.Submitted && t.InPlan == true)))
                .ForMember(d => d.LostVisitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.IsLostVisit)))
                .ForMember(d => d.InPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState == SubmitStateEnum.Submitted && t.InPlan == true)))
                .ForMember(d => d.OutPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState == SubmitStateEnum.Submitted && t.InPlan == false)));
            // .ForMember(d => d.InPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState>=SubmitStateEnum.ToSubmit && t.InPlan == true)))
            //.ForMember(d => d.OutPlanVisitSubmmitCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.SubmitState >= SubmitStateEnum.ToSubmit && t.InPlan == false)));
            //.ForMember(d => d.InPlanVisitUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.VisitExecuted == VisitExecutedEnum.Executed && t.InPlan)))
            //.ForMember(d => d.OutPlanVisitUploadCount, u => u.MapFrom(s => s.SubjectVisitList.Count(t => t.VisitExecuted == VisitExecutedEnum.Executed && t.InPlan == false)));


            //审计信息  这里不用IncludeMembers 也可以识别 是以导航属性名称开头 
            // 还有  外键？ COALESCE([t0].[SubjectId], '00000000-0000-0000-0000-000000000000') 因为destination 是Guid


            CreateMap<SubjectVisitCommand, SubjectVisit>().ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));



            CreateMap<SubjectVisit, DicomTrialSiteSubjectInfo>().IncludeMembers(t => t.Trial)
                .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
                 .ForMember(d => d.SubjectVisitId, u => u.MapFrom(s => s.Id))
                  .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                  .ForMember(d => d.TrialCode, u => u.MapFrom(s => s.Trial.TrialCode))
                  .ForMember(d => d.Sponsor, u => u.MapFrom(s => s.Trial.Sponsor.SponsorName));
            CreateMap<Trial, DicomTrialSiteSubjectInfo>();

            CreateMap<VisitPlanInfluenceStudy, VisitPlanInfluenceSubjectVisitDTO>()
                  .ForMember(d => d.SubjectVisitId, u => u.MapFrom(s => s.Id))
                  .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.SubjectVisit.Subject.Code))
                  .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
                  .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.SubjectVisit.TrialSite.TrialSiteCode));


            CreateMap<TaskStudy, VisitStudyDTO>()
                 .ForMember(d => d.StudyId, u => u.MapFrom(s => s.Id))
                  .ForMember(d => d.SeriesList, u => u.MapFrom(s => s.SeriesList));

            CreateMap<TaskSeries, DicomSeriesDTO>()
                 .ForMember(d => d.InstanceInfoList, u => u.MapFrom(s => s.InstanceList));

            CreateMap<TaskInstance, InstanceBasicInfo>();


            CreateMap<SCPImageUpload, SCPImageUploadView>()
                 .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                 .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
                 .ForMember(d => d.TrialSiteName, u => u.MapFrom(s => s.TrialSite.TrialSiteName));

            CreateMap<SCPImageUpload, SCPImageUploadExportDTO>()
               .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
               .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
               .ForMember(d => d.TrialSiteName, u => u.MapFrom(s => s.TrialSite.TrialSiteName));


            CreateMap<SCPStudy, DicomStudy>();
            CreateMap<SCPSeries, DicomSeries>();
            CreateMap<SCPInstance, DicomInstance>();

            CreateMap<DicomStudy, VisitStudyDTO>()
              .ForMember(d => d.StudyId, u => u.MapFrom(s => s.Id))
               .ForMember(d => d.SeriesList, u => u.MapFrom(s => s.SeriesList));
        }
    }

}
