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

namespace IRaCIS.Core.Application.Service
{
    public class DoctorConfig : Profile
    {
        public DoctorConfig()
        {
            #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<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<Education, EducationInfoViewModel>();

            CreateMap<Vacation, VacationCommand>();
            CreateMap<Education, EducationInfoViewModel>();
            CreateMap<ResearchPublication, ResearchPublicationDTO>();
            CreateMap<Postgraduate, PostgraduateViewModel>();
            CreateMap<Attachment, AttachmentDTO>();
            CreateMap<Doctor, ResumeConfirmDTO>();

            CreateMap<Doctor, DoctorSelectDTO>();

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

            CreateMap<Doctor, DoctorBasicInfo>();

            #endregion
            CreateMap<Dictionary, KeyNameType>();

            CreateMap<Dictionary, DicViewModelDTO>();

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

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

            CreateMap<User, UserBasicInfo>()
             .ForMember(d => d.UserTypeShortName, u => u.MapFrom(t => t.UserTypeRole.UserTypeShortName))
             .ForMember(d => d.Code, u => u.MapFrom(t => t.UserCode))
             .ForMember(d => d.PermissionStr, u => u.MapFrom(t => t.UserTypeRole.PermissionStr))
             .ForMember(d => d.RealName, u => u.MapFrom(user => string.IsNullOrEmpty(user.FirstName) ? user.LastName : user.LastName + " / " + user.FirstName));

            CreateMap<TrialExperience, TrialExperienceListDTO>()
            .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>();
            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.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.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.CreateUser.UserName));

            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))
                   ;
            CreateMap<Doctor, ConfirmationReviewerDTO>();
            CreateMap<Hospital, ConfirmationReviewerDTO>();


            #endregion

           
        }
    }
   
}
