
package de.ivu.elect.business.kandidatur.entity;

import java.io.Serializable;

import javax.inject.Inject;

import de.ivu.elect.jee.faces.Messages;

import static org.apache.commons.lang3.StringUtils.*;

public class KandidaturAttributePraesentationFormatter implements NamePraesentationFormatter, Serializable {

  private static final long serialVersionUID = 1L;

  public static final String NAME_PRAESENTATION_FORMAT_VORNAME_SEPARATOR = ",";

  private static final String NAME_PRAESENTATION_FORMAT_MSG_KEY = "entity.einzahl.KANDIDATUR.NAME_PRAESENTATION_FORMAT";
  private static final String KUENSTLERNAME_FORMAT_MSG_KEY = "entity.einzahl.KANDIDATUR.NAME_PRAESENTATION_FORMAT.KUENSTLERNAME_FORMAT";

  @Inject
  private transient Messages messages;

  public KandidaturAttributePraesentationFormatter() {
    super();
  }

  public KandidaturAttributePraesentationFormatter(Messages messages) {
    super();

    this.messages = messages;
  }

  @Override
  public String formatNamePraesentation(String titel, String rufname, String vornamen, String familienname, String familiennameBestandteil, String kuenstlername) {
    String vornameAnzuzeigen = defaultIfBlank(vornamen, rufname); // WASDE-1641

    String kuenstlernameAnzuzeigen = "";
    if (isNotBlank(kuenstlername)) {
      kuenstlernameAnzuzeigen = format(KUENSTLERNAME_FORMAT_MSG_KEY, kuenstlername);
    }

    return format(NAME_PRAESENTATION_FORMAT_MSG_KEY, NAME_PRAESENTATION_FORMAT_VORNAME_SEPARATOR, titel, vornameAnzuzeigen, familienname, familiennameBestandteil, kuenstlernameAnzuzeigen);
  }

  private String format(String msgKey, String... params) {
    Object[] paramsNormalized = new String[params.length];

    for (int i = 0; i < params.length; i++) {
      paramsNormalized[i] = trimToEmpty(params[i]);
    }

    return normalizeSpace(getMessage(msgKey, paramsNormalized));
  }

  private String getMessage(String msgKey, Object... params) {
    return getMessages().getMessageForMessageKey(msgKey, params);
  }

  private Messages getMessages() {
    return messages;
  }
}
