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 ManagementConfig : Profile
    {
        public ManagementConfig()
        {

            CreateMap<UserTypeMenu, MenuTreeNode>().IncludeMembers(t => t.Menu);




            CreateMap<RoleCommand, Role>();

            CreateMap<UserCommand, User>().ForMember(d => d.UserCode, x => x.Ignore());
            CreateMap<UserType, UserTypeMenuAddOrEdit>().ReverseMap()
                .ForMember(t => t.UserTypeMenuList, u => u.MapFrom(c => c.MenuIds))
                .ForMember(t => t.UserTypeGroupList, u => u.MapFrom(c => c.UserTypeGroupIdList));

            CreateMap<Guid, UserTypeGroup>().ForMember(t => t.DictionaryId, u => u.MapFrom(c => c))
               .EqualityComparison((odto, o) => odto == o.DictionaryId);

            CreateMap<Guid, UserTypeMenu>().ForMember(t => t.MenuId, u => u.MapFrom(c => c))
                .EqualityComparison((odto, o) => odto == o.MenuId);

            CreateMap<UserTypeGroup, UserTypeGroupInfo>()
                .ForMember(t => t.GroupName, u => u.MapFrom(c => c.Group.Value))
                .ForMember(t => t.GroupNameCN, u => u.MapFrom(c => c.Group.ValueCN));


            CreateMap<MenuCommand, Menu>().ForMember(d => d.Id, u => u.MapFrom(t => t.MenuId)).ReverseMap();

            CreateMap<Menu, MenuTreeNode>()
                .ForMember(d => d.MenuId, u => u.MapFrom(t => t.Id));

            CreateMap<Menu, MenuDTO>().ForMember(d => d.MenuId, u => u.MapFrom(t => t.Id));


            CreateMap<UserType, UserTypeRoleView>()
                .ForMember(t => t.MenuIds, u => u.MapFrom(c => c.UserTypeMenuList.Select(t => t.MenuId)));

            CreateMap<Role, RoleDTO>();
            CreateMap<Menu, MenuFunctionDTO>();


            CreateMap<UserType, UserTypeViewModel>();


            //用户类型菜单勾选
            var userTypeId = Guid.Empty;
            CreateMap<Menu, MenuTreeNodeSelect>()
                  .ForMember(d => d.IsSelect, u => u.MapFrom(s => s.UserTypeMenuList.Any(t => t.UserTypeId == userTypeId)));

            //菜单树
            CreateMap<Menu, MenuFuncTreeNodeView>()
                /*.ForMember(d => d.meta, u => u.MapFrom(s => new Meta() { MetaTitle = s.MetaTitle, MetaBreadcrumb = s.MetaBreadcrumb, MetaIcon = s.MetaIcon, MetaActiveMenu = s.MetaActiveMenu }))*/;

            //功能树
            CreateMap<Menu, FunctionTreeNodeDTO>();

            //普通用户菜单树
            CreateMap<UserTypeMenu, MenuFuncTreeNodeView>().IncludeMembers(t => t.Menu)
                .ForMember(d => d.Id, u => u.MapFrom(t => t.Menu.Id));
            //.ForMember(d => d.meta, u => u.MapFrom(s => new Meta() { MetaTitle = s.MenuFunction.MetaTitle, MetaBreadcrumb = s.MenuFunction.MetaBreadcrumb, MetaIcon = s.MenuFunction.MetaIcon, MetaActiveMenu = s.MenuFunction.MetaActiveMenu })); 


            CreateMap<UserTypeMenu, FunctionTreeNodeDTO>().IncludeMembers(t => t.Menu)
                 .ForMember(d => d.Id, u => u.MapFrom(t => t.Menu.Id));

            CreateMap<UserType, TrialUserType>()
                 .ForMember(d => d.UserType, u => u.MapFrom(t => t.UserTypeName));

            CreateMap<User, UserDetailDTO>()
           .ForMember(d => d.RealName, u => u.MapFrom(s => s.FullName))
           .ForMember(d => d.UserTypeId, u => u.MapFrom(s => s.UserTypeRole.Id))
           .ForMember(d => d.UserType, u => u.MapFrom(s => s.UserTypeRole.UserTypeName))
           .ForMember(d => d.UserTypeShortName, u => u.MapFrom(s => s.UserTypeRole.UserTypeShortName))
           .ForMember(d => d.CanEditUserType, u => u.MapFrom(s => !s.UserTrials.Any()));

            CreateMap<User, UserListDTO>()
              .ForMember(d => d.RealName, u => u.MapFrom(s => s.FullName))
              .ForMember(d => d.UserTypeId, u => u.MapFrom(s => s.UserTypeRole.Id))
              .ForMember(d => d.UserType, u => u.MapFrom(s => s.UserTypeRole.UserTypeShortName))
              .ForMember(d => d.CanEditUserType, u => u.MapFrom(s => !s.UserTrials.Any()));

            var token = string.Empty;
            var userId = Guid.Empty;
            CreateMap<SystemNotice, SystemNoticeView>()
               .ForMember(t => t.PublishUserName, d => d.MapFrom(t => t.PublishedUser.UserName))
                .ForMember(t => t.CreateUserName, d => d.MapFrom(t => t.CreateUser.UserName))
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));




            CreateMap<SystemNotice, SystemNoticeReadDTO>()
                 .ForMember(t => t.PublishUserName, d => d.MapFrom(t => t.PublishedUser.UserName))
                .ForMember(t => t.CreateUserName, d => d.MapFrom(t => t.CreateUser.UserName))
                .ForMember(t => t.IsRead, d => d.MapFrom(t => t.NoticeUserReadList.Any(t => t.CreateUserId == userId)))
                .ForMember(d => d.FullFilePath, u => u.MapFrom(s => s.Path + "?access_token=" + token));

            CreateMap<SystemNotice, SystemNoticeAddOrEdit>().ReverseMap()
                .ForMember(t => t.NoticeUserTypeList, u => u.MapFrom(t => t.NoticeUserTypeIdList));


            CreateMap<SystemNoticeUserType, UserTypeSimpleDTO>().IncludeMembers(t => t.NoticeUserType)
                .ForMember(t => t.Id, u => u.MapFrom(u => u.NoticeUserType.Id));

            CreateMap<UserType, UserTypeSimpleDTO>();

            CreateMap<Guid, SystemNoticeUserType>().EqualityComparison((odto, o) => odto == o.UserTypeId)
                .ForMember(d => d.UserTypeId, c => c.MapFrom(t => t));



        }
    }

}
