package testprocs;

import simulator.*;
import modules.color.*;
import modules.hierarchy.*;
import modules.neighborset.*;
import modules.gradient.*;
import utils.*;
import java.awt.*;
import java.util.*;

// Persistent Node testing

public class HTestProc implements ProcessorModule {
  public Symbol getName() { return Symbol.GetSymbol("HTestProc"); }
  public void link(PortedProcessor p) {
    pp = p;
    pp.addClockListener(this);
    pp.openNetworkPort(this,uiPort);
    ag = (ActiveGradient)p.getModule("ActiveGradient");
    cm = (ColorModule)p.getModule("Color");
    hm = (Hierarchy)p.getModule("Hierarchy");
  }

  PortedProcessor pp; ActiveGradient ag; ColorModule cm; Hierarchy hm;

  static final Symbol uiPort = Symbol.GetSymbol("UI");

  public void init() {
  }

  public static int level = 0;
  public boolean signalEvent(Symbol name,Object data) {
    boolean dirty = false;
    if(name==uiPort) {
      StringTokenizer tok = new StringTokenizer(data.toString());
      if(tok.hasMoreTokens()) { 
        String cmd=tok.nextToken();
        if(cmd.equals("level")) {
          if(tok.hasMoreTokens()) {
            Integer i = new Integer(tok.nextToken()); level=i.intValue();
          } else {
            level=0;
          }
        }
      }
    } else if(name==PortedProcessor.kClock) {
      int t = ((Integer)data).intValue();
      
      // coloring: #levels = color
      double[] id = hm.getID();
      cm.setColor(cm.numberToColor(id.length));

      int status = hm.status(level-1);
      if(status==Level.NID_SELF) {
        cm.setColor(Color.magenta);
      } else if(status==Level.NID_NBR) {
        cm.setColor(Color.red);
      } else {
        cm.setColor(Color.blue);
      }
    }
    return dirty;
  }

  public String toString() {
    Symbol type = Symbol.GetSymbol("HM"+level);
    Set nbrs = ag.getTypeLive(type);
    return "\nLevel "+level+" nbrs: "+nbrs.toString()+"\n\n";
  }
}


