
package de.ivu.elect.presentation.person.action;

import static java.util.Objects.nonNull;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import de.ivu.elect.business.person.entity.Personendaten;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.presentation.menu.AbstractActionEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;

/**
 *  on 06.11.2015.
 */
public abstract class AbstractPersonendatenRowDblSelectAction<P extends Personendaten> extends AbstractActionEntry<P> {

  private static final long serialVersionUID = 1L;

  public AbstractPersonendatenRowDblSelectAction() {
    super();
  }

  @Override
  public UseCasePermission getRequiredPermission() {
    return UseCasePermission.OHNE;
  }

  @Override
  public AbstractGebietSecurity getGebietSecurity() {
    return getEinsehenGebietSecurity();
  }

  @Override
  public boolean isDisabledInternal(@Nonnull P personendaten) {
    return resolvePersonenKonfliktEditAction(personendaten) == null && resolveEditAction(personendaten) == null;
  }

  @Override
  public boolean isDisabledInternalOnRowDblselect(@Nonnull P entity) {
    return false;
  }

  @Nullable
  @Override
  public String doIt(P personendaten) {
    AbstractActionEntry<P> personenKonfliktEditAction = resolvePersonenKonfliktEditAction(personendaten);

    if (nonNull(personenKonfliktEditAction) && personenKonfliktEditAction.isActionPossible(personendaten)) {
      return personenKonfliktEditAction.action(personendaten);
    }

    AbstractActionEntry<P> editAction = resolveEditAction(personendaten);

    if (nonNull(editAction) && editAction.isActionPossible(personendaten)) {
      return editAction.action(personendaten);
    }

    return null;
  }

  protected abstract AbstractActionEntry<P> resolveEditAction(P personendaten);

  protected abstract AbstractPersonendatenPersonenKonfliktAufloesenAction<P> resolvePersonenKonfliktEditAction(P personendaten);

  @Override
  public boolean isFireChangeEvent() {
    return false;
  }

  @Override
  public void fireChangeEvent() {
    // Do nothing.
  }

  @Override
  public String getLabelKey() {
    return "entity.edit.action.edit";
  }

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