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

import java.util.List;

import javax.inject.Inject;

import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;
import de.ivu.elect.presentation.system.SystemLogSearchView;


import static java.util.Collections.singletonList;

public class SystemLogMainMenuEntry extends AbstractMenuItemEntry {

  private static final long serialVersionUID = 1L;

  // Cf. /elect-common-war/src/main/webapp/resources/css/common/Icons.css.
  public static final String ICON_BADGE_COUNTER_NAME = "ui-icon-badge-counter-system-log";

  @Inject
  private SystemLogSearchMenuItemEntry systemLogSearchMenuItemEntry;

  @View
  @Inject
  private transient SystemLogSearchView systemLogSearchView;

  public SystemLogMainMenuEntry() {
    super();
  }

  @Override
  protected AbstractGebietSecurity getGebietSecurity() {
    return KEINE_GEBIET_SECURITY;
  }

  @Override
  public boolean isRendered() {
    return systemLogSearchMenuItemEntry.isRendered() && !isDisabled() && getSystemLogCount() < 1L;
  }

  @Override
  public boolean isDisabledInternal() {
    return systemLogSearchMenuItemEntry.isDisabled();
  }

  @Override
  public String getTargetViewId() {
    return null; // AKTION
  }

  @Override
  public String doAction() {
    systemLogSearchView.reset();
    return systemLogSearchMenuItemEntry.getTargetViewId();
  }

  public long getSystemLogCount() {
    return systemLogSearchMenuItemEntry.getSystemLogCount();
  }

  @Override
  public String getTitle() {
    return systemLogSearchMenuItemEntry.getLabelFinal();
  }

  @Override
  protected String getLabel() {
    return systemLogSearchMenuItemEntry.getLabelFinal();
  }

  @Override
  public String getLabelKey() {
    return systemLogSearchMenuItemEntry.getLabelKey();
  }

  @Override
  public String getIcon() {
    if (getSystemLogCount() > 0) {
      return systemLogSearchMenuItemEntry.getIcon();
    }
    return getIcons().getIconCheck();
  }

  @Override
  public String getStyleClass() {
    if (getSystemLogCount() > 0) {
      return super.getStyleClass() + " ui-menuitem-link-icon-only ui-icon-badge " + ICON_BADGE_COUNTER_NAME;
    }
    return super.getStyleClass() + " ui-menuitem-link-icon-only";
  }

  @Override
  public String getStyle() {
    long systemLogCount = getSystemLogCount();
    if (systemLogCount > 0) {
      return super.getStyle() + " counter-reset: " + ICON_BADGE_COUNTER_NAME + " " + Math.min(systemLogCount, 99) + ";";
    }

    return super.getStyle();
  }

  @Override
  protected List<String> getAdditionalTopicViewIds() {
    return singletonList("");
  }
}
