
package de.ivu.elect.presentation.dokumente;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.dokumente.boundary.DokumentVorlageOpenOfficeTest;
import de.ivu.elect.business.dokumente.boundary.DokumentVorlageOpenOfficeTestResult;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@RequestScoped
@OnlineHilfeId("377")
@Named(DokumentVorlageOpenOfficeTestView.COMPONENT_NAME)
@View(viewId = DokumentVorlageOpenOfficeTestView.VIEW_ID, useCase = UseCasePermission.DOKUMENT_VORLAGE_SEARCH)
public class DokumentVorlageOpenOfficeTestView implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "dokumentVorlageOpenOfficeTestView";

  public static final String VIEW_ID = "/dokumente/dokument-vorlage-open-office-test.xhtml";

  @Inject
  private transient DokumentVorlageOpenOfficeTest dokumentVorlageOpenOfficeTest;

  @Inject
  transient DokumentGenerierenProgressMonitor abstractProgressMonitor;

  private transient DokumentVorlageOpenOfficeTestResult dokumentVorlageOpenOfficeTestResult;

  public DokumentVorlageOpenOfficeTestResult getDokumentVorlageOpenOfficeTestResult() {
    if (dokumentVorlageOpenOfficeTestResult == null) {
      abstractProgressMonitor.reset();
      dokumentVorlageOpenOfficeTestResult = dokumentVorlageOpenOfficeTest
          .isOpenOfficeAvailable(abstractProgressMonitor);
    }
    return dokumentVorlageOpenOfficeTestResult;
  }

  public boolean isOpenOfficeErreichbar() {
    return getDokumentVorlageOpenOfficeTestResult().isConnectionPossible();
  }

  public boolean isOpenOfficeKonvertierungMoeglich() {
    return getDokumentVorlageOpenOfficeTestResult().isConversionPossible();
  }

  public Throwable getOpenOfficeInvokationException() {
    return getDokumentVorlageOpenOfficeTestResult().getThrowable();
  }

  public String getOpenOfficeInvokationExceptionMessage() {
    return getLocalizedMessageOrDefault(getOpenOfficeInvokationException());
  }

  public String getOpenOfficeInvokationExceptionCauses() {
    return getRawCausesOrDefault(getOpenOfficeInvokationException());
  }

  private String getLocalizedMessageOrDefault(Throwable cause) {
    if (cause == null) {
      return "";
    }

    return cause.getLocalizedMessage();
  }

  private String getRawCausesOrDefault(Throwable cause) {
    return addRawCauses(new StringBuilder(), cause).toString();
  }

  private StringBuilder addRawCauses(StringBuilder result, Throwable cause) {
    if (cause != null) {
      result.append(cause);
      if (cause.getCause() != null) {
        result.append(System.lineSeparator());
        addRawCauses(result, cause.getCause());
      }
    }
    return result;
  }

  public List<String> getProgressMessages() {
    return getDokumentVorlageOpenOfficeTestResult().getMessages();
  }
}
