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

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import de.ivu.elect.business.wahl.entity.WahlPK;
import de.ivu.elect.business.wahl.entity.WahlterminPK;

@ViewAccessScoped
public class LazyGebietsbaumStore implements Serializable {

  private static final long serialVersionUID = 1L;

  private volatile /*WASDE-2178 Versuch*/ DefaultTreeNode root;

  private volatile /*WASDE-2178 Versuch*/ TreeNode selectedNode;

  private LazyGebietsbaumStoreReferenz lastGebietsbaumStoreReferenz = new LazyGebietsbaumStoreReferenz(null, null, null);

  private final AtomicBoolean treeReady = new AtomicBoolean(false); /*WASDE-2178 Versuch*/

  public DefaultTreeNode getRoot() {
    return root;
  }

  public void setRoot(DefaultTreeNode root) {
    this.root = root;
  }

  public TreeNode getSelectedNode() {
    return selectedNode;
  }

  public void setSelectedNode(TreeNode selectedNode) {
    this.selectedNode = selectedNode;
  }

  public boolean requestViewWahlOrWahlterminChanged(String currentViewId, WahlterminPK wahlterminPK, WahlPK wahlPK) {
    LazyGebietsbaumStoreReferenz compare = new LazyGebietsbaumStoreReferenz(currentViewId, wahlterminPK, wahlPK);
    if (compare.equals(lastGebietsbaumStoreReferenz)) {
      return false;
    }
    lastGebietsbaumStoreReferenz = compare;
    return true;
  }

  public void setTreeReady(boolean treeReady) {
    this.treeReady.set(treeReady);
  }

  public boolean isTreeReady() {
    return treeReady.get();
  }
}
