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

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.system.control.SystemStatusOptionResolver;
import de.ivu.elect.business.system.entity.SystemStatusOptionGroup;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.status.OutputSeverity;
import de.ivu.elect.jee.presentation.status.StatusEntry;
import de.ivu.elect.jee.presentation.status.StatusOption;

/**
 *  on 26.05.2017.
 */
public abstract class AbstractSystemStatusOptionResolver implements SystemStatusOptionResolver {

  @Inject
  protected transient Messages messages;

  protected StatusEntry ok(String key, Object... params) {
    return new StatusEntry(messages.getMessageForMessageKey(key, params), OutputSeverity.SUCCESS);
  }

  protected StatusEntry error(String key, Object... params) {
    return new StatusEntry(messages.getMessageForMessageKey(key, params), OutputSeverity.ERROR);
  }

  protected StatusEntry warn(String key, Object... params) {
    return new StatusEntry(messages.getMessageForMessageKey(key, params), OutputSeverity.WARN);
  }

  @Override
  public boolean isSuitable(StatusOption statusOption) {
    if (statusOption instanceof SystemStatusOption) {
      return getStatusOptions(((SystemStatusOption) statusOption).getMandant()).contains(statusOption);
    }
    return false;
  }

  @Override
  public StatusEntry resolveStatus(StatusOption statusOption) {
    return resolveSystemStatusOption((SystemStatusOption) statusOption);
  }

  protected abstract StatusEntry resolveSystemStatusOption(SystemStatusOption systemStatusOption);

  @Override
  public List<SystemStatusOption> getStatusOptions(Mandant mandant) {
    if (isAccept(mandant)){
      return getStatusOptionsInternal(mandant);
    }
    return Collections.emptyList();
  }

  protected abstract boolean isAccept(Mandant mandant);

  protected abstract List<SystemStatusOption> getStatusOptionsInternal(Mandant mandant);

  @Override public boolean isResolveableByThorntail() {
    return getSystemStatusOptionGroup().isResolveableByThorntail();
  }
}
