package de.ivu.elect.business.internet.control;

import java.io.Serializable;
import java.util.Set;
import java.util.stream.Collectors;

import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.inject.Inject;

import de.ivu.elect.business.wahl.control.WahlParameterGeaendertEvent;
import de.ivu.elect.business.wahl.entity.WahlParameterWerte;
import de.ivu.elect.jee.faces.FacesMessageUtil;

public class InternetPreasentationUploadServiceWahlParameterGeaendertEventListener implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetPreasentationUploadServiceSupported internetPreasentationUploadServiceSupported;

  public void listen(@Observes(notifyObserver = Reception.ALWAYS) WahlParameterGeaendertEvent wahlParameterGeaendertEvent) {
    if (WahlParameterWerte.PREASENTATIION_UPLOAD_SERVICE_TYP.equals(wahlParameterGeaendertEvent.getWahlParameterWerte())) {
      String neuerWert = wahlParameterGeaendertEvent.getNeuerWert();

      if (!getSupportedTypen().contains(neuerWert)) {
        FacesMessageUtil.addErrorMessage("internet.upload.service.not.supported");
      }
    }
  }

  public Set<String> getSupportedTypen() {
    return internetPreasentationUploadServiceSupported.getSupportedTypen().stream().map(Enum::name).collect(Collectors.toSet());
  }


}
