package de.ivu.elect.business.system.control.status;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.konfiguration.boundary.KonfigurationExtendedDAO;
import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationPK;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.system.entity.SystemStatusOptionGroup;
import de.ivu.elect.jee.presentation.status.StatusEntry;
import de.ivu.elect.jee.presentation.status.StatusOptionType;


import static java.util.stream.Collectors.toList;

/**
 *  on 14.07.2017.
 */
public class SystemparameterStatusOptionResolver extends AbstractSystemStatusOptionResolver {

  @Inject
  private transient CurrentCustomer currentCustomer;

  public static final StatusOptionType SYSTEM_PARAMETER = new StatusOptionType() {
    @Override
    public boolean isLogWarning() {
      return true;
    }
  };

  @Inject
  private transient KonfigurationExtendedDAO konfigurationExtendedDAO;

  @Override
  public SystemStatusOptionGroup getSystemStatusOptionGroup() {
    return SystemStatusOptionGroup.SYSTEM_PARAMETER;
  }


  @Override
  public StatusEntry resolveSystemStatusOption(SystemStatusOption statusOption) {
    if (statusOption.getPayload() == null) {
      if (konfigurationExtendedDAO.findByMandant(statusOption.getMandant()).stream().anyMatch(k -> !k.isValid())) {
        return error("status.systemparameter.fehlerhaft.vorhanden");
      }
      return ok("status.systemparameter.ok");
    }

    KonfigurationPK pk = (KonfigurationPK) statusOption.getPayload();
    Konfiguration konfiguration = konfigurationExtendedDAO.findByPrimaryKey(pk);

    List<ValidatorException> validate = konfiguration.validate();
    if (validate.isEmpty()) {
      return ok("status.systemparameter.einzeln.ok", konfiguration.getName(messages));
    }

    for (ValidatorException validatorException : validate) {
      if (validatorException.getFacesMessage().getSeverity().getOrdinal() >= FacesMessage.SEVERITY_ERROR.getOrdinal()) {
        return error("status.systemparameter.einzeln.fehler", konfiguration.getName(messages), validatorException.getFacesMessage().getSummary());
      }
    }
    return warn("status.systemparameter.einzeln.fehler", konfiguration.getName(messages), validate.get(0).getFacesMessage().getSummary());
  }

  @Override
  protected boolean isAccept(Mandant mandant) {
    if (currentCustomer.hasUseCasePermission(UseCasePermission.DASHBOARD_ANZEIGEN)) {
      return mandant.getArt().isSystemMandant(); // Für Kundenmandanten im Dashboard anzeigen
    }
    return true;
  }

  @Override
  protected List<SystemStatusOption> getStatusOptionsInternal(Mandant mandant) {
    List<SystemStatusOption> result = new ArrayList<>();
    result.addAll(
        konfigurationExtendedDAO.findByMandant(mandant).stream().filter(k -> !k.isValid()).map(Konfiguration::getPk).map(p -> new SystemStatusOption(SYSTEM_PARAMETER, p, mandant))
            .collect(toList()));
    if (result.isEmpty()){
      result.add(new SystemStatusOption(SYSTEM_PARAMETER, mandant)); // Alles OK Meldung ergänzen
    }
    return result;

  }
}
