package de.ivu.elect.presentation.email.form;

import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.jee.faces.FacesComponentUtil;
import de.ivu.elect.jee.faces.FacesMessageUtil;

import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.io.Serializable;

@RequestScoped
public class EmailLayoutPlatzhalterFacesValidator implements Validator, Serializable {

  private static final long serialVersionUID = 1L;

  @Override
  public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {
    if (o instanceof String) {
      String emailLayoutInhalt = (String) o;
      String inhaltPlatzhalter = EmailVorgang.EMAIL_LAYOUT_INHALT_PLATZHALTER;
      if (!emailLayoutInhalt.contains(inhaltPlatzhalter)) {
        throw new ValidatorException(FacesMessageUtil.createErrorMessage(facesContext, "email.layout.validator.platzhalter.fehlt.message",
            "email.layout.validator.platzhalter.fehlt.message", new Object[]{FacesComponentUtil.getLocalizedLabelOrId(uiComponent), o}));
      }
    }
  }
}
