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

import static java.util.Collections.singletonList;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;

import de.ivu.elect.business.system.entity.LogNachrichtArt;
import de.ivu.elect.business.system.entity.SystemLogStatus;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;
import de.ivu.elect.presentation.menu.BadgeCounterMainMenuItemEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;
import de.ivu.elect.presentation.system.SystemLogSearchView;

public abstract class AbstractSystemLogArtMainMenuEntry extends AbstractMenuItemEntry implements BadgeCounterMainMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient SystemLogSearchMenuItemEntry systemLogSearchMenuItemEntry;

  @View
  @Inject
  private transient SystemLogSearchView systemLogSearchView;

  public AbstractSystemLogArtMainMenuEntry() {
    super();
  }

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

  @Nullable
  @Override
  public String getTargetViewId() {
    return null; // action aufrufen
  }

  @Override
  public String doAction() {
    systemLogSearchView.reset();
    systemLogSearchView.getSearchParameters().getSystemLog().setStatus(SystemLogStatus.ZU_PRUEFEN);
    systemLogSearchView.getSearchParameters().getSystemLog().setLogNachrichtArt(getLogNachrichtArt());

    return systemLogSearchMenuItemEntry.getTargetViewId();
  }

  @Override
  public boolean isRendered() {
    return isRenderedCountIgnored() && getCount() > 0L;
  }

  @Override
  public boolean isRenderedCountIgnored() {
    return systemLogSearchMenuItemEntry.isRendered();
  }

  private long getCount() {
    return systemLogSearchMenuItemEntry.getSystemLogCount(getLogNachrichtArt());
  }

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

  @Override
  public String getTitle() {
    return systemLogSearchMenuItemEntry.getLabelFinal() + " (" + getMessage(getLogNachrichtArt()) + ")";
  }

  protected abstract LogNachrichtArt getLogNachrichtArt();

  @Override
  protected boolean isIconOnly() {
    // NOTE, dass nur das Icon angezeigt werden soll!
    return true;
  }

  @Override
  protected String getLabel() {
    return getTitle();
  }

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

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

  @Override
  public String getStyleClass() {
    return super.getStyleClass() + " " + getLogNachrichtArt().getStyleClass() + " ui-icon-badge " + SystemLogMainMenuEntry.ICON_BADGE_COUNTER_NAME;

  }

  @Override
  public String getStyle() {
    return super.getStyle() + " counter-reset: " + SystemLogMainMenuEntry.ICON_BADGE_COUNTER_NAME + " " + Math.min(getCount(), 99) + ";";
  }

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