package testprocs;

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

public class HierarchyColorModel implements NodeColorModel {
  static final Color dkgreen = Color.green.darker().darker();
  public Color getLinkColor(Object a,Object b) {
    PortedProcessor pa = (PortedProcessor)a;
    PortedProcessor pb = (PortedProcessor)b;
    if(pa.suspended() || pb.suspended()) return Color.darkGray;
    // find out the level information:
    int level = HTestProc.level;
    if(level >= 0) {
      Hierarchy hma = (Hierarchy)pa.getModule("Hierarchy");
      ActiveGradient aga = (ActiveGradient)pa.getModule("ActiveGradient");
      Hierarchy hmb = (Hierarchy)pb.getModule("Hierarchy");
      ActiveGradient agb = (ActiveGradient)pb.getModule("ActiveGradient");

      boolean neighbora=false, neighborb=false;
      boolean group=false; boolean subgroup=false;
      double[] ida = hma.getID();
      double[] idb = hmb.getID();
      if(ida!=null && idb!=null && 
         ida.length > level && idb.length > level) {
        // check for group relation
        if(ida[level]==idb[level]) group=true;
        if(level>0 && ida[level-1]==idb[level-1]) subgroup=true;
        // check for nbr relation
        Symbol type = Symbol.GetSymbol("HM"+level);
        Gradient g=aga.get(new Pair(type,new Double(idb[level])));
        if(g!=null && !g.isDead()) neighbora=true;
        g=agb.get(new Pair(type,new Double(ida[level])));
        if(g!=null && !g.isDead()) neighborb=true;
      }
      if((neighbora || neighborb) && group) return Color.red;
      else if((neighbora || neighborb) && subgroup) return Color.magenta;
      else if(neighbora && neighborb) return Color.blue;
      else if(neighbora || neighborb) return Color.cyan;
      else if(subgroup) return Color.green;
      else if(group) return dkgreen;
    }
    return Color.darkGray;
  }
  public NodeImage getNodeImage(Object o) {
    NodeImage ni = new NodeImage();
    PortedProcessor p = (PortedProcessor)o;
    if(p.suspended()) {
      ni.core = Color.darkGray;
    } else {
      ni.core = p.getColor();
      int level = HTestProc.level;
      Hierarchy hm = (Hierarchy)p.getModule("Hierarchy");
      int s = hm.getStability(); int ls = hm.getStability(level);
      if(ls==-1 || ls>5) ni.ring=Color.black; else ni.ring=Color.white;
      if(s>5) ni.rim=Color.gray; else ni.rim=Color.green;
    }
    return ni;
  }

}
