
package de.ivu.elect.presentation.konfiguration.table;

import java.util.List;

import javax.annotation.Nullable;
import javax.faces.validator.ValidatorException;

import de.ivu.elect.business.konfiguration.entity.CustomParameter;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.Icons;
import de.ivu.elect.jee.presentation.dynamictable.ColumnType;
import de.ivu.elect.presentation.table.DefaultColumnEntry;

public class CustomParameterWertValidColumn<E extends CustomParameter< ? >> extends DefaultColumnEntry<E> {

  private static final long serialVersionUID = 1L;

  private final Icons icons;

  public CustomParameterWertValidColumn(Messages messages, Icons icons) {
    super("CustomParameter-WertValid", messages, "custom.parameter.wert.valid", ColumnType.STRING, parameter -> {
      List<ValidatorException> validate = parameter.validate();
      if (validate.isEmpty()) {
        return "";
      }
      return validate.get(0).getFacesMessage().getSummary();
    });

    this.icons = icons;
  }

  @Override
  public boolean isIconPresent() {
    return true;
  }

  @Override
  public boolean isIconRendered(E parameter) {
    return true;
  }

  @Override
  @Nullable
  public String getIcon(E parameter) {
    if (parameter.isValid()) {
      return icons.getIconOk();
    }

    return icons.getIconAlert();
  }
}
