
package de.ivu.elect.presentation.dokumente;

import java.util.Arrays;
import java.util.Optional;

import javax.annotation.Nullable;

import de.ivu.elect.business.datei.entity.DateiExportPK;
import de.ivu.elect.presentation.exchange.ExportViewResult;

public class DokumentErzeugenResult implements ExportViewResult {

  private final Optional<DateiExportPK> dateiExport;

  private final Exception ex;

  private final String targetViewId;

  private final String msgKey;

  private final Object[] params;

  private DokumentErzeugenResult(DateiExportPK dateiExport, @Nullable Exception ex, @Nullable String targetViewId, @Nullable String msgKey, Object... params) {
    super();

    this.dateiExport = Optional.ofNullable(dateiExport);
    this.ex = ex;

    this.targetViewId = targetViewId;

    this.msgKey = msgKey;
    this.params = params;
  }

  @Override
  public Optional<DateiExportPK> getDateiExport() {
    return dateiExport;
  }

  public boolean hasDokument() {
    return getDateiExport().isPresent();
  }

  @Nullable
  public DateiExportPK getDokument() {
    return getDateiExport().orElse(null);
  }

  public boolean hasException() {
    return getException() != null;
  }

  @Nullable
  public Exception getException() {
    return ex;
  }

  public String getTargetViewId() {
    return targetViewId;
  }

  public String getMsgKey() {
    return msgKey;
  }

  public Object[] getParams() {
    return params;
  }

  @Override
  public String toString() {
    return "DokumentSynchronErzeugenResult{" + "hasDokument=" + hasDokument() + ", dateiExport=" + dateiExport + ", hasException=" + hasException() + ", ex=" + ex + ", targetViewId='" + targetViewId
        + '\'' + ", msgKey='" + msgKey + '\'' + ", params=" + Arrays.toString(params) + '}';
  }

  public static DokumentErzeugenResult success(DateiExportPK dateiExport) {
    return new DokumentErzeugenResult(dateiExport, null, null, null);
  }

  public static DokumentErzeugenResult error(Exception ex) {
    return new DokumentErzeugenResult(null, ex, null, null);
  }

  public static DokumentErzeugenResult error(String targetViewId, String msgKey, Object... params) {
    return new DokumentErzeugenResult(null, null, targetViewId, msgKey, params);
  }
}
