
package de.ivu.elect.presentation.exchange;

import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.datei.entity.Datei;
import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.exchange.control.FileFormat;
import de.ivu.elect.jee.faces.Messages;

@Named(FileFormatFormatter.COMPONENT_NAME)
public class FileFormatFormatter {

  public static final String COMPONENT_NAME = "fileFormatFormatter";

  @Inject
  private transient Messages messages;

  public FileFormatFormatter() {
    super();
  }

  public String format(Datei datei) {
    return format(datei.getFileFormat());
  }

  public String format(FileFormat fileFormat) {
    String msgKey = "FileFormatFormatter.format";

    Optional<FileType> fileTypeArchived = fileFormat.getFileTypeArchived();

    if (fileTypeArchived.isPresent()) {
      msgKey += ".archived";
    }

    return getMessage(msgKey, getMessage(fileFormat.getName()), fileFormat.getFileType().name(), fileTypeArchived.map(FileType::name).orElse(""));
  }

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