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

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

            var userId = Guid.Empty;
            var token = string.Empty;
            CreateMap<SystemDocument, SystemDocumentView>()
                 .ForMember(d => d.FileType, u => u.MapFrom(s => s.FileType.Value))
                 .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path)); 

            CreateMap<TrialDocument, TrialDocumentView>()
                .ForMember(d => d.FileType, u => u.MapFrom(s => s.FileType.MappedValue))
                .ForMember(d => d.IsSomeUserSigned, u => u.MapFrom(s => s.TrialDocConfirmedUserList.Any()))
                .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<TrialDocConfirmedUser, 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<TrialUser, TrialDocumentUserConfirmView>();




            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.TrialCriterionName, c => c.MapFrom(t => t.TrialReadingCriterion.CriterionName))
                 .ForMember(d => d.TrialEmailNoticeUserList, c => c.MapFrom(t => t.TrialEmailNoticeUserList));

            CreateMap<TrialEmailNoticeConfigAddOrEdit, TrialEmailNoticeConfig>();

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

            CreateMap<TrialEmailNoticeUser, EmailUserInfoDto>();

        }
    }

}
