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

import javax.enterprise.inject.Specializes;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.wahl.entity.NlWahlart;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahlvorschlag.boundary.WahlvorschlagExtendedDAO;
import de.ivu.elect.jee.faces.FacesContextUtil;

@Specializes
@Named(KandidaturSearchMenuItemEntry.COMPONENT_NAME)
public class NlWasKandidaturSearchMenuItemEntry extends KandidaturSearchMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient WahlvorschlagExtendedDAO wahlvorschlagExtendedDAO;

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Override
  public String getLabelKey() {
    return "nl.wahlvorschlag.search.menu.item.label"; // ELECT-10301
  }

  @Override
  public String getIcon() {
    return "fa fa-user-times";
  }

  @Override
  public String doAction() {
    FacesContextUtil.redirectToView(getTargetViewId());
    return super.doAction();
  }

  @Override
  public boolean isRendered() {
    return super.isRendered() && isWahlvorschlaegeVorhanden() && isCsb() /*ELECT-11341*/;
  }

  private boolean isCsb() {
    Wahl selectedWahl = getSelectedWahl();
    if (selectedWahl == null) {
      return false;
    }
    return NlWahlart.isWasCSB(selectedWahl, currentCustomer.getCustomer());
  }

  private boolean isWahlvorschlaegeVorhanden() {
    Wahl selectedWahl = getSelectedWahl();
    if (selectedWahl == null) {
      return false;
    }
    return wahlvorschlagExtendedDAO.findByCountWahlvorschlagByWahl(selectedWahl) > 0;
  }
}
