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

import javax.inject.Inject;

import de.ivu.elect.business.internet.boundary.InternetWidgetConversationSelect;
import de.ivu.elect.business.internet.boundary.InternetWidgetEdit;
import de.ivu.elect.business.internet.boundary.InternetWidgetExtendedDAO;
import de.ivu.elect.business.internet.boundary.InternetWidgetFileConversationSelect;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetCustomData;
import de.ivu.elect.business.internet.entity.InternetWidgetFile;
import de.ivu.elect.business.internet.entity.InternetWidgetFileEntity;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.internet.InternetWidgetFileEditView;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;
import de.ivu.elect.presentation.security.AbstractGebietSecurity;

public class InternetWidgetFileCreateMenuItemEntry extends AbstractMenuItemEntry {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetWidgetConversationSelect internetWidgetConversationSelect;

  @Inject
  private transient InternetWidgetFileConversationSelect internetWidgetFileConversationSelect;

  @Inject
  private transient InternetWidgetEdit internetWidgetEdit;

  @Inject
  private transient InternetWidgetExtendedDAO internetWidgetExtendedDAO;

  public InternetWidgetFileCreateMenuItemEntry() {
    super();
  }

  @Override
  public boolean isRendered() {
    return hasUseCasePermission(UseCasePermission.INTERNET_WIDGET_EDIT);
  }

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

  @Override
  public boolean isDisabledInternal() {
    return !isInteretWidgetSelected();
  }

  @Override
  public String getTargetViewId() {
    return KEIN_TARGET;
  }

  @Override
  @Redirect
  public String doAction() {

    InternetWidget entity = internetWidgetConversationSelect.getEntity();
    InternetWidgetCustomData customData = entity.getCustomData(InternetWidgetCustomData.class);

    long lastPosition = customData.getFiles().stream().mapToLong(InternetWidgetFile::getPosition).max().orElse(0);

    InternetWidgetFile internetWidgetFile = new InternetWidgetFile(lastPosition + 1, "", "", null);
    customData.getFiles().add(internetWidgetFile);
    customData.filesChanged();
    internetWidgetEdit.save(entity);
    entity = internetWidgetExtendedDAO.refresh(entity);
    addInfoMessage("internet.widget.file.created");
    internetWidgetFileConversationSelect.setEntity(new InternetWidgetFileEntity(internetWidgetFile), entity);
    return InternetWidgetFileEditView.VIEW_ID;
  }

  protected boolean isInteretWidgetSelected() {
    return internetWidgetConversationSelect.isSelected();
  }

  protected InternetWidget getSelectedInternetWidget() {
    return internetWidgetConversationSelect.getEntity();
  }

  @Override
  protected String getLabel() {
    return getMessage(getLabelKey(), getMessage("internet.wigdet.file.entity.einzahl"));
  }

  @Override
  public String getLabelKey() {
    return "entity.create.menu.item.label";
  }

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