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

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

import de.ivu.elect.jee.presentation.menu.MenuEntry;

@RequestScoped
public class InterfacesPageMenuEntriesCollector implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient Instance<InterfacesPageMenuEntries> interfacesPageMenuEntries;

  public InterfacesPageMenuEntriesCollector() {
    super();
  }

  public List<MenuEntry> collectMenuEntries() {
    List<InterfacesPageMenuEntries> result = new ArrayList<>();

    for (InterfacesPageMenuEntries instance : interfacesPageMenuEntries) {
      result.add(instance);
    }

    return result.stream() //
        .filter(InterfacesPageMenuEntries::isAvailable)
        .sorted((one, another) -> another.getPriority() - one.getPriority()) // Höhere Priorität nach vorn!
        .flatMap(menuEntries -> menuEntries.collectMenuEntries().stream()) //
        .collect(toList());
  }
}
