
package de.ivu.elect.dokumente.image;

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

import java.util.Locale;
import java.util.Optional;

import javax.annotation.Nullable;

import de.ivu.document.generator.template.BaseDocument;
import de.ivu.document.generator.template.annotations.DocumentMethod;
import de.ivu.document.generator.template.annotations.DocumentParam;
import de.ivu.document.generator.template.annotations.DocumentSimpleField;
import de.ivu.elect.business.image.entity.Bild;
import de.ivu.elect.dokumente.DokumentFormatter;

// ELECT-15373
public class BildDokument extends BaseDocument {

  private static final long serialVersionUID = 1L;

  @DocumentSimpleField("Ist-Vorhanden")
  public final boolean vorhanden;

  @DocumentSimpleField("Content-Base64 -> OOXML:<BildDokument>.draw(<widthInCm>, <heightInCm>)")
  public final String contentBase64;

  public final Optional<Double> aspectRatio;

  public BildDokument() {
    this(null);
  }

  public BildDokument(@Nullable Bild bild) {
    super();

    if (bild == null) {
      this.contentBase64 = "";

      this.aspectRatio = Optional.empty();
    } else {
      this.contentBase64 = bild.getContentBase64();

      this.aspectRatio = bild.getAspectRatio();
    }

    this.vorhanden = isNotBlank(contentBase64);
  }

  @DocumentMethod
  public String draw() {
    return draw(null, null);
  }

  @DocumentMethod
  public String draw(@Nullable @DocumentParam("\"widthInCm\"") Number widthInCm, @Nullable @DocumentParam("\"heightInCm\"") Number heightInCm) {
    return new DokumentFormatter(Locale.getDefault()).drawImageBase64(this, widthInCm, heightInCm);
  }
}
