
package de.ivu.elect.presentation.system.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.system.boundary.BackgroundServiceStatusErmittler;
import de.ivu.elect.business.system.entity.BackgroundServiceStatus;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
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.presentation.table.DefaultColumnEntry;

@Named(BackgroundServiceStatusColumnBuilder.COMPONENT_NAME)
public class BackgroundServiceStatusColumnBuilder extends AbstractColumnBuilder<BackgroundServiceStatus> {

  public static final String COMPONENT_NAME = "backgroundServiceStatusColumnBuilder";

  private static final long serialVersionUID = 1L;

  @Inject
  private transient BackgroundServiceStatusErmittler backgroundServiceStatusErmittler;

  private ColumnEntry<BackgroundServiceStatus> quelleColumn;

  private ColumnEntry<BackgroundServiceStatus> runningColumn;

  private ColumnEntry<BackgroundServiceStatus> runningCountColumn;

  private ColumnEntry<BackgroundServiceStatus> enabledColumn;

  private ColumnEntry<BackgroundServiceStatus> todoColumn;

  private ColumnEntry<BackgroundServiceStatus> invocationColumn;

  private ColumnEntry<BackgroundServiceStatus> errorColumn;

  private ColumnEntry<BackgroundServiceStatus> timeColumn;

  private ColumnEntry<BackgroundServiceStatus> eventColumn;

  public BackgroundServiceStatusColumnBuilder() {
    super();
  }

  @Override
  protected void initColumns() {
    Set<SystemLogQuelle> events = backgroundServiceStatusErmittler.getBackgroundServiceEvents();

    quelleColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-quelle", getMessages(), "background.service.quelle", ColumnType.ENUM, BackgroundServiceStatus::getSystemLogQuelle);
    runningColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-running", getMessages(), "background.service.running", ColumnType.BOOLEAN, BackgroundServiceStatus::isLaeuftGerade);
    runningCountColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-runningCount", getMessages(), "background.service.running.count", ColumnType.LONG, BackgroundServiceStatus::getRunningCount);
    enabledColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-enabled", getMessages(), "background.service.enabled", ColumnType.BOOLEAN, BackgroundServiceStatus::isAktiviert);
    todoColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-todo", getMessages(), "background.service.todo.count", ColumnType.LONG, BackgroundServiceStatus::getAnzahlTodoCheck);
    invocationColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-invocation", getMessages(), "background.service.invocation.count", ColumnType.LONG,
        BackgroundServiceStatus::getAnzahlAufrufe);
    errorColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-error", getMessages(), "background.service.error.count", ColumnType.LONG, BackgroundServiceStatus::getAnzahlFehler);
    timeColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-time", getMessages(), "background.service.invocation.time.ms", ColumnType.LONG,
        BackgroundServiceStatus::getZeitInMsInsgesamt);
    eventColumn = new DefaultColumnEntry<>("BackgroundServiceStatusColumnBuilder-event", getMessages(), "background.service.event.possible", ColumnType.BOOLEAN,
        s -> events.contains(s.getSystemLogQuelle()));

  }

  @Override
  protected List<ColumnEntry< ? super BackgroundServiceStatus>> getColumnEntriesSorted() {
    List<ColumnEntry< ? super BackgroundServiceStatus>> result = new ArrayList<>();
    result.add(quelleColumn);
    result.add(runningColumn);
    result.add(runningCountColumn);
    result.add(eventColumn);
    result.add(enabledColumn);
    result.add(todoColumn);
    result.add(invocationColumn);
    result.add(errorColumn);
    result.add(timeColumn);
    return result;
  }

  @Override
  public boolean isColumnsSortableByValue() {
    return false; // funktioniert nicht...
  }
}
