
package de.ivu.elect.presentation.menu;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.jee.presentation.menu.AbstractMenuModelBuilder;
import de.ivu.elect.jee.presentation.menu.MenuEntry;
import de.ivu.elect.presentation.LayoutComponents;
import de.ivu.elect.presentation.admin.menu.AdminMainMenuEntry;
import de.ivu.elect.presentation.benutzerverwaltung.menu.NachrichtMainMenuEntry;
import de.ivu.elect.presentation.hilfe.menu.HilfeMainMenuEntry;
import de.ivu.elect.presentation.kalender.menu.KalenderMainMenuEntry;
import de.ivu.elect.presentation.security.menu.LogoutMainMenuEntry;
import de.ivu.elect.presentation.system.menu.SystemLogErrorMainMenuEntry;
import de.ivu.elect.presentation.system.menu.SystemLogInfoMainMenuEntry;
import de.ivu.elect.presentation.system.menu.SystemLogMainMenuEntry;
import de.ivu.elect.presentation.system.menu.SystemLogWarnMainMenuEntry;

@Named(MainServiceMenu.COMPONENT_NAME)
public class MainServiceMenu extends AbstractMenuModelBuilder {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "mainServiceMenu";

  @Inject
  private transient ElectCommonPrincipal electCommonPrincipal;

  @Inject
  private transient KalenderMainMenuEntry kalenderMainMenuEntry;

  @Inject
  private transient SystemLogMainMenuEntry systemLogMainMenuEntry;

  @Inject
  private transient SystemLogInfoMainMenuEntry systemLogInfoMainMenuEntry;

  @Inject
  private transient SystemLogWarnMainMenuEntry systemLogWarnMainMenuEntry;

  @Inject
  private transient SystemLogErrorMainMenuEntry systemLogErrorMainMenuEntry;

  @Inject
  private transient NachrichtMainMenuEntry nachrichtMainMenuEntry;

  @Inject
  private transient AdminMainMenuEntry adminMainMenuEntry;

  @Inject
  private transient HilfeMainMenuEntry hilfeMainMenuEntry;

  @Inject
  private transient LogoutMainMenuEntry logoutMainMenuEntry;

  public MainServiceMenu() {
    super();
  }

  public boolean isAnyBadgeCounterMainMenuItemEntryRendered() {
    return getMenuEntriesSorted().stream() //
        .filter(BadgeCounterMainMenuItemEntry.class::isInstance) //
        .map(BadgeCounterMainMenuItemEntry.class::cast) //
        .anyMatch(BadgeCounterMainMenuItemEntry::isRenderedCountIgnored);
  }

  public boolean isMessagePolling() {
    return isMessagePollingEnabled() && isAnyBadgeCounterMainMenuItemEntryRendered();
  }

  public boolean isMessagePollingEnabled() {
    return false; // Performance-Problem, die ganze Seite wird neu erstellt für den kleinen Teil "Nachrichten"...
  }

  @Override
  public List<MenuEntry> getMenuEntriesSorted() {
    List<MenuEntry> result = new ArrayList<>();

    // Einträge nur freichschalten, wenn TFA bestanden
    if (isLoggedInTfa()) {
      result.add(systemLogErrorMainMenuEntry);
      result.add(systemLogWarnMainMenuEntry);
      result.add(systemLogInfoMainMenuEntry);
      result.add(systemLogMainMenuEntry);

      result.add(nachrichtMainMenuEntry);

      if (LayoutComponents.EXPERIMENTAL__FEATURE_TOGGLE__CALENDAR_SIDEBAR) {
        result.add(kalenderMainMenuEntry);
      }

      result.add(adminMainMenuEntry);
    }

    addMenuEntriesHilfe(result);

    result.add(logoutMainMenuEntry);

    return result;
  }

  protected void addMenuEntriesHilfe(List<MenuEntry> result) {
    result.add(hilfeMainMenuEntry);
  }

  protected boolean isLoggedInTfa() {
    return electCommonPrincipal.isLoggedInTfa();
  }
}
