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.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationWertGeaendertEvent;
import de.ivu.elect.jee.faces.FacesMessageUtil;

public class InternetPreasentationTypKonfigrationWertGeaendertEventListener implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetPreasentationTypSupported internetPreasentationTypSupported;

  public void listen(@Observes(notifyObserver = Reception.ALWAYS) KonfigurationWertGeaendertEvent konfigurationWertGeaendertEvent) {
    if (ElectKonfigurationWerte.PRAESENTATION_TYP.equals(konfigurationWertGeaendertEvent.getKey())) {
      String neuerWert = konfigurationWertGeaendertEvent.getNeuerWert();

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

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


}
