class ApplicationTeamReadOnlyViewModelPopulator implements QuestionReadOnlyViewModelPopulator<ApplicationTeamReadOnlyViewModel> {

    @Override
    public ApplicationTeamReadOnlyViewModel populate(CompetitionResource competition, QuestionResource question, ApplicationReadOnlyData data, ApplicationReadOnlySettings settings) {
        boolean internalUser = data.getUser().isInternalUser();
        List<ProcessRoleResource> applicationProcessRoles = userRestService.findProcessRole(data.getApplication().getId()).getSuccess();
        List<ProcessRoleResource> processRoles = applicationProcessRoles.stream()
                .filter(role -> applicantProcessRoles().contains(role.getRole()))
                .collect(toList());
        List<InviteOrganisationResource> inviteOrganisationResources = emptyList();
        if (showInvites(data)) {
            inviteOrganisationResources = inviteRestService.getInvitesByApplication(data.getApplication().getId()).getSuccess();
        }

        Optional<ProcessRoleResource> ktaProcessRole = Optional.empty();
        String ktaPhoneNumber = null;

        if (competition.isKtp()) {
            ktaProcessRole = applicationProcessRoles.stream()
                    .filter(role -> KNOWLEDGE_TRANSFER_ADVISER == role.getRole())
                    .findAny();

            if(internalUser && ktaProcessRole.isPresent()) {
               ktaPhoneNumber = getPhoneNumber(ktaProcessRole.get().getUserEmail());
            }
        }

        List<OrganisationResource> organisations = organisationRestService.getOrganisationsByApplicationId(data.getApplication().getId()).getSuccess();

        Multimap<Long, ProcessRoleResource> organisationToProcessRole = index(processRoles, ProcessRoleResource::getOrganisationId);
        Map<Long, InviteOrganisationResource> organisationToInvite = inviteOrganisationResources.stream()
                .filter(organisationInvite -> organisationInvite.getOrganisation() != null)
                .collect(toMap(InviteOrganisationResource::getOrganisation, Function.identity()));

        List<ApplicationTeamOrganisationReadOnlyViewModel> organisationViewModels = organisations.stream()
                .map(organisation -> toOrganisationTeamViewModel(data.getApplication(), organisation, organisationToProcessRole.get(organisation.getId()), organisationToInvite.get(organisation.getId()), internalUser))
                .collect(toList());

        organisationViewModels.addAll(inviteOrganisationResources.stream()
                .filter(invite -> invite.getOrganisation() == null)
                .map(this::toInviteOrganisationTeamViewModel)
                .collect(toList()));

        return new ApplicationTeamReadOnlyViewModel(data, question, organisationViewModels, ktaProcessRole, ktaPhoneNumber, internalUser);
    }

}