package de.ivu.elect.core.exchange.internet;

import de.ivu.elect.business.internet.control.InternetWidgetCustomDataFactory;
import de.ivu.elect.business.internet.entity.InternetCustomDataCreateContext;
import de.ivu.elect.business.internet.entity.InternetWidgetCustomData;
import de.ivu.elect.business.internet.entity.InternetWidgetTyp;

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

@RequestScoped // ELECTMUC-1963
public class CoreInternetWidgetCustomDataFactory implements InternetWidgetCustomDataFactory {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient Instance<CoreInternetWidgetTypHandler> handlers;

  @Override
  public InternetWidgetCustomData createCustomData(InternetWidgetTyp internetWidgetTyp, InternetCustomDataCreateContext createContext) {
    for (CoreInternetWidgetTypHandler handler : handlers) {
      if (internetWidgetTyp.equals(handler.getInternetWidgetTyp())) {
        return handler.createCustomData(createContext);
      }
    }

    throw new IllegalStateException("Kein Handler für: " + internetWidgetTyp);
  }

  @Override
  public boolean isSupported(InternetWidgetTyp internetWidgetTyp) {
    for (CoreInternetWidgetTypHandler handler : handlers) {
      if (internetWidgetTyp.equals(handler.getInternetWidgetTyp())) {
        return true;
      }
    }
    return false;
  }
}
