package com.ensign.crm.module.system.convert.user;

import com.ensign.crm.framework.common.util.collection.CollectionUtils;
import com.ensign.crm.framework.common.util.collection.MapUtils;
import com.ensign.crm.framework.common.util.object.BeanUtils;
import com.ensign.crm.module.system.controller.admin.dept.vo.dept.DeptSimpleRespVO;
import com.ensign.crm.module.system.controller.admin.dept.vo.post.PostSimpleRespVO;
import com.ensign.crm.module.system.controller.admin.permission.vo.role.RoleSimpleRespVO;
import com.ensign.crm.module.system.controller.admin.user.vo.profile.UserProfileRespVO;
import com.ensign.crm.module.system.controller.admin.user.vo.user.UserRespVO;
import com.ensign.crm.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
import com.ensign.crm.module.system.dal.dataobject.dept.DeptDO;
import com.ensign.crm.module.system.dal.dataobject.dept.PostDO;
import com.ensign.crm.module.system.dal.dataobject.permission.RoleDO;
import com.ensign.crm.module.system.dal.dataobject.social.SocialUserDO;
import com.ensign.crm.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserConvert {

    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);

    default List<UserRespVO> convertList(List<AdminUserDO> list, Map<Long, DeptDO> deptMap) {
        return CollectionUtils.convertList(list, user -> convert(user, deptMap.get(user.getDeptId())));
    }

    default UserRespVO convert(AdminUserDO user, DeptDO dept) {
        UserRespVO userVO = BeanUtils.toBean(user, UserRespVO.class);
        if (dept != null) {
            userVO.setDeptName(dept.getName());
        }
        return userVO;
    }

    default List<UserSimpleRespVO> convertSimpleList(List<AdminUserDO> list, Map<Long, DeptDO> deptMap) {
        return CollectionUtils.convertList(list, user -> {
            UserSimpleRespVO userVO = BeanUtils.toBean(user, UserSimpleRespVO.class);
            String deptIdColl = userVO.getDeptIdColl();
            if (StringUtils.isBlank(deptIdColl)) {
                MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> userVO.setDeptName(dept.getName()));
                return userVO;
            }
            String[] split = deptIdColl.split(",");
            StringBuilder deptName = new StringBuilder();
            StringBuilder deptColl = new StringBuilder();
            for (String s : split) {
                Long deptId = Long.valueOf(s);
                if (deptMap.containsKey(deptId)) {
                    deptName.append(deptMap.get(deptId).getName()).append(",");
                    deptColl.append(deptMap.get(deptId).getId()).append(",");
                }
            }
            if (deptName.length() > 0) {
                userVO.setDeptName(deptName.substring(0, deptName.length() - 1));
            }
            if (deptColl.length() > 0) {
                userVO.setDeptIdColl(deptColl.substring(0, deptColl.length() - 1));
            }
            return userVO;
        });
    }

    default UserProfileRespVO convert(AdminUserDO user, List<RoleDO> userRoles,
                                      DeptDO dept, List<PostDO> posts, List<SocialUserDO> socialUsers) {
        UserProfileRespVO userVO = BeanUtils.toBean(user, UserProfileRespVO.class);
        userVO.setRoles(BeanUtils.toBean(userRoles, RoleSimpleRespVO.class));
        userVO.setDept(BeanUtils.toBean(dept, DeptSimpleRespVO.class));
        userVO.setPosts(BeanUtils.toBean(posts, PostSimpleRespVO.class));
        userVO.setSocialUsers(BeanUtils.toBean(socialUsers, UserProfileRespVO.SocialUser.class));
        return userVO;
    }

}
