package de.ivu.elect.presentation.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.dynamictable.AbstractColumnBuilder;
import de.ivu.elect.jee.presentation.dynamictable.ColumnEntry;
import de.ivu.elect.jee.presentation.dynamictable.ColumnType;
import de.ivu.elect.jee.presentation.status.StatusEntry;
import de.ivu.elect.jee.presentation.tooltip.TooltipEntry;
import de.ivu.elect.presentation.system.table.DefaultStatusEntryColumn;


import static java.util.stream.Collectors.toList;

public class StatusExporterColumnBuilder extends AbstractColumnBuilder<StatusEntry> {

  @Inject
  protected transient Messages messages;

  private DefaultStatusEntryColumn labelColumn;
  private DefaultStatusEntryColumn severityColorColumn;
  private DefaultStatusEntryColumn severityColumn;
  private DefaultStatusEntryColumn statusinformationColumn;
  private DefaultStatusEntryColumn tooltipColumn;

  @Override
  protected void initColumns() {
    labelColumn = new DefaultStatusEntryColumn<>("labelColumn", messages, "status.entry.label", ColumnType.STRING, StatusEntry::getLabel);
    severityColorColumn = new DefaultStatusEntryColumn<>("severityColorColumn", messages, "status.entry.serverity.color", ColumnType.COLOR,
        s -> s.getSeverity().getColor());
    severityColumn = new DefaultStatusEntryColumn<>("severityColumn", messages, "status.entry.serverity.label", ColumnType.ENUM, StatusEntry::getSeverity);
    statusinformationColumn = new DefaultStatusEntryColumn<>("statusinformationColumn", messages, "status.entry.statusinformation", ColumnType.COLLECTION,
        s -> s.getStatusInformationen().stream().map(TooltipEntry::getLabelAndValueAsString).flatMap(tt -> Arrays.stream(
            StatusEntry.LINE_BREAK_PATTERN.split(tt))).collect(toList()));
    tooltipColumn = new DefaultStatusEntryColumn<>("tooltipColumn", messages, "status.entry.tooltip", ColumnType.COLLECTION,
        s -> s.getTooltipInformationen().stream().map(TooltipEntry::getLabelAndValueAsString)
            .flatMap(tt -> Arrays.stream(StatusEntry.LINE_BREAK_PATTERN.split(tt))).collect(toList()));
  }

  private List additionalColumns = new ArrayList<>();

  public void setAdditionalColumns(List<DefaultStatusEntryColumn<? extends StatusEntry>> additionalColumns) {
    this.additionalColumns = additionalColumns;
  }

  @Override
  protected List<ColumnEntry<? super StatusEntry>> getColumnEntriesSorted() {
    List<ColumnEntry<? super StatusEntry>> result = new ArrayList<>();
    result.addAll(getDefaultColumns());
    result.addAll(additionalColumns);
    return result;
  }

  protected List<ColumnEntry<? super StatusEntry>> getDefaultColumns() {
    List<ColumnEntry<? super StatusEntry>> result = new ArrayList<>();
    result.add(labelColumn);
    result.add(severityColorColumn);
    result.add(severityColumn);
    result.add(statusinformationColumn);
    result.add(tooltipColumn);
    return result;
  }
}
