
package de.ivu.elect.presentation.gruppe.menu;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.boundary.WahlSessionSelect;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;

public abstract class AbstractGruppeMenuItemEntry extends AbstractMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient WahlSessionSelect wahlSessionSelect;

  @Inject
  private transient Instance<GruppeMenuEnabled> gruppeMenuEnableds;

  public AbstractGruppeMenuItemEntry() {
    super();
  }

  @Override
  public boolean isRendered() {
    return hasUseCasePermission(getRequiredUseCasePermission()) && isGruppeMenuEnabled();
  }

  private boolean isGruppeMenuEnabled() {
    for (GruppeMenuEnabled gruppeMenuEnabled : gruppeMenuEnableds) {
      if (gruppeMenuEnabled.isRendered()){
        return true;
      }
    }
    return false;
  }

  protected abstract UseCasePermission getRequiredUseCasePermission();

  @Override
  protected AbstractGebietSecurity getGebietSecurity() {
    return KEINE_GEBIET_SECURITY;
  }

  @Override
  public boolean isDisabledInternal() {
    return !isEnabled();
  }

  private boolean isEnabled() {
    for (GruppeMenuEnabled gruppeMenuEnabled : gruppeMenuEnableds) {
      if (gruppeMenuEnabled.isRendered() && gruppeMenuEnabled.isEnabled()){
        return true;
      }
    }
    return false;
  }

  protected boolean isWahlSelected() {
    return wahlSessionSelect.isSelected();
  }

  protected Wahl getSelectedWahl() {
    return wahlSessionSelect.getEntity();
  }


}
