
package de.ivu.elect.jee.presentation;

import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.join;

import java.io.Serializable;
import java.util.Collection;

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

import org.primefaces.PrimeFaces;

import de.ivu.elect.jee.faces.FacesMessageUtil;

@RequestScoped
@Named(AjaxHelper.COMPONENT_NAME)
public class AjaxHelper implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "ajaxHelper";

  public static final String GLOBAL_MESSAGES_ID = FacesMessageUtil.GLOBAL_MESSAGES_ID;

  public static final String GLOBAL_MESSAGES_DEFERRED_ID = GLOBAL_MESSAGES_ID + "Deferred";

  public AjaxHelper() {
    super();
  }

  public void update(String... clientIds) {
    update(asList(clientIds));
  }

  public void update(Collection<String> clientIds) {
    PrimeFaces.current().ajax().update(clientIds);
  }

  public void executeScript(String statement) {
    PrimeFaces.current().executeScript(statement);
  }

  public void addOnCompleteCallbackParam(String name, Object value) {
    PrimeFaces.current().ajax().addCallbackParam(name, value);
  }

  public void updateMessages() {
    update(GLOBAL_MESSAGES_ID);
  }

  public void updateMessagesDeferred() {
    update(GLOBAL_MESSAGES_DEFERRED_ID);
  }

  public void openPanel(String clientId) {
    executeScript(createOpenPanelStatement(clientId));
  }

  public String createOpenPanelStatement(String clientId) {
    return createPanelStatement(clientId, "expand");
  }

  public void closePanel(String clientId) {
    executeScript(createClosePanelStatement(clientId));
  }

  public String createClosePanelStatement(String clientId) {
    return createPanelStatement(clientId, "collapse");
  }

  private String createPanelStatement(String clientId, String functionName) {
    return createWidgetStatementByClientId(clientId, functionName);
  }

  public String createPageDialogWidgetVar(String dlgName) {
    return DialogActionListener.createDialogWidgetVar(dlgName);
  }

  /**
   * NOTE, dass diese Methode nur für die in XHTML definierten (ELECT-Standard) Dialoge verwendet werden kann!!!
   *
   * @param dlgName
   *
   * @see DialogActionListener
   * @see elect-base-war/src/main/webapp/WEB-INF/tags/basicDialog.xhtml
   */
  public void openPageDialog(String dlgName) {
    openDialog(createPageDialogWidgetVar(dlgName));
  }

  /**
   * NOTE, dass diese Methode nur für die in XHTML definierten (ELECT-Standard) Dialoge verwendet werden kann!!!
   *
   * @param dlgName
   * @return JavaScript
   *
   * @see DialogActionListener
   * @see elect-base-war/src/main/webapp/WEB-INF/tags/basicDialog.xhtml
   */
  public String createOpenPageDialogStatement(String dlgName) {
    return createOpenDialogStatement(createPageDialogWidgetVar(dlgName));
  }

  /**
   * NOTE, dass diese Methode nur für die in XHTML definierten (ELECT-Standard) Dialoge verwendet werden kann!!!
   *
   * @param dlgName
   *
   * @see DialogActionListener
   * @see elect-base-war/src/main/webapp/WEB-INF/tags/basicDialog.xhtml
   */
  public void closePageDialog(String dlgName) {
    closeDialog(createPageDialogWidgetVar(dlgName));
  }

  /**
   * NOTE, dass diese Methode nur für die in XHTML definierten (ELECT-Standard) Dialoge verwendet werden kann!!!
   *
   * @param dlgName
   * @return JavaScript
   *
   * @see DialogActionListener
   * @see elect-base-war/src/main/webapp/WEB-INF/tags/basicDialog.xhtml
   */
  public String createClosePageDialogStatement(String dlgName) {
    return createCloseDialogStatement(createPageDialogWidgetVar(dlgName));
  }

  /**
   * NOTE, dass diese Methode nur für die in XHTML definierten (ELECT-Standard) Dialoge verwendet werden kann!!!
   *
   * @param dlgName
   * @return JavaScript
   *
   * @see DialogActionListener
   * @see elect-base-war/src/main/webapp/WEB-INF/tags/basicDialog.xhtml
   */
  public String createPageDialogStatement(String dlgName, String functionName, String... functionParams) {
    return createDialogStatement(createPageDialogWidgetVar(dlgName), functionName, functionParams);
  }

  public void openDialog(String widgetVar) {
    executeScript(createOpenDialogStatement(widgetVar));
  }

  public String createOpenDialogStatement(String widgetVar) {
    return createDialogStatement(widgetVar, "show");
  }

  public void closeDialog(String widgetVar) {
    executeScript(createCloseDialogStatement(widgetVar));
  }

  public String createCloseDialogStatement(String widgetVar) {
    return createDialogStatement(widgetVar, "hide");
  }

  public String createDialogStatement(String widgetVar, String functionName, String... functionParams) {
    return createWidgetStatementByWidgetVar(widgetVar, functionName, functionParams);
  }

  public String createGlobalBlockUiWidgetVar() {
    return createBlockUiWidgetVar("ctnContent");
  }

  public void openGlobalBlockUi() {
    openBlockUi(createGlobalBlockUiWidgetVar());
  }

  public String createOpenGlobalBlockUiStatement() {
    return createOpenBlockUiStatement(createGlobalBlockUiWidgetVar());
  }

  public void closeGlobalBlockUi() {
    closeBlockUi(createGlobalBlockUiWidgetVar());
  }

  public String createCloseGlobalBlockUiStatement() {
    return createCloseBlockUiStatement(createGlobalBlockUiWidgetVar());
  }

  public String createGlobalBlockUiStatement(String functionName, String... functionParams) {
    return createBlockUiStatement(createGlobalBlockUiWidgetVar(), functionName, functionParams);
  }

  public String createBlockUiWidgetVar(String targetId) {
    return targetId + "BlockUI";
  }

  public void openBlockUi(String widgetVar) {
    executeScript(createOpenBlockUiStatement(widgetVar));
  }

  public String createOpenBlockUiStatement(String widgetVar) {
    return createBlockUiStatement(widgetVar, "show");
  }

  public void closeBlockUi(String widgetVar) {
    executeScript(createCloseBlockUiStatement(widgetVar));
  }

  public String createCloseBlockUiStatement(String widgetVar) {
    return createBlockUiStatement(widgetVar, "hide");
  }

  public String createBlockUiStatement(String widgetVar, String functionName, String... functionParams) {
    return createWidgetStatementByWidgetVar(widgetVar, functionName, functionParams);
  }

  private String createWidgetStatementByClientId(String clientId, String functionName, String... functionParams) {
    return "PrimeFaces.getWidgetById('" + clientId + "')." + functionName + "(" + join(functionParams, ", ") + ");";
  }

  private String createWidgetStatementByWidgetVar(String widgetVar, String functionName, String... functionParams) {
    return "PF('" + widgetVar + "')." + functionName + "(" + join(functionParams, ", ") + ");";
  }
}
