/*
   * (non-Javadoc)
   *
   * @see
   * cc.kune.core.server.notifier.NotificationSender#send(cc.kune.core.server
   * .notifier.PendingNotification,
   * cc.kune.core.shared.dto.EmailNotificationFrequency)
   */
  @Override
  public void send(
      final PendingNotification notification, final EmailNotificationFrequency withFrequency) {
    final FormattedString subject = notification.getSubject().copy();
    final FormattedString body = notification.getBody();
    final NotificationType notifyType = notification.getNotifyType();
    final boolean forceSend = notification.isForceSend();
    final boolean isHtml = notification.isHtml();
    final String subjectPrefix = notification.getSubjectPrefix();
    final String subjectWithoutBra =
        subjectPrefix.equals(PendingNotification.SITE_DEFAULT_SUBJECT_PREFIX)
            ? siteName
            : subjectPrefix;
    subject.setTemplate(addBraquet(subjectWithoutBra) + subject.getTemplate());
    if (subject.shouldBeTranslated()) {
      // Translate per recipient language
      // final String subjectTranslation = i18n.tWithNT(user.getLanguage(),
      // subject.getTemplate(), "");
      // if (subjectTranslation != null) {
      // Right now commented because we are only testing
      // subject.setTemplate(subjectTranslation);
      // }
    }
    if (body.shouldBeTranslated()) {
      // final String bodyTranslation = i18n.tWithNT(user.getLanguage(),
      // body.getTemplate(), "");
      // if (bodyTranslation != null) {
      // Right now commented because we are only testing
      // body.setTemplate(bodyTranslation);
      // }
    }

    for (final Addressee user : notification.getDestProvider().getDest()) {
      final String username = user.getShortName();

      switch (notifyType) {
        case chat:
          // FIXME seems that html is not sending correctly... check server specs
          xmppManager.sendMessage(
              username, String.format("<b>%s</b>%s", subject.getString(), body.getString()));
          break;
        case email:
          if (forceSend
              || (user.isEmailVerified()
                  && noOnline(username)
                  && withFrequency == user.getFreq())) {
            // we'll send this notification if is mandatory or this user is not
            // only and has this freq configured
            mailService.send(
                subject,
                FormattedString.build(emailTemplate.replace("%s", body.getString())),
                isHtml,
                user.getAddress());
          }
          break;
        case wave:
          if (isHtml) {
            LOG.error("Wave html messages not supported yet");
          }
          waveService.createWave(
              subject.getString(), body.getString(), KuneWaveService.DO_NOTHING_CBACK, username);
          break;
      }
    }
  }