
package de.ivu.elect.presentation.gebietsbaum.tree;

import java.util.function.Function;

import org.primefaces.model.DefaultTreeNode;

import de.ivu.elect.business.gebietsbaum.entity.Gebiet;
import de.ivu.elect.business.gebietsbaum.entity.GebietPK;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.presentation.gebietsbaum.GebietNummerFormatter;

public class GebietsbaumTreeNode extends DefaultTreeNode {

  private static final long serialVersionUID = 1L;

  private boolean childrenLoaded;

  private boolean leaf = false;

  public GebietsbaumTreeNode(Gebiet gebiet, GebietsbaumTreeNodeIconResolver gebietsbaumTreeNodeIconResolver,
                             Messages messages, Function<GebietPK, Gebiet> gebietLoader, GebietNummerFormatter gebietNummerFormatter, boolean isSelectable, boolean isEditable) {
    this(gebiet, true, gebietsbaumTreeNodeIconResolver, messages, gebietLoader, gebietNummerFormatter, isSelectable, isEditable);
  }

  public GebietsbaumTreeNode(Gebiet gebiet, boolean childrenLoaded,
                             GebietsbaumTreeNodeIconResolver gebietsbaumTreeNodeIconResolver,
                             Messages messages, Function<GebietPK, Gebiet> gebietLoader, GebietNummerFormatter gebietNummerFormatter, boolean isSelectable, boolean isEditable) {
    this(new GebietsbaumTreeNodeData(gebiet, gebietsbaumTreeNodeIconResolver, messages, gebietLoader, gebietNummerFormatter, isSelectable, isEditable), childrenLoaded);
  }

  GebietsbaumTreeNode(GebietsbaumTreeNodeData nodeData, boolean childrenLoaded) {
    super(nodeData, null);
    setExpanded(false);
    setType("genericIcon");
    this.childrenLoaded = childrenLoaded;
  }

  @Override
  public boolean isLeaf() {
    if (childrenLoaded) {
      return super.isLeaf();
    }

    return leaf;
  }

  public void setLeaf(boolean leaf) {
    this.leaf = leaf;
  }

  public boolean isChildrenLoaded() {
    return childrenLoaded;
  }

  public void setChildrenLoaded(boolean childrenLoaded) {
    this.childrenLoaded = childrenLoaded;
  }

}
