package testprocs;

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

// Persistent Node testing

public class PNHTestProc implements ProcessorModule {
  public Symbol getName() { return Symbol.GetSymbol("MyProc"); }
  public void link(PortedProcessor p) {
    pp = p;
    pp.addClockListener(this);
    pp.openNetworkPort(this,uiPort);
    ag = (ActiveGradient)p.getModule("ActiveGradient");
    cm = (ColorModule)p.getModule("Color");
    pn = (PersistentNodeB)p.getModule("PersistentNode");
    nh = (PNHierarchy)p.getModule("Hierarchy");
    rm = (RegionManager)p.getModule("RegionManager");
  }

  PortedProcessor pp; ActiveGradient ag; ColorModule cm; PersistentNodeB pn;
  RegionManager rm; PNHierarchy nh;

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

  public void init() {
    if(!designated)
      { designate=true; designated=true; }
  }

  boolean designate=false;
  static boolean designated = false;

  Vector oldID = new Vector();
  static int emphasize = -1;
  static boolean color_centers=false;
  static int falsecolor=0;
  public boolean signalEvent(Symbol name,Object data) {
    boolean dirty = false;
    if(name==uiPort) {
      System.out.println("recv msg: "+data);
      StringTokenizer tok = new StringTokenizer(data.toString());
      if(tok.hasMoreTokens()) { 
        String cmd=tok.nextToken();
        if(cmd.equals("emphasize")) {
          String s = "off";
          if(tok.hasMoreTokens()) { s = tok.nextToken(); }
          if(s.equals("off")) emphasize=-1;
          else emphasize = (new Integer(s)).intValue();
        } else if(cmd.equals("centers")) {
          if(tok.hasMoreTokens()){color_centers=tok.nextToken().equals("on"); }
          else color_centers=!color_centers;
        } else if(cmd.equals("falsecolor")) {
          if(tok.hasMoreTokens()) { 
            String s = tok.nextToken();
            if(s.equals("A")) falsecolor=0;
            if(s.equals("B")) falsecolor=1;
            if(s.equals("C")) falsecolor=2;
            if(s.equals("D")) falsecolor=3;
          } else {
            falsecolor++;
          }
          while(falsecolor>=4) falsecolor-=4;
        }
      }
    } else if(name==PortedProcessor.kClock) {
      int t = ((Integer)data).intValue();

      Set pnv = ag.getType(pn.pnType);
      Vector nodes = pn.getNodes();
      Vector umbras = pn.getNodeUmbras();

      // coloring: none=blue; l1=red, l2=cyan, both=magenta, covered=
      boolean l1 = false, l2=false, l3=false;
      Iterator i = nodes.iterator();
      while(i.hasNext()) {
        Pair nid = (Pair)i.next();
        if(nid.car()==LID(1)) l1=true;
        if(nid.car()==LID(2)) l2=true;
        if(nid.car()==LID(3)) l3=true;
      }
      boolean l1u=false, l2u=false, l3u=false;
      i = umbras.iterator();
      while(i.hasNext()) {
        Pair nid = (Pair)i.next();
        if(nid.car()==LID(1)) l1u=true;
        if(nid.car()==LID(2)) l2u=true;
        if(nid.car()==LID(3)) l3u=true;
      }

      double r=0.25,g=0.25,b=0.25;
      if(l1) r=1.0; else if(l1u) r=0.6;
      if(l2) g=1.0; else if(l2u) g=0.6;
      if(l3) b=1.0; else if(l3u) b=0.6;
      if(emphasize==1) { g*=0.3; b*=0.3; }
      if(emphasize==2) { r*=0.3; b*=0.3; }
      if(emphasize==3) { r*=0.3; g*=0.3; }

      if(falsecolor==0) {
        cm.setColor(new Color((float)r,(float)g,(float)b));
      } else if(falsecolor==1) {
        Vector id = nh.getID();
        double v = 0;
        if(id.size()>emphasize+1) 
          v=((Double)id.get(emphasize+1)).doubleValue();
        cm.setColor(new Color((int)(v*256*256*256)));
      } else if(falsecolor==2) { // falsecolor=2
        Vector id = nh.getID();
        cm.setColor(cm.numberToColor(id.size()-2));
      } else { // falsecolor=3
        Vector id = nh.getID();
        if(!id.equals(oldID)) {
          if(id.size() != oldID.size()) {
            cm.setColor(Color.white);
          } else if(id.size()>1 && !id.get(1).equals(oldID.get(1))) {
            cm.setColor(Color.red);
          } else if(id.size()>2 && !id.get(2).equals(oldID.get(2))) {
            cm.setColor(Color.green);
          } else if(id.size()>3 && !id.get(3).equals(oldID.get(3))) {
            cm.setColor(Color.cyan);
          } else if(id.size()>4 && !id.get(4).equals(oldID.get(4))) {
            cm.setColor(Color.magenta);
          } else {
            cm.setColor(Color.yellow);
          }
        }
        else 
          cm.setColor(Color.blue);
      }
      oldID = nh.getID();
      //cm.setColor(Color.blue);

      if(color_centers) {
        i = pnv.iterator();
        while(i.hasNext()) {
          Pair nid = (Pair)i.next();
          if(ag.get(nid).getHopcount()==0 &&
             (emphasize==-1 || nid.car()==LID(emphasize))) {
            if(nodes.contains(nid))
              cm.setColor(Color.yellow);
            else 
              cm.setColor(Color.orange); 
          }
        }
      }
    }
    return dirty;
  }
  static Color dkGreen = Color.green.darker().darker();

  static Symbol LID(int level) 
  { return Symbol.GetSymbol("HM"+level); }

  public String toString() {
    return "PNHTestProc";
  }
}


