package com.izouma.awesomeadmin.activiti;

import com.izouma.awesomeadmin.model.SysRole;
import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.UserInfoService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class CustomUserEntityManager extends UserEntityManager {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public User findUserById(String userId) {
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        if (userInfo != null) {
            User user = new UserEntity();
            user.setId(userInfo.getId().toString());
            user.setFirstName(userInfo.getUsername());
            user.setLastName(userInfo.getNickname());
        }
        return null;
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        List<Group> groupList = new ArrayList<>();
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        if (userInfo == null) {
            return null;
        }
        if (userInfo.getRoleInfoList() != null) {
            for (SysRole sysRole : userInfo.getRoleInfoList()) {
                Group group = new GroupEntity();
                group.setId(sysRole.getId().toString());
                group.setName(sysRole.getName());
                group.setType("assignment");
                groupList.add(group);
            }
        }
        return groupList;
    }
}
