
package de.ivu.elect.jee.presentation.dynamicform;

import java.io.ByteArrayInputStream;

import javax.faces.convert.Converter;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class ReadOnlyImageProperty extends AbstractProperty {

  private final String label;

  private final byte[] content;

  private final String mimeType;

  public ReadOnlyImageProperty(String label, byte[] content, String mimeType) {
    this.label = label;
    this.content = content;
    this.mimeType = mimeType;
  }

  @Override
  public PropertyType getDefaultType() {
    return PropertyType.IMAGE_READONLY;
  }

  @Override
  protected boolean hasDefaultValue() {
    return false;
  }

  @Override
  public boolean hasValue() {
    return content != null && content.length > 0;
  }

  @Override
  public String getDefaultLabel() {
    return label;
  }

  @Override
  public boolean getDefaultRequired() {
    return false;
  }

  @Override
  public Converter getDefaultConverter() {
    return null;
  }

  @Override
  public boolean isReadonly() {
    return true;
  }

  public StreamedContent getContent() {
    return DefaultStreamedContent.builder()
        .stream(() -> new ByteArrayInputStream(content))
        .contentType(mimeType)
        .build();
  }

}
