package de.ivu.elect.jee.security;

import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.jee.cdi.InstanceFinder;

@RequestScoped
public class LoginServiceFinder implements InstanceFinder {

  private static final Logger LOG = LoggerFactory.getLogger(LoginServiceFinder.class);

  @Inject
  private transient Instance<LoginService> loginServices;

  @Inject
  private transient DefaultLoginService defaultLoginService;

  public LoginService getLoginService(String serviceNameToFind) {

    for (LoginService loginService : loginServices) {
      if (serviceNameToFind.equalsIgnoreCase(loginService.getServiceName())) {
        return loginService;
      }
    }


    LOG.error("Kein Login-Sevice mit dem Namen '" + serviceNameToFind + "' gefunden! Konfiguration anpassen! Verwende: " +defaultLoginService.getServiceName());
    return defaultLoginService;
  }

  public Set<String> getRegisterServiceNames() {
    Set<String> result = new HashSet<>();
    for (LoginService loginService : loginServices) {
      result.add(loginService.getServiceName());
    }

    return result;
  }

}
