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

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

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

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

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.boundary.WahlConversationStore;
import de.ivu.elect.business.wahl.boundary.WahlSessionSelect;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.presentation.wahl.WahlEditView;

// ELECT-3532
public class WahlEditMenuItemEntry extends AbstractWahlMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private WahlSessionSelect wahlSelect;

  @Inject
  @ConversationGroup(WahlConversationStore.class)
  private WahlConversationStore wahlConversationStore;

  public WahlEditMenuItemEntry() {
    super();
  }

  @Override
  protected UseCasePermission getUseCasePermission() {
    return UseCasePermission.WAHL_EDIT;
  }

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

  private boolean isWahlSelected() {
    return wahlSelect.isSelected();
  }

  private Wahl getSelectedWahl() {
    return wahlSelect.getEntity();
  }

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

  @Override
  public String doAction() {
    if (isWahlSelected()) {
      wahlConversationStore.setEntityPK(getSelectedWahl().getPk());
    }

    return WahlEditView.VIEW_ID;
  }

  @Override
  protected List<String> getAdditionalTopicViewIds() {
    return Collections.singletonList(WahlEditView.VIEW_ID);
  }

  @Override
  protected String getLabel() {
    return getMessage(getLabelKey(), getMeta().getEinzahlMessage());
  }

  @Override
  public String getLabelKey() {
    return "entity.edit.menu.item.label";
  }

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