
package de.ivu.elect.presentation.ui;

import java.io.Serializable;

import javax.annotation.Nullable;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;

import de.ivu.elect.jee.cdi.BeanManagerUtil;
import de.ivu.elect.jee.presentation.dynamictable.ColumnBuilder;


import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
 *  on 08.07.2015.
 */
@ViewAccessScoped
@Named(ColumnBuilderStore.COMPONENT_NAME)
public class ColumnBuilderStore implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "columnBuilderStore";

  private Class<ColumnBuilder> columnBuilderClass;

  public ColumnBuilderStore() {
    super();
  }

  public void setColumnBuilder(ColumnBuilder columnBuilder) {
    this.columnBuilderClass = columnBuilder.getColumnBuilderClass();
  }

  @Nullable
  public ColumnBuilder getColumnBuilder() {
    if (isNull(columnBuilderClass)) {
      return null;
    }

    return BeanManagerUtil.getBeanByClass(columnBuilderClass);
  }

  public void onRowReorder(AjaxBehaviorEvent ajaxBehaviorEvent) {
    ColumnBuilder columnBuilder = getColumnBuilder();

    if (nonNull(columnBuilder)) {
      columnBuilder.onRowReorder(ajaxBehaviorEvent);
    }
  }
}
