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

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import de.ivu.elect.jee.presentation.tooltip.TooltipEntry;

public class StatusEntry {

  public static final String TOOLTIP_LINE_SEPARATOR = "<br />";

  public static final Pattern LINE_BREAK_PATTERN = Pattern.compile(TOOLTIP_LINE_SEPARATOR);

  private String label = null;

  private OutputSeverity outputSeverity = OutputSeverity.SUCCESS;

  private List<TooltipEntry> statusInformationen = new ArrayList<>();

  private List<TooltipEntry> tooltipInformationen = new ArrayList<>();

  public StatusEntry() {
    // Standard-Konstruktor
  }

  public StatusEntry(String label) {
    setLabel(label);
  }

  public StatusEntry(String label, OutputSeverity outputSeverity) {
    this(label);
    setSeverity(outputSeverity);
  }

  public String getLabel() {
    return label;
  }

  public void setLabel(String label) {
    this.label = label;
  }

  public OutputSeverity getSeverity() {
    return outputSeverity;
  }

  public void setSeverity(OutputSeverity outputSeverity) {
    this.outputSeverity = nullToNone(outputSeverity);
  }

  private OutputSeverity nullToNone(OutputSeverity outputSeverity) {
    if (outputSeverity == null) {
      return OutputSeverity.NONE;
    }
    return outputSeverity;
  }

  public List<TooltipEntry> getStatusInformationen() {
    return statusInformationen;
  }

  public void setStatusInformationen(List<TooltipEntry> statusInformationen) {
    this.statusInformationen = statusInformationen;
  }

  public void addStatusInformation(Object value) {
    addStatusInformation(null, value);
  }

  public void addStatusInformation(String label, Object value) {
    statusInformationen.add(new TooltipEntry(label, value));
  }

  public boolean isInformationenRendered() {
    return statusInformationen != null;
  }

  public List<TooltipEntry> getTooltipInformationen() {
    return tooltipInformationen;
  }

  public void setTooltipInformationen(List<TooltipEntry> tooltipInformationen) {
    this.tooltipInformationen = tooltipInformationen;
  }

  public void addTooltipInformation(Object value) {
    addTooltipInformation(null, value);
  }

  public void addTooltipInformation(String label, Object value) {
    tooltipInformationen.add(new TooltipEntry(label, value));
  }

  public boolean isSeparator() {
    return false;
  }

  public boolean isError() {
    return getSeverity().isError();
  }

  public boolean isWarn() {
    return getSeverity().isWarn();
  }

  public boolean isInfo() {
    return getSeverity().isInfo();
  }

  public boolean isSuccess() {
    return getSeverity().isSuccess();
  }

  public boolean isNichtVorhanden() {
    return getSeverity().isEmpty();
  }

  public String getTooltipInformationenAsString() {
    StringBuilder sb = new StringBuilder();
    for (TooltipEntry tooltipEntry : getTooltipInformationen()) {
      if (tooltipEntry.isRendered()) {
        for (String splitted : LINE_BREAK_PATTERN.split(tooltipEntry.getLabelAndValueAsString())) {
          if (sb.length() != 0) {
            sb.append("\n");
          }
          sb.append(splitted);
        }
      }
    }
    return sb.toString();
  }
}
