package de.ivu.elect.presentation.dashboard.entries;

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

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

import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.MenuModel;

import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.dynamicform.AdvancedDynaFormModel;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.status.OutputSeverity;
import de.ivu.elect.presentation.menu.AbstractMenuItemEntry;


import static java.util.Objects.isNull;

@RequestScoped //ELECT-15902
public abstract class AbstractDashboardEntry implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient Messages messages;

  public abstract String getLegend();

  protected Messages getMessages() {
    return messages;
  }

  private AdvancedDynaFormModel model;

  public AdvancedDynaFormModel getModel() {
    if (isNull(model)) {
      FormBuilder formBuilder = createFormBuilder();
      formBuilder.withId("md_" + this.getClass().getSimpleName());

      addProperties(formBuilder);

      model = formBuilder.createModel();

    }

    return model;
  }

  protected abstract void addProperties(FormBuilder formBuilder);

  protected FormBuilder createFormBuilder() {
    return new FormBuilder();
  }

  public String getSplitButtonIconLabel() {
    return "";
  }

  public MenuModel getSplitButtonModel() {
    return new DefaultMenuModel();
  }

  public boolean isSplitButtonRendered() {
    return StringUtils.isNotBlank(getSplitButtonIconLabel());
  }

  public abstract List<AbstractMenuItemEntry> getMenuItemsFirst();

  public abstract List<AbstractMenuItemEntry> getMenuItemsLast();

  public void doMenuAction(AbstractMenuItemEntry menuItemEntry) {
    String redirect = null;
    if (menuItemEntry.isPossible()) {
      if (StringUtils.isBlank(menuItemEntry.getTargetViewId())) {
        redirect = menuItemEntry.doAction();
      } else {
        redirect = menuItemEntry.getTargetViewId();
      }
    }
    if (!menuItemEntry.isButtonAjax() && StringUtils.isNotBlank(redirect)) {
      FacesContextUtil.redirectToView(redirect);
    }
  }

  public boolean isRendered() {
    return isMenuItemsRendered() || isSplitButtonRendered();
  }

  protected boolean isMenuItemsRendered() {
    return isMenuItemsRendered(getMenuItemsFirst()) || isMenuItemsRendered(getMenuItemsLast());
  }

  private boolean isMenuItemsRendered(List<AbstractMenuItemEntry> menuItemsLast) {
    return menuItemsLast.stream().anyMatch(AbstractMenuItemEntry::isPossible);
  }

  public boolean isLineBreakAfterFirstButtons() {
    return false;
  }

  public String getExtraStyleClass() {
    OutputSeverity problem = getSeverity();
    if (OutputSeverity.EMPTY.equals(problem)) {
      return "";
    }
    return "dashboard-problem-" + problem.name().toLowerCase();
  }

  protected OutputSeverity getSeverity() {
    return OutputSeverity.EMPTY;
  }

  public String getId() {
    String simpleName = getClass().getSimpleName();
    if (simpleName.contains("$")) {
      // Weld Proxy...
      simpleName = simpleName.substring(0, simpleName.indexOf("$"));
    }
    return "fld" + simpleName;
  }

  protected String getMessage(String msgKey, Object... params) {
    return getMessages().getMessageForMessageKey(msgKey, params);
  }

  protected String getMessage(Enum<?> enumValue) {
    return getMessages().lokalisiere(enumValue);
  }

  public boolean isEingabeErwartet() {
    return false;
  }

  public boolean isTemplatePresent() {
    return StringUtils.isNotBlank(getTemplate());
  }

  public String getTemplate() {
    return "";
  }

  public String getIcon() {
    if (OutputSeverity.EMPTY.equals(getSeverity())) {
      return "";
    }
    return "ui-message-" + getSeverity().name().toLowerCase() + "-icon";
  }

  public boolean isIconPresent() {
    return StringUtils.isNotBlank(getIcon());
  }

  public String getExtraOnClick(AbstractMenuItemEntry menuItemEntry) {
    if (menuItemEntry instanceof DasboardMenuItemEntry) {
      return ((DasboardMenuItemEntry) menuItemEntry).getExtraOnClick();
    }

    return "PF('ctnContentBlockUI').show();";
  }

  public List<String> getAfterTemplates() {
    return Collections.emptyList();
  }

}
