package de.ivu.elect.presentation.menu;

import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.wahl.boundary.WahlOderWahlterminSessionSelect;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;

public abstract class AbstractExternerLinkMenuItemEntry extends AbstractMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  protected transient WahlOderWahlterminSessionSelect wahlOderWahlterminSessionSelect;

  @Override
  protected boolean isDisabledInternal() {
    return false;
  }

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

  @Override
  protected List<String> getAdditionalTopicViewIds() {
    return Collections.singletonList(""); // ignorieren
  }

  private boolean isWahlOderWahlterminSelected() {
    return wahlOderWahlterminSessionSelect.isSelected();
  }

  @Override
  public boolean isRendered() {
    return isWahlOderWahlterminSelected() && isUrlSet() && isLabelSet() && hasUseCasePermission();
  }

  protected abstract boolean hasUseCasePermission();

  private boolean isLabelSet() {
    return StringUtils.isNotBlank(getLabelForLink());
  }

  protected abstract String getLabelForLink();

  private boolean isUrlSet() {
    return StringUtils.isNotBlank(getUrlForLink());
  }

  protected abstract String getUrlForLink();

  @Override
  protected String getLabel() {
    return getMessage(getLabelKey(), getLabelForLink());
  }

  @Override
  public String getLabelKey() {
    return "externer.link.label.key";
  }

  @Override
  protected boolean isShowBlockUi() {
    return false; // wird in neuem Browserfenster geöffnet, daher kein BlockUi
  }

  @Override
  public String getTargetViewId() {
    return getUrlForLink();
  }

  @Override
  protected String buildUrlForTargetViewId(String viewID) {
    return viewID;
  }

  @Nullable
  @Override
  public String getTarget() {
    return TARGET_EXTERN;
  }

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

}
