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

namespace IRaCIS.Core.Application.Service
{
    public class TrialSiteUserConfig : Profile
    {
        public TrialSiteUserConfig()
        {
            //CreateMap<TrialAttachment, AcquisitionSpecificationDTO>();
            //CreateMap<TrialAttachmentCommand, TrialAttachment>().ForMember(t => t.UserTypes, u => u.MapFrom(k => string.Join(',', k.UserTypeList)));



            //CreateMap<TrialTaskConfigView, Trial>().ForMember(d => d.Id, u => u.MapFrom(s => s.TrialId)).ReverseMap();


            CreateMap<Trial, TrialProcessConfigDTO>();
            

            CreateMap<ReadingQuestionCriterionTrial, TrialTaskConfigView > ()
                .ForMember(d => d.QCProcessEnum, u => u.MapFrom(s => s.Trial.QCProcessEnum))
                .ForMember(d => d.IsImageConsistencyVerification, u => u.MapFrom(s => s.Trial.IsImageConsistencyVerification))
                .ReverseMap();


            CreateMap<TrialTaskConfig, Trial>().ForMember(d => d.Id, u => u.MapFrom(s => s.TrialId)).ReverseMap();

            CreateMap<TrialReadingTaskViewConfig, Trial>().ForMember(d => d.Id, u => u.MapFrom(s => s.TrialId)).ReverseMap();

            CreateMap<TrialJudgeTaskConfig, Trial>().ForMember(d => d.Id, u => u.MapFrom(s => s.TrialId));

            

            CreateMap<UserTrialCommand, TrialUser>();

            CreateMap<TrialSiteCommand, TrialSite>()
                .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.SiteName))
                .ForMember(x => x.Id, x => x.Ignore());

            CreateMap<EditTrialSiteCommand, TrialSite>();

            CreateMap<UpdateTrialUserCommand, TrialUser>();



            CreateMap<AssginSiteCRCCommand, TrialUser>();

            CreateMap<TrialUserAddCommand, TrialUser>();

            CreateMap<SiteCRCCommand, TrialUser>();


            //临床项目
            CreateMap<TrialCommand, Trial>().ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

            CreateMap<Dictionary, TrialDictionaryView>();

            var userId = Guid.Empty;
            var userTypeEnumInt = 0;
            CreateMap<Trial, TrialDetailDTO>()
                .ForMember(x=>x.CriterionList,y=>y.MapFrom(z=>z.ReadingQuestionCriterionTrialList.Where(n=>n.IsConfirm).Select(m=>m.CriterionName)))
                .ForMember(d => d.DictionaryList, u => u.MapFrom(s => s.TrialDicList.Select(t => t.Dictionary).OrderBy(t => t.ShowOrder)))
                //.ForMember(d => d.Code, u => u.MapFrom(s => s.TrialCode))
                .ForMember(d => d.Sponsor, u => u.MapFrom(s => s.Sponsor.SponsorName))
                .ForMember(d => d.Phase, u => u.MapFrom(s => s.Phase.MappedValue))
                .ForMember(d => d.DeclarationType, u => u.MapFrom(s => s.DeclarationType.MappedValue))
                .ForMember(d => d.IndicationType, u => u.MapFrom(s => s.IndicationType.MappedValue))
                .ForMember(d => d.CRO, u => u.MapFrom(s => s.CRO.CROName))
                .ForMember(d => d.ReviewMode, u => u.MapFrom(s => s.ReviewMode.MappedValue))
                //.ForMember(d => d.ReviewType, u => u.MapFrom(s => s.ReviewType.Value))
                .ForMember(d => d.IsLocked, u => u.MapFrom(s => s.WorkloadList.Any(u => u.DataFrom == (int)WorkLoadFromStatus.FinalConfirm)))
                //.ForMember(d => d.SiteCount, u => u.MapFrom(s => userTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator ? s.TrialSiteUserList.Count(k => k.UserId == userId) : s.TrialSiteList.Count()))
                //.ForMember(d => d.StudyCount, u => u.MapFrom(s => userTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator ? s.StudyList.Count(t => t.TrialSite.CRCUserList.Any(t => t.UserId == userId)) : s.StudyList.Count()))
                //.ForMember(d => d.SubjectCount, u => u.MapFrom(s => userTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator ? s.SubjectList.Count(t => t.TrialSite.CRCUserList.Any(t => t.UserId == userId)) : s.SubjectList.Count()))
                ;




            CreateMap<TrialUser, TrialMaintenanceDTO>().IncludeMembers(t => t.User)
            .ForMember(d => d.UserRealName, u => u.MapFrom(s => s.User.FullName))
            .ForMember(t => t.UserType, u => u.MapFrom(t => t.User.UserTypeRole.UserTypeShortName));
            CreateMap<User, TrialMaintenanceDTO>();

            var siteId = Guid.Empty;
            CreateMap<TrialUser, AssginSiteCRCListDTO>().IncludeMembers(t => t.User)
              .ForMember(d => d.UserRealName, u => u.MapFrom(s => s.User.FullName))
              .ForMember(d => d.SiteId, u => u.MapFrom(t => siteId))
              .ForMember(d => d.UserType, u => u.MapFrom(t => t.User.UserTypeRole.UserTypeShortName))
              .ForMember(d => d.IsSelect, u => u.MapFrom(t => t.SiteList.Any(k => k.SiteId == siteId)));
            CreateMap<User, AssginSiteCRCListDTO>();

            var trialId = Guid.Empty;
            CreateMap<User, TrialUserScreeningDTO>()
             .ForMember(d => d.UserRealName, u => u.MapFrom(s => s.FullName))
              .ForMember(d => d.UserType, u => u.MapFrom(s => s.UserTypeRole.UserTypeShortName))
              .ForMember(d => d.UserId, u => u.MapFrom(s => s.Id))
              .ForMember(d => d.TrialId, u => u.MapFrom(s => trialId))
              .ForMember(d => d.IsSelect, u => u.MapFrom(t => t.UserTrials.Any(t => t.TrialId == trialId)));


            CreateMap<Site, SiteSelectDTO>()
          .ForMember(d => d.HospitalName, u => u.MapFrom(s => s.Hospital.HospitalName));

            //trial site 选择列表  subjectVisit pannel 模式添加的时候
            CreateMap<Site, TrialSiteScreeningDTO>()
                 .ForMember(d => d.IsSelect, u => u.MapFrom(s => s.TrialSiteList.Any(k => k.TrialId == trialId)));

            #region 项目 stie pannel

            #region  site 也有country  hospital 也有  注意区分
            CreateMap<TrialSite, SiteStatDTO>()
           .ForMember(d => d.SiteCode, u => u.MapFrom(s => s.Site.SiteCode))
           .ForMember(d => d.City, u => u.MapFrom(s => s.Site.City))
           .ForMember(d => d.Country, u => u.MapFrom(s => s.Site.Country))
           .ForMember(d => d.Hospital, u => u.MapFrom(s => s.Site.Hospital.HospitalName))


           .ForMember(d => d.DirectorName, u => u.MapFrom(s => s.Site.DirectorName))
           .ForMember(d => d.DirectorPhone, u => u.MapFrom(s => s.Site.DirectorPhone))
           .ForMember(d => d.ContactPhone, u => u.MapFrom(s => s.Site.ContactPhone))
           .ForMember(d => d.Address, u => u.MapFrom(s => s.Site.Address))
           .ForMember(d => d.Site, u => u.MapFrom(s => s.Site.SiteName))
           .ForMember(d => d.VisitCount, u => u.MapFrom(s => s.SubjectVisitList.Count()))
           .ForMember(d => d.SubjectCount, u => u.MapFrom(s => s.SubjectList.Count()))



           .ForMember(d => d.UserCount, u => u.MapFrom(s => s.CRCUserList.Count()))
           .ForMember(d => d.UserNameList, u => u.MapFrom(s => s.CRCUserList.Where(t => t.IsDeleted == false).Select(u => u.User.FullName)));
            #endregion





            CreateMap<TrialSite, SiteStatSimpleDTO>().IncludeMembers(t => t.Site)
              .ForMember(d => d.Id, u => u.MapFrom(s => s.Id))
              .ForMember(d => d.UpdateTime, u => u.MapFrom(s => s.UpdateTime))
              .ForMember(d => d.Site, u => u.MapFrom(s => s.Site.SiteName))
              .ForMember(d => d.Hospital, u => u.MapFrom(s => s.Site.Hospital.HospitalName))
              .ForMember(d => d.UserCount, u => u.MapFrom(s => s.CRCUserList.Count()))
              .ForMember(d => d.UserNameList, u => u.MapFrom(s => s.CRCUserList.Where(t => t.IsDeleted == false).Select(u => u.User.FullName)));
            CreateMap<Site, SiteStatSimpleDTO>();



            CreateMap<TrialSiteUser, UserTrialDTO>().IncludeMembers(t => t.User)
                .ForMember(d => d.UserType, u => u.MapFrom(s => s.User.UserTypeRole.UserTypeShortName))
                .ForMember(d => d.UserRealName, u => u.MapFrom(s => s.User.FullName));
            CreateMap<User, UserTrialDTO>();
            #endregion



            CreateMap<AssginSiteCRCCommand, TrialSiteUser>();
            CreateMap<Domain.Models.Trial, TrialConfigDTO>().ForMember(t => t.TrialId, u => u.MapFrom(c => c.Id))
                    .ForMember(t => t.TrialCriterionIds, u => u.MapFrom(c => c.ReadingQuestionCriterionTrialList.Where(v =>v.IsConfirm).OrderBy(x=>x.ShowOrder).Select(r => r.Id)))
                    .ForMember(t => t.TrialCriterionNames, u => u.MapFrom(c => c.ReadingQuestionCriterionTrialList.Where(v => v.IsConfirm).OrderBy(x => x.ShowOrder).Select(r => r.CriterionName)))
                    .ForMember(t => t.ClinicalDataTrialSetIds, u => u.MapFrom(c => c.clinicalDataTrialSets.Where(v => v.IsConfirm).Select(r => r.Id)))
                    .ForMember(t => t.ClinicalDataSetNames, u => u.MapFrom(c => c.clinicalDataTrialSets.Where(v => v.IsConfirm).Select(r => r.ClinicalDataSetName)))
                    //.ForMember(t => t.CriterionIds, u => u.MapFrom(c => c.TrialDicList.Where(v => v.KeyName == StaticData.Criterion).Select(r => r.DictionaryId)))
                    ;
            CreateMap<Domain.Models.Trial, TrialSubjectConfig>();
            CreateMap<Domain.Models.Trial, TrialSubjectAndSVConfig>();


            CreateMap<BasicTrialConfig, Trial>();

            CreateMap<TrialProcessConfig, Trial>().ForMember(t => t.TrialDicList, u => u.MapFrom(k => k.CriterionIds));
            CreateMap<Guid, TrialDictionary>().EqualityComparison((odto, o) => odto == o.DictionaryId)
                .ForMember(t => t.DictionaryId, u => u.MapFrom(c => c))
                 .ForMember(t => t.KeyName, u => u.MapFrom(c => StaticData.Criterion));


            CreateMap<Trial, TrialSelectDTO>();

            CreateMap<TrialUrgentConfig, Trial>();
            CreateMap<Trial, TrialUrgentConfig > ().ForMember(t=>t.TrialId,u=>u.MapFrom(c=>c.Id));

            CreateMap<SignDTO, TrialSign>();


            CreateMap<SignDTO, TrialSign>();

            CreateMap<TrialStateChange, TrialStateChangeDTO>()
                 .ForMember(t => t.UserRealName, u => u.MapFrom(c => c.User.FullName))
                 .ForMember(t => t.UserName, u => u.MapFrom(c => c.User.UserName));



            CreateMap<TrialExternalUserAddAndSendEmail, TrialExternalUser>().ReverseMap();

            CreateMap<TrialExternalUserAddAndSendEmail, User>();

            CreateMap<TrialExternalUser, TrialExternalUserAddOrEdit>().ReverseMap();
            CreateMap<TrialExternalUserAddOrEdit, User>();

            CreateMap<TrialExternalUser, TrialExternalUserView>();


            CreateMap<User, TrialExternalUser>().ReverseMap();


            CreateMap<TrialUserPreparation, TrialUserPreparationView>()

                .ForMember(t => t.UserRealName, u => u.MapFrom(c => c.User.FullName))
                .ForMember(t => t.UserName, u => u.MapFrom(c => c.User.UserName))
                .ForMember(t => t.UserTypeShortName, u => u.MapFrom(c => c.User.UserTypeRole.UserTypeShortName));


            CreateMap<TrialExternalUser, TrialInfoWithPreparationInfo>().IncludeMembers(t => t.Trial)
                .ForMember(t => t.UserId, u => u.MapFrom(c => c.SystemUserId));
            CreateMap<Trial, TrialInfoWithPreparationInfo>();

            CreateMap<TrialSiteUserSurvey, TrialInfoWithPreparationInfo>().IncludeMembers(t => t.TrialSiteSurvey.Trial)
                .ForMember(t => t.UserId, u => u.MapFrom(c => c.SystemUserId))
                .ForMember(t => t.TrialId, u => u.MapFrom(c => c.TrialSiteSurvey.TrialId));


            CreateMap<TrialExternalUserConfirm, TrialExternalUser>();

            CreateMap<TrialExternalUserConfirm, TrialSiteUserSurvey>();

            CreateMap<ReadingCriterionPage, ReadingCriterionPageDto>();

            CreateMap<Trial, TrialBasicConfigView>();


            CreateMap<Trial, TrialUserExportDTO>()
                .ForMember(t => t.TrialUserList, u => u.Ignore());




            CreateMap<ReadingQuestionCriterionTrial, GetTrialReadingInfoOutDto>();



            CreateMap<ReadingQuestionTrial, TrialReadQuestion>()
            .ForMember(d => d.GroupName, u => u.MapFrom(s => s.GroupInfo == null ? s.GroupName : s.GroupInfo.GroupName))
           .ForMember(d => d.GroupEnName, u => u.MapFrom(s => s.GroupInfo == null ? s.GroupEnName : s.GroupInfo.GroupEnName))
       .ForMember(t => t.PageName, u => u.MapFrom(c => c.ReadingCriterionPage.PageName))
          .ForMember(d => d.ParentQuestionGenre, u => u.MapFrom(s => s.ParentReadingQuestionTrial.QuestionGenre))
             .ForMember(d => d.ParentDictionaryCode, u => u.MapFrom(s => s.ParentReadingQuestionTrial.DictionaryCode))
        .ForMember(t => t.ParentQuestionName, u => u.MapFrom(c => c.ParentReadingQuestionTrial.QuestionName))
          .ForMember(t => t.RelevanceShowOrder, u => u.MapFrom(c => c.RelevanceReadingQuestionTrial.ShowOrder))
              .ForMember(t => t.ParentQuestionShowOrder, u => u.MapFrom(c => c.ParentReadingQuestionTrial.ShowOrder));



            CreateMap<Trial, TrialSiteUserExportDto>()
                .ForMember(t => t.TrialSiteUserList, u => u.Ignore());

            CreateMap<TrialSiteUser, SiteUserExportDTO>().IncludeMembers(t => t.User)
                 .ForMember(t => t.TrialSiteCode, u => u.MapFrom(c => c.TrialSite.TrialSiteCode))
                 .ForMember(t => t.TrialSiteAliasName, u => u.MapFrom(c => c.TrialSite.TrialSiteAliasName))
                .ForMember(t => t.UserRealName, u => u.MapFrom(c => c.User.FullName))
                .ForMember(t => t.UserType, u => u.MapFrom(c => c.User.UserTypeRole.UserTypeShortName));
            CreateMap<User, SiteUserExportDTO>();




            CreateMap<Trial, TrialSiteUserSummaryExportDto>()
                .ForMember(t => t.TrialSiteUserList, u => u.Ignore());

            CreateMap<TrialSiteUserSurvey, TrialSiteUserSummaryDto>()
                .ForMember(t => t.TrialRoleName, u => u.MapFrom(d => d.TrialRoleName.Value))
                .ForMember(d => d.UserType, u => u.MapFrom(s => s.UserTypeRole.UserTypeShortName))
                .ForMember(t => t.TrialSiteCode, u => u.MapFrom(d => d.TrialSiteSurvey.TrialSite.TrialSiteCode))
                .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSiteSurvey.TrialSite.TrialSiteAliasName));


            CreateMap<Trial, TrialSiteSurveyStat>()
                .ForMember(t => t.ApprovalRequiredCount, u =>
                       u.MapFrom(c => userTypeEnumInt == (int)UserTypeEnum.ProjectManager || userTypeEnumInt == (int)UserTypeEnum.APM ?
                         c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.SPMApproved).Count()
                         : c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.CRCSubmitted).Count())
                )

                 .ForMember(t => t.ApprovalRequiredSiteCount, u =>
                    u.MapFrom(c => userTypeEnumInt == (int)UserTypeEnum.ProjectManager || userTypeEnumInt == (int)UserTypeEnum.APM ?
                        c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.SPMApproved).Select(t => t.SiteId).Distinct().Count()
                        : c.TrialSiteSurveyList.Where(t => t.State == TrialSiteSurveyEnum.CRCSubmitted).Select(t => t.SiteId).Distinct().Count())
                )
                ;

            var userTypeId = Guid.Empty;
            CreateMap<Trial, DocSignStat>()
                .ForMember(t => t.WaitSignCount, u =>
                    u.MapFrom(c => userTypeEnumInt == (int)UserTypeEnum.SuperAdmin ? 0

                        : c.TrialDocumentList.Where(t => t.IsDeleted == false && t.NeedConfirmedUserTypeList.Any(t => t.NeedConfirmUserTypeId == userTypeId) && !t.TrialDocConfirmedUserList.Any(t => t.ConfirmUserId == userId && t.ConfirmTime!=null)).Count())
                );




        }
    }

}
