package de.ivu.elect.presentation.security;

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

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.wahl.entity.NlWahlart;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.presentation.NlSystemProperties;

@Specializes
public class NlWasPrivatePublicKeyNeeded extends NlPrivatePublicKeyNeeded {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient NlSystemProperties nlSystemProperties;

  @Override
  public boolean isPrivateKeyNeeded(Mandant mandant) {
    return mandant != null && nlSystemProperties.getDefaultElectionLevel(mandant).isPrivateKeyNeeded();
  }

  @Override
  public boolean isPublicKeyNeeded(Mandant mandant, Wahl wahl) {
    return mandant != null && nlSystemProperties.getDefaultElectionLevel(mandant).isPublicKeyNeeded() && isWahlSupportsPublicKey(wahl);
  }

  private boolean isWahlSupportsPublicKey(Wahl wahl) {
    if (wahl == null) {
      return false;
    }
    return NlWahlart.valueOf(wahl).isPublicKeySopported();
  }
}
