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

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.scope.ConversationGroup;

import de.ivu.elect.business.konfiguration.boundary.AbstractKonfigurationSearchParameters;
import de.ivu.elect.business.konfiguration.boundary.KonfigurationSearchParameters;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.system.control.status.SystemStatusOption;
import de.ivu.elect.business.system.control.status.SystemparameterStatusOptionResolver;
import de.ivu.elect.jee.presentation.status.StatusEntry;
import de.ivu.elect.presentation.konfiguration.KonfigurationSearchView;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;

@RequestScoped
public class KonfigurationUngueltigSearchMenuItemEntry extends AbstractMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient SystemparameterStatusOptionResolver systemparameterStatusOptionResolver;

  @Override
  public boolean isRendered() {
    return hasUseCasePermission(UseCasePermission.KONFIGURATION_SEARCH);
  }

  @Inject
  @ConversationGroup(AbstractKonfigurationSearchParameters.class)
  protected transient KonfigurationSearchParameters searchParameters;

  @Override
  public String getLabelKey() {
    return "konfiguration.ungueltig.search.menu.item.label";
  }

  @Override
  public String getTargetViewId() {
    return null; // action ausführen
  }

  @Override
  public String doAction() {
    searchParameters.searchUngueltigFuerMandant(getPrincipal().getMandant());
    return KonfigurationSearchView.VIEW_ID;
  }

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

  private boolean isUngueltigeWertVorhanden() {
    StatusEntry statusEntry = systemparameterStatusOptionResolver.resolveSystemStatusOption(new SystemStatusOption(SystemparameterStatusOptionResolver.SYSTEM_PARAMETER, getPrincipal().getMandant()));
    return !statusEntry.isSuccess();
  }

  @Override
  public String getIcon() {
    return getIcons().getIconWarning();
  }

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

}
