
package de.ivu.elect.presentation.search.menu;

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.io.Serializable;

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

import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.ViewHavingSuchparameterSpeicherbar;

@RequestScoped
public class CurrentViewViewHavingSuchparameterSpeicherbar implements Serializable {

  private static final long serialVersionUID = 1L;

  @View
  @Inject
  private transient Instance<ViewHavingSuchparameterSpeicherbar> views;

  public CurrentViewViewHavingSuchparameterSpeicherbar() {
    super();
  }

  @Nullable
  public ViewHavingSuchparameterSpeicherbar getCurrentViewInstance() {
    String currentViewId = FacesContextUtil.getCurrentViewId();

    if (isBlank(currentViewId)) {
      return null;
    }

    for (ViewHavingSuchparameterSpeicherbar view : views) {
      View viewAnnotation = view.getClass().getAnnotation(View.class);

      if (currentViewId.equals(viewAnnotation.viewId())) {
        return view;
      }
    }

    return null;
  }
}
