class FinanceLinksUtil {

    public Optional<String> financesLink(OrganisationResource organisation, List<ProcessRoleResource> processRoles, UserResource user, ApplicationResource application, CompetitionResource competition) {
        Optional<ProcessRoleResource> currentUserRole = getCurrentUsersRole(processRoles, user);

        UserResource authenticatedUser = userAuthenticationService.getAuthenticatedUser(httpServletUtil.request());
        if (authenticatedUser.isInternalUser() || authenticatedUser.getRoles().contains(STAKEHOLDER) || authenticatedUser.getRoles().contains(EXTERNAL_FINANCE)) {
            if (!application.isSubmitted()) {
                if (authenticatedUser.getRoles().contains(IFS_ADMINISTRATOR) || authenticatedUser.getRoles().contains(SUPPORT) || authenticatedUser.getRoles().contains(EXTERNAL_FINANCE)) {
                    return Optional.of(organisationIdInLink(application.getId(), organisation));
                }
            } else {
                return Optional.of(organisationIdInLink(application.getId(), organisation));
            }
        }

        if (currentUserRole.isPresent()) {
            if (applicantProcessRoles().contains(currentUserRole.get().getRole())) {
                if (competition.isKtp()) {
                    //All KTP users can see each others finances.
                    return Optional.of(organisationIdInLink(application.getId(), organisation));
                } else if (currentUserRole.get().getOrganisationId().equals(organisation.getId())) {
                    return Optional.of(applicantsOrganisationLink(application.getId()));
                }
            }

            if (currentUserRole.get().getRole().isKta()) {
                return Optional.of(organisationIdInLink(application.getId(), organisation));
            }

            CompetitionAssessmentConfigResource competitionAssessmentConfigResource = competitionAssessmentConfigRestService.findOneByCompetitionId(competition.getId()).getSuccess();

            if (assessorProcessRoles().contains(currentUserRole.get().getRole())
                    && DETAILED.equals(competitionAssessmentConfigResource.getAssessorFinanceView())) {
                return Optional.of(assessorLink(application, organisation));
            }
        }

        return Optional.empty();
    }

}