package de.ivu.elect.presentation.dashboard.entries;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.primefaces.model.menu.MenuModel;

import de.ivu.elect.jee.presentation.menu.AbstractMenuModelBuilder;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;

public abstract class AbstractWahlOderWahlterminDashboardEntry extends AbstractDashboardEntry {
  private static final long serialVersionUID = 1L;

  private static final long MAX_ANZAHL_AUSWAHL_BUTTONS = 10; // willkürlich

  @Override
  public String getLegend() {
    if (isSelected()) {
      return getMessage(getAusgewaehltKey());
    }
    return getMessage(getNichtAusgewaehltKey());

  }

  @Override
  public String getSplitButtonIconLabel() {
    if (isSelected()) {
      return getMessage(getWechselnKey());
    }
    return getMessage(getAuswaehlenKey());
  }

  protected abstract String getAuswaehlenKey();

  protected abstract String getWechselnKey();

  protected abstract String getAusgewaehltKey();

  protected abstract String getNichtAusgewaehltKey();

  protected abstract boolean isSelected();

  protected abstract AbstractMenuModelBuilder getSplitButtonModelBuilder();

  @Override
  public MenuModel getSplitButtonModel() {
    return getSplitButtonModelBuilder().getMenuModel();
  }

  @Override
  public boolean isSplitButtonRendered() {
    return super.isSplitButtonRendered() && getSplitButtonModelBuilder().isRendered() && !isWahlAuswahlAlsEinzelneButtons() && isELECT9098Fixed();
  }

  protected boolean isELECT9098Fixed() {
    return false;
  }

  private boolean isWahlAuswahlAlsEinzelneButtons() {
    return !isSelected();
  }

  @Override
  public List<AbstractMenuItemEntry> getMenuItemsFirst() {
    List<AbstractMenuItemEntry> result = new ArrayList<>();
    if (isWahlAuswahlAlsEinzelneButtons()) {
      result.addAll(getSplitButtonModelBuilder().listMenuEntries().stream().filter(listMenuEntry -> listMenuEntry instanceof AbstractMenuItemEntry)
          .map(listMenuEntry -> (AbstractMenuItemEntry) listMenuEntry).collect(Collectors.toList()));
    }
    return result;
  }

  @Override
  public boolean isLineBreakAfterFirstButtons() {
    return super.isLineBreakAfterFirstButtons() || isWahlAuswahlAlsEinzelneButtons();
  }
}
