using AutoMapper;
using AutoMapper.EquivalencyExpression;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Service.DTO;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infrastructure.Extention;

namespace IRaCIS.Core.Application.Service
{
    public class DocumentConfig : Profile
    {
        public DocumentConfig()
        {

            var userId = Guid.Empty;
            var isEn_Us = false;

            //  在此处拷贝automapper 映射

            CreateMap<UserAgreement, UserAgreementView>();
            CreateMap<UserAgreement, UserAgreementAddOrEdit>().ReverseMap();

            //  在此处拷贝automapper 映射
            CreateMap<TrialDocumentAttachment, TrialDocumentAttachmentView>();
            CreateMap<TrialDocumentAttachment, TrialDocumentAttachmentAddOrEdit>().ReverseMap();

            CreateMap<SystemDocumentAttachment, SystemDocumentAttachmentView>();
            CreateMap<SystemDocumentAttachment, SystemDocumentAttachmentAddOrEdit>().ReverseMap();

            CreateMap<AuditDocumentData, AuditDocumentAddOrEdit>();
            //CreateMap<List<AuditDocumentData>, List<AuditDocumentAddOrEdit>>();
            CreateMap<AuditDocument, AuditDocumentView>();
            CreateMap<AuditDocument, AuditDocumentUpdateDto>();
            CreateMap<AuditDocument, HistoricalVersionDto>();
            CreateMap<AuditDocumentUpdateDto, AuditDocumentAddOrEdit>();
            CreateMap<AuditDocument, AuditDocumentData>()
                .ForMember(d => d.HistoricalVersionsCount, u => u.MapFrom(s => s.AuditDocumentOldVersionList.Count()));
            CreateMap<AuditDocument, AuditDocumentAddOrEdit>().ReverseMap();
            CreateMap<SystemDocument, SystemDocumentView>()
                  .ForMember(d => d.AttachmentCount, u => u.MapFrom(s => s.SystemDocumentAttachmentList.Count()))
                 .ForMember(d => d.FileType, u => u.MapFrom(s => isEn_Us ? s.FileType.Value : s.FileType.ValueCN))
                 .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path));


            CreateMap<SysFileType, TrialFileType>()
            .ForMember(d => d.SysFileTypeId, u => u.MapFrom(s => s.Id))
            .ForMember(d => d.IsSelfDefine, u => u.MapFrom(s => false))
               .ForMember(dest => dest.CreateUserRole, opt => opt.Ignore());

            CreateMap<TrialDocument, TrialDocumentView>()
                    .ForMember(d => d.AttachmentCount, u => u.MapFrom(s => s.TrialDocumentAttachmentList.Count()))
                .ForMember(d => d.FileType, u => u.MapFrom(s => isEn_Us ? s.FileType.Value : s.FileType.ValueCN))
                .ForMember(d => d.IsSomeUserSigned, u => u.MapFrom(s => s.TrialDocConfirmedUserList.Any(t => t.ConfirmTime != null)))
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path));


            CreateMap<SystemDocument, UnionDocumentView>()
                .ForMember(d => d.IsSystemDoc, u => u.MapFrom(s => true))
             .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path));

            CreateMap<TrialDocument, UnionDocumentView>()
                .ForMember(d => d.IsSystemDoc, u => u.MapFrom(s => false))
               .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path));

            CreateMap<TrialDocNeedConfirmedUserType, NeedConfirmedUserTypeView>().ForMember(d => d.UserTypeShortName, t => t.MapFrom(c => c.UserTypeRole.UserTypeShortName));
            CreateMap<SystemDocNeedConfirmedUserType, NeedConfirmedUserTypeView>().ForMember(d => d.UserTypeShortName, t => t.MapFrom(c => c.UserTypeRole.UserTypeShortName));


            //CreateMap<TrialDocument, TrialDocumentUserView>()
            //    .ForMember(t => t.UserConfirmInfo, c => c.MapFrom(t => t.TrialDocConfirmedUserList.Where(u => u.ConfirmUserId == userId).FirstOrDefault()))
            //    .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token)); ;

            CreateMap<TrialDocConfirmedIdentityUser, TrialDocumentUserConfirmView>()
                .ForMember(d => d.UserName, c => c.MapFrom(t => t.User.UserName))
                .ForMember(d => d.RealName, c => c.MapFrom(t => t.User.FullName));

            //CreateMap<SystemDocConfirmedUser, SystemDocumentUserConfirmView>()
            //   .ForMember(d => d.UserName, c => c.MapFrom(t => t.User.UserName))
            //   .ForMember(d => d.RealName, c => c.MapFrom(t => t.User.LastName + " / " + t.User.FirstName));


            CreateMap<TrialUserRole, TrialDocumentUserConfirmView>();

            CreateMap<TrialFileDto, TrialFile>();
            CreateMap<TrialFile, TrialFileDto>();

            CreateMap<BatchAddTrialEmailNoticeConfig, TrialEmailNoticeConfig>();

            CreateMap<TrialSelectEmailNoticeConfigView, BatchAddTrialEmailNoticeConfig>();


            CreateMap<AddOrEditTrialDocument, TrialDocument>()
                .ForMember(d => d.NeedConfirmedUserTypeList, c => c.MapFrom(t => t.NeedConfirmedUserTypeIdList));

            CreateMap<Guid, TrialDocNeedConfirmedUserType>().EqualityComparison((odto, o) => odto == o.NeedConfirmUserTypeId)
                .ForMember(d => d.NeedConfirmUserTypeId, c => c.MapFrom(t => t))
                .ForMember(d => d.TrialDocumentId, c => c.Ignore());



            CreateMap<AddOrEditSystemDocument, SystemDocument>().ForMember(d => d.NeedConfirmedUserTypeList, c => c.MapFrom(t => t.NeedConfirmedUserTypeIdList));
            CreateMap<Guid, SystemDocNeedConfirmedUserType>()
                .EqualityComparison((odto, o) => odto == o.NeedConfirmUserTypeId)
               .ForMember(d => d.NeedConfirmUserTypeId, c => c.MapFrom(t => t))
               .ForMember(d => d.SystemDocumentId, c => c.Ignore());


            CreateMap<TrialEmailNoticeConfig, TrialEmailNoticeConfigView>()
                .ForMember(d => d.SysCriterionTypeList, c => c.MapFrom(t => t.SysEmailNoticeConfig.CriterionTypeList))
                .ForMember(d => d.TrialCriterionName, c => c.MapFrom(t => t.TrialReadingCriterion.CriterionName))
                 .ForMember(d => d.TrialEmailNoticeUserList, c => c.MapFrom(t => t.TrialEmailNoticeUserList));

            CreateMap<TrialEmailNoticeConfigAddOrEdit, TrialEmailNoticeConfig>();

            CreateMap<TrialUserRole, TrialUserSelct>()
                 .ForMember(d => d.Phone, c => c.MapFrom(t => t.UserRole.IdentityUser.Phone))
                 .ForMember(d => d.Email, c => c.MapFrom(t => t.UserRole.IdentityUser.EMail))
                 .ForMember(d => d.RealName, c => c.MapFrom(t => t.UserRole.IdentityUser.FullName))
                .ForMember(d => d.UserName, c => c.MapFrom(t => t.UserRole.IdentityUser.UserName));


            CreateMap<TrialEmailNoticeUser, EmailUserInfoDto>();




            CreateMap<SysFileType, SysFileTypeView>();
            CreateMap<SysFileType, SysFileTypeAddOrEdit>().ReverseMap();

            CreateMap<TrialFileType, TrialFileTypeView>();

            CreateMap<TrialFileType, TrialFileTypeAddOrEdit>().ReverseMap();

            CreateMap<TrialFinalRecord, TrialFinalRecordView>()
                  .ForMember(d => d.HistoryCount, c => c.MapFrom(t => t.TrialHistoryRecordFileList.Count));
            CreateMap<TrialFinalRecordAddOrEdit, TrialFinalRecord>();
            //.ForMember(d => d.HistoryFileRecord.TrialFileTypeId, c => c.MapFrom(t => t.TrialFileTypeId))
            //.ForMember(d => d.SignFileRecord.TrialFileTypeId, c => c.MapFrom(t => t.TrialFileTypeId))
            //.ForMember(d => d.WordFileRecord.TrialFileTypeId, c => c.MapFrom(t => t.TrialFileTypeId))
            //.ForMember(d => d.PDFFileRecord.TrialFileTypeId, c => c.MapFrom(t => t.TrialFileTypeId));

            CreateMap<TrialNormalRecord, TrialNormalRecordView>()
                .ForMember(d => d.HistoryCount, c => c.MapFrom(t => t.TrialHistoryRecordFileList.Count))
                  .ForMember(d => d.FileName, c => c.MapFrom(t => t.TrialFileRecord.FileName));
            CreateMap<TrialNormalRecord, TrialNormalRecordAddOrEdit>().ReverseMap();

            CreateMap<TrialTrianingRecord, TrialTrianingRecordView>()
                .ForMember(d => d.HistoryCount, c => c.MapFrom(t => t.TrialHistoryRecordFileList.Count))
            .ForMember(d => d.FileName, c => c.MapFrom(t => t.TrialFileRecord.FileName));
            CreateMap<TrialTrianingRecord, TrialTrianingRecordAddOrEdit>().ReverseMap();


            CreateMap<TrialHistoryRecordFile, TrialHistoryRecordFileView>();
            CreateMap<TrialHistoryRecordFile, TrialHistoryRecordFileAddOrEdit>().ReverseMap();



            CreateMap<AuditRecord, AuditRecordView>()
                 .ForMember(d => d.IdentityUserList, c => c.MapFrom(t => t.AuditRecordIdentityUserList));

            CreateMap<AuditRecordAddOrEdit, AuditRecord>()
                /* .ForMember(d => d.AuditRecordIdentityUserList, c => c.MapFrom(t => t.IdnetityUserIdList))*/;

            CreateMap<Guid, AuditRecordIdentityUser>()
                 .EqualityComparison((odto, o) => odto == o.IdentityUserId)
               .ForMember(d => d.AuditRecordId, c => c.Ignore())
                .ForMember(d => d.IdentityUserId, c => c.MapFrom(t => t));


            CreateMap<AuditRecordIdentityUser, AuditIdentiUserInfo>()
                 .ForMember(d => d.Id, c => c.MapFrom(t => t.IdentityUser.Id))
                 .ForMember(d => d.UserName, c => c.MapFrom(t => t.IdentityUser.UserName))
                 .ForMember(d => d.FullName, c => c.MapFrom(t => t.IdentityUser.FullName));

        }
    }

}
