package commons.graph.worker;
import java.util.List;
import commons.graph.model.GraphModelException;
import commons.graph.model.Node;
public class CascadedFadeOut extends EdgeWorker
{

private int[] visibleNodes;
private int   lastDepth = -1;

public CascadedFadeOut(int[] visibleNodes)
{
   this.visibleNodes = visibleNodes;
}

public void init ()
{
   if (edges.countChildren(graph.getOrigin()) > 0)
   {
      List<Node> children = edges.getChildNodesListSortedByDistance(graph.getOrigin());
      remove(children);
   }
}

public void work (String a, String b) throws GraphModelException
{
   if (lastDepth < depth)
   {
      List<Node> children = edges.getChildNodesListSortedByDistance(b);
      remove(children);
   }
   else if (lastDepth == depth)
   {
      List<Node> children = edges.getChildNodesListSortedByDistance(b);
      remove(children);
   }
   else
   {
      List<Node> children = edges.getChildNodesListSortedByDistance(b);
      remove(children);
   }
   lastDepth = depth;
}

void remove (List<Node> children)
{
   int max = visibleNodes.length > depth ? visibleNodes[depth] : 0;
   if (max==-1) return; // allow all
   int counter = 0;
   for (Node n : children)
      n.visible = (counter++ < max);
}
}
