package de.ivu.elect.business.system.entity;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.stream.Collectors;

import de.ivu.elect.jee.entity.AbstractNoPersistentEntity;
import de.ivu.elect.jee.entity.EntityLongPK;
import de.ivu.elect.jee.entity.tk.EntityUK;
import de.ivu.elect.jee.faces.Messages;

public class Skript extends AbstractNoPersistentEntity<EntityLongPK, EntityUK> {

  private static final long serialVersionUID = 1L;

  boolean success;

  private String skript;

  private Object result;

  private SkriptConsoleLog log;

  public boolean isSuccess() {
    return success;
  }

  public void setSuccess(boolean success) {
    this.success = success;
  }

  public SkriptConsoleLog getLog() {
    return log;
  }

  public void setLog(SkriptConsoleLog log) {
    this.log = log;
  }

  public String getSkript() {
    return skript;
  }

  public void setSkript(String skript) {
    this.skript = skript;
  }

  public void resetResult() {
    result = null;
    log = null;
    success = false;
  }

  public Object getResult() {
    return result;
  }

  public void setResult(Object result) {
    this.result = result;
  }

  public String getOutput(Messages messages) {

    if (!isResultPresent()) {
      return "";
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss,SSS", Locale.GERMANY);
    return log.getEntries().stream()
        .map(entry -> simpleDateFormat.format(entry.getZeitpunkt()) + " [" + messages.lokalisiere(entry.getSeverity()) + "]: " + entry.getEntry() + "\n")
        .collect(Collectors.joining()) + "\nResult: " + result + "\n";
  }

  public boolean isResultPresent() {
    return log != null;
  }
}
