using AutoMapper;
using AutoMapper.EquivalencyExpression;
using IRaCIS.Application.Contracts;
using IRaCIS.Application.Contracts.Pay;
using IRaCIS.Core.Application.Service.Reading.Dto;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;

namespace IRaCIS.Core.Application.Service
{
    public class DoctorConfig : Profile
    {
        public DoctorConfig()
        {
            var isEn_Us = false;

            Guid TrialId = Guid.Empty;

            // 从 DateTime 映射到 DateOnly
            CreateMap<DateTime, DateOnly>().ConvertUsing(dt => DateOnly.FromDateTime(dt));

            // 从 DateOnly 映射到 DateTime，假设时间部分为 00:00:00

            CreateMap<DateOnly, DateTime>().ConvertUsing(t => new DateTime(t.Year, t.Month, t.Day));

            // 从 DateOnly? 映射到 DateTime?
            CreateMap<DateOnly?, DateTime?>()
                .ConvertUsing(t => t.HasValue ? new DateTime(t.Value.Year, t.Value.Month, t.Value.Day) : (DateTime?)null);

            // 从 DateTime? 映射到 DateOnly?
            CreateMap<DateTime?, DateOnly?>()
                .ConvertUsing(dt => dt.HasValue ? DateOnly.FromDateTime(dt.Value) : (DateOnly?)null);


            #region reviewer

            //基本信息  工作信息      添加时转换使用
            CreateMap<DoctorBasicInfoCommand, Doctor>().EqualityComparison((odto, o) => odto.Id == o.Id);

            //学习经历  添加时转换使用
            CreateMap<EducationCommand, Education>().EqualityComparison((odto, o) => odto.Id == o.Id);
            CreateMap<PostgraduateCommand, Postgraduate>().EqualityComparison((odto, o) => odto.Id == o.Id);
            CreateMap<ResearchPublicationDTO, ResearchPublication>().EqualityComparison((odto, o) => odto.Id == o.Id);
            CreateMap<TrialExperienceCommand, TrialExperience>().EqualityComparison((odto, o) => odto.Id == o.Id);

            CreateMap<ResearchPublicationInfoDTO, ResearchPublication>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));


            //医生账户
            CreateMap<DoctorAccountLoginDTO, Doctor>();
            CreateMap<DoctorAccountRegisterModel, Doctor>();

            CreateMap<VacationCommand, Vacation>().EqualityComparison((odto, o) => odto.Id == o.Id);


            CreateMap<AttachmentDTO, Attachment>().EqualityComparison((odto, o) => odto.Id == o.Id);

            CreateMap<ReviewerAckDTO, Attachment>().EqualityComparison((odto, o) => odto.Id == o.Id);

            CreateMap<AddDoctorCriterionFileDto, DoctorCriterionFile>();
            CreateMap<DoctorCriterionFile, GetDoctorCriterionFileOutDto>();
            CreateMap<Doctor, DoctorBasicInfoCommand>();

            CreateMap<Doctor, BasicInfoAndEmploymentDto>();

            CreateMap<BasicInfoAndEmploymentDto, Doctor>().ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null)); ;

            CreateMap<Education, EducationInfoViewModel>();

            CreateMap<Vacation, VacationCommand>();
            CreateMap<ResearchPublication, ResearchPublicationDTO>();
            CreateMap<Postgraduate, PostgraduateViewModel>();
              //    .ForMember(t => t.Hospital, c => c.MapFrom(d => d.HospitalEnt.HospitalName))
              //.ForMember(t => t.HospitalCN, c => c.MapFrom(d => d.HospitalEnt.HospitalNameCN));
            CreateMap<Attachment, AttachmentDTO>()
            .ForMember(d => d.BlindName, u => u.MapFrom(t => t.Doctor.BlindName))
            .ForMember(d => d.BlindNameCN, u => u.MapFrom(t => t.Doctor.BlindNameCN))
            .ForMember(d => d.ChineseName, u => u.MapFrom(t => t.Doctor.ChineseName))
            .ForMember(d => d.FirstName, u => u.MapFrom(t => t.Doctor.FirstName))
            .ForMember(d => d.LastName, u => u.MapFrom(t => t.Doctor.LastName))
            .ForMember(d => d.UserName, u => u.MapFrom(t => t.Doctor.EnrollList.Where(y=>y.TrialId== TrialId).Select(x=>x.DoctorUser.UserName).First()))
                ;
            CreateMap<Doctor, GetTrialDoctorListOutDto>();
            CreateMap<Doctor, ResumeConfirmDTO>();

            CreateMap<Doctor, DoctorSelectDTO>();
            CreateMap<Doctor, UpdateGneralSituationDto>();

            CreateMap<Doctor, PaymentModeDto>();

            CreateMap<DoctorSummarize, SummarizeInfoDto>();
            CreateMap<SummarizeInfoDto, DoctorSummarize>();

            CreateMap<PaymentModeDto, Doctor>();

            CreateMap<Doctor, TrialExperienceModel>();
            CreateMap<TrialExperience, TrialExperienceCommand>();

            CreateMap<Doctor, DoctorBasicInfo>();

            #endregion
            CreateMap<Dictionary, KeyNameType>();

            CreateMap<Dictionary, DicViewModelDTO>();

            CreateMap<AddOrUpdateDicDTO, Dictionary>().ReverseMap();

            //医生列表、项目显示列表模型转换
            CreateMap<DoctorDTO, SelectionReviewerDTO>()
                ;

            CreateMap<UserRole, UserBasicInfo>();

            CreateMap<TrialExperience, TrialExperienceListDTO>()
            .ForMember(x=>x.VisitReadingCount, u => u.MapFrom(t => (t.ExperienceDataType==ExperienceDataType.TrialAuto||t.ExperienceDataType==ExperienceDataType.SystemAuto)?
            
            // 统计数量
            t.Trial.VisitTaskList.Where(x=>x.ReadingTaskState==ReadingTaskState.HaveSigned&&x.TaskState== TaskState.Effect&&x.DoctorUser.DoctorId==t.DoctorId &&x.TrialReadingCriterion.CriterionType==t.CriterionType).Count():
            t.VisitReadingCount
            ))
            .ForMember(d => d.Phase, u => u.MapFrom(t => t.Phase.Value))
             .ForMember(d => d.EvaluationCriteriaList, u => u.MapFrom(t => t.ExperienceCriteriaList.Select(t => t.EvaluationCriteria.Value)))
             .ForMember(d => d.EvaluationCriteriaIdList, u => u.MapFrom(t => t.ExperienceCriteriaList.Select(t => t.EvaluationCriteriaId)));

            //CreateMap<Doctor, UserBasicInfo>()
            //     .ForMember(d => d.Code, u => u.MapFrom(t => t.ReviewerCode))
            //      .ForMember(d => d.RealName, u => u.MapFrom(t => t.ChineseName))
            //      .ForMember(d => d.IsReviewer, u => u.MapFrom(t => true))
            //      .ForMember(d => d.UserName, u => u.MapFrom(doctor => doctor.LastName + " / " + doctor.FirstName));

            #region 医生基本信息  
            CreateMap<Doctor, SelectionReviewerDTO>()
                         .ForMember(d => d.IsVacation, u => u.MapFrom(t => t.VacationList.Any(x=>x.StartDate<DateTime.Now&&x.EndDate>DateTime.Now)))
                .ForMember(d => d.DoctorUserName, u => u.MapFrom(t => t.UserRole.IdentityUser.UserName));


            CreateMap<Doctor, DoctorDTO>().IncludeMembers(t => t.Hospital).Include<Doctor, SelectionReviewerDTO>()
                 .ForMember(d => d.AccountUserName, u => u.MapFrom(s => s.EnrollList.Where(t => t.DoctorUserId != null).Select(c => c.DoctorUser.UserName).FirstOrDefault()))
                 .ForMember(d => d.Department, u => u.MapFrom(s => s.Department.Value))
                 .ForMember(d => d.DepartmentCN, u => u.MapFrom(s => s.Department.ValueCN))
                 .ForMember(d => d.Position, u => u.MapFrom(s => s.Position.Value))
                 .ForMember(d => d.PositionCN, u => u.MapFrom(s => s.Position.ValueCN))
                 .ForMember(d => d.Rank, u => u.MapFrom(s => s.Rank.Value))
                 .ForMember(d => d.RankCN, u => u.MapFrom(s => s.Rank.ValueCN))
                 .ForMember(d => d.Speciality, u => u.MapFrom(s => s.Speciality.Value))
                 .ForMember(d => d.SpecialityCN, u => u.MapFrom(s => s.Speciality.ValueCN))
                 .ForMember(d => d.HasResume, u => u.MapFrom(s => s.AttachmentList.Any(u => u.Type == "Resume" && u.IsOfficial)))
                 .ForMember(d => d.Submitted, u => u.MapFrom(s => s.EnrollList.Count(t => t.EnrollStatus == EnrollStatus.HasCommittedToCRO)))
                 .ForMember(d => d.Approved, u => u.MapFrom(s => s.EnrollList.Count(t => t.EnrollStatus == EnrollStatus.InviteIntoGroup)))
                 .ForMember(d => d.Reading, u => u.MapFrom(s => s.EnrollList.Count(t => t.EnrollStatus == EnrollStatus.DoctorReading)))
                 .ForMember(d => d.Finished, u => u.MapFrom(s => s.EnrollList.Count(t => t.EnrollStatus == EnrollStatus.Finished)))
                 .ForMember(d => d.Reconfirmed, u => u.MapFrom(s => s.ReviewStatus == ReviewerInformationConfirmStatus.ConfirmPass))
                 .ForMember(o => o.DictionaryList, t => t.MapFrom(u => u.DoctorDicRelationList.Where(t => t.KeyName == StaticData.ReadingType || t.KeyName == StaticData.Subspeciality).Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)));
            CreateMap<Hospital, DoctorDTO>();

            CreateMap<EmploymentCommand, Doctor>();


            //这样会左连接三次
            // CreateMap<Doctor, DoctorBasicInfoDTO>()
            //.ForMember(d => d.TitleCNList, u => u.MapFrom(s => s.DoctorDicRelationList.Where(t => t.KeyName == StaticData.Title)
            //.Select(t => new { TitleCN = t.Dictionary.ValueCN, ShowOrder = t.Dictionary.ShowOrder }).OrderBy(k => k.ShowOrder).Select(t => t.TitleCN)))
            //  .ForMember(d => d.TitleList, u => u.MapFrom(s => s.DoctorDicRelationList.Where(t => t.KeyName == StaticData.Title)
            //.Select(t => new { Title = t.Dictionary.Value, ShowOrder = t.Dictionary.ShowOrder }).OrderBy(k => k.ShowOrder).Select(t => t.Title)))
            //    .ForMember(d => d.TitleIds, u => u.MapFrom(s => s.DoctorDicRelationList.Where(t => t.KeyName == StaticData.Title)
            //.Select(t => new { TitleId = t.Dictionary.Id, ShowOrder = t.Dictionary.ShowOrder }).OrderBy(k => k.ShowOrder).Select(t => t.TitleId)));

            //这样只会查询一次 
            CreateMap<Doctor, DoctorBasicInfoDTO>()
                .ForMember(o => o.DoctorDicViewDtos, t => t.MapFrom(u => u.DoctorDicRelationList.Where(t => t.KeyName == StaticData.Title).Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)));
            CreateMap<Dictionary, DicView>()
                .ForMember(t => t.ParentCode, u => u.MapFrom(c => c.Parent.Code));
            //CreateMap<DoctorDictionary, DicView>();

            CreateMap<Doctor, SpecialtyDTO>()
                 .ForMember(o => o.Speciality, t => t.MapFrom(u => u.Speciality.Value))
                 .ForMember(o => o.SpecialityCN, t => t.MapFrom(u => u.Speciality.ValueCN))
                .ForMember(o => o.DictionaryList, t => t.MapFrom(u => u.DoctorDicRelationList.Where(t => t.KeyName == StaticData.ReadingType || t.KeyName == StaticData.Subspeciality).Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)));
            CreateMap<SpecialtyCommand, Doctor>();

            //医生职业信息
            CreateMap<Doctor, EmploymentDTO>().IncludeMembers(t => t.Hospital)
                 .ForMember(d => d.Department, u => u.MapFrom(s => s.Department.Value))
                 .ForMember(d => d.DepartmentCN, u => u.MapFrom(s => s.Department.ValueCN))
                 .ForMember(d => d.Position, u => u.MapFrom(s => s.Position.Value))
                 .ForMember(d => d.PositionCN, u => u.MapFrom(s => s.Position.ValueCN))
                 .ForMember(d => d.Rank, u => u.MapFrom(s => s.Rank.Value))
                 .ForMember(d => d.RankCN, u => u.MapFrom(s => s.Rank.ValueCN));
            CreateMap<Hospital, EmploymentDTO>();

            CreateMap<EnrollDetail, DoctorStateModelDTO>()
                   .ForMember(d => d.UserTypeEnum, u => u.MapFrom(s => s.CreateUserRole.UserTypeEnum))
                .ForMember(d => d.IntoGroupState, u => u.MapFrom(s => s.EnrollStatus))
                .ForMember(d => d.OptTime, u => u.MapFrom(s => s.CreateTime))
                 .ForMember(d => d.OptUserName, u => u.MapFrom(s => s.CreateUserRole.IdentityUser.UserName));


            CreateMap<Enroll, TrialDoctorInfo>().IncludeMembers(t => t.Doctor, t => t.Doctor.Hospital)
              .ForMember(o => o.DictionaryList, t => t.MapFrom(u => u.Doctor.DoctorDicRelationList.Where(t => t.KeyName == StaticData.ReadingType || t.KeyName == StaticData.Subspeciality).Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)))
               .ForMember(d => d.Speciality, u => u.MapFrom(s => s.Doctor.Speciality.Value))
                .ForMember(d => d.SpecialityCN, u => u.MapFrom(s => s.Doctor.Speciality.ValueCN))
                .ForMember(d => d.Id, u => u.MapFrom(s => s.Doctor.Id))
                .ForMember(d => d.Code, u => u.MapFrom(s => s.DoctorUser.UserName));

            CreateMap<Doctor, TrialDoctorInfo>();
            CreateMap<Hospital, TrialDoctorInfo>();

            CreateMap<Enroll, ConfirmationReviewerDTO>().IncludeMembers(t => t.Doctor, t => t.Doctor.Hospital)
                 .ForMember(o => o.DictionaryList, t => t.MapFrom(u => u.Doctor.DoctorDicRelationList.Where(t => t.KeyName == StaticData.ReadingType || t.KeyName == StaticData.Subspeciality).Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)))
                  .ForMember(d => d.Speciality, u => u.MapFrom(s => s.Doctor.Speciality.Value))
                   .ForMember(d => d.SpecialityCN, u => u.MapFrom(s => s.Doctor.Speciality.ValueCN))
                   .ForMember(d => d.Id, u => u.MapFrom(s => s.Doctor.Id))
                   .ForMember(d => d.Code, u => u.MapFrom(s => s.DoctorUser.UserName))
                     .ForMember(d => d.HospitalName, u => u.MapFrom(s => s.Doctor.HospitalName))
                       .ForMember(d => d.HospitalNameCN, u => u.MapFrom(s => s.Doctor.HospitalNameCN))
                       .ForMember(d => d.EnrollId, u => u.MapFrom(s => s.Id))
                       
                   ;
            CreateMap<Doctor, ConfirmationReviewerDTO>();
            CreateMap<Hospital, ConfirmationReviewerDTO>();


            #endregion


        }
    }

}
