class InventoryUtil {

	public static Map<String,List<UserVH>> fetchIAMUsers(BasicSessionCredentials temporaryCredentials,String accountId,String accountName) {

	    String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"IAM\"" ;

		AmazonIdentityManagement iamClient = AmazonIdentityManagementClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(InventoryConstants.REGION_US_WEST_2).build();
		String marker = null;
		List<User> users = new ArrayList<>();
		ListUsersResult rslt;
		do{
			rslt = iamClient.listUsers(new ListUsersRequest().withMarker(marker));
			users.addAll(rslt.getUsers());
			marker = rslt.getMarker();
		}while(marker!=null);

		List<UserVH> userList = new ArrayList<>();
		Map<String,List<UserVH>> iamUsers = new HashMap<>();
		iamUsers.put(accountId+delimiter+accountName, userList);
		users.parallelStream().forEach(user -> {
			UserVH userTemp = new UserVH(user);
			String userName = user.getUserName();
			List<AccessKeyMetadata> accessKeys = iamClient.listAccessKeys(new ListAccessKeysRequest().withUserName(userName)).getAccessKeyMetadata();
			List<AccessKeyMetadataVH> accessKeysTemp = new ArrayList<>();
			userTemp.setAccessKeys(accessKeysTemp);
			try {
				if(!CollectionUtils.isEmpty(accessKeys)){
					accessKeys.stream().forEach(accesskeyInfo -> {
						GetAccessKeyLastUsedResult accessKeyLastUsedResult = iamClient.getAccessKeyLastUsed(new GetAccessKeyLastUsedRequest().withAccessKeyId(accesskeyInfo.getAccessKeyId()));
						AccessKeyMetadataVH accessKeyVH = new AccessKeyMetadataVH(accesskeyInfo);
						accessKeysTemp.add(accessKeyVH);
						if(accessKeyLastUsedResult != null) {
							accessKeyVH.setLastUsedDate(accessKeyLastUsedResult.getAccessKeyLastUsed().getLastUsedDate());

						}
					});
				}
			}
			catch (Exception e){
			    log.warn(expPrefix+ InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			    ErrorManageUtil.uploadError(accountId,"","IAM",e.getMessage());
			}

			try{
				LoginProfile logProf =  iamClient.getLoginProfile(new GetLoginProfileRequest().withUserName(userName)).getLoginProfile();
				userTemp.setPasswordCreationDate(logProf.getCreateDate());
				userTemp.setPasswordResetRequired(logProf.isPasswordResetRequired());
			}catch (Exception e) {
				// Ignore as there may not be login profile for all users
			}
			List<Group> groups = iamClient.listGroupsForUser(new ListGroupsForUserRequest().withUserName(userName)).getGroups();
			List<String> groupsList = new ArrayList<>();
			for(Group grp : groups){
				groupsList.add(grp.getGroupName());
			}
			userTemp.setGroups(groupsList);
			userTemp.setMfa(!iamClient.listMFADevices(new ListMFADevicesRequest().withUserName(userName)).getMFADevices().isEmpty());
			synchronized (userList) {
				userList.add(userTemp);
			}
		});
		log.debug(InventoryConstants.ACCOUNT + accountId +" Type : IAM User >> "+userList.size());
		return iamUsers;
	}

	private  InventoryUtil();

}