HashSet movers = new HashSet();
HashMap bigmap = new HashMap();

String buffer = "";

ArrayList oldLines = new ArrayList();

String a, b;

Pos posA = new Pos(10,250);
Pos posB;

float lineHeight = 24;
String currentLine = "";


pile p;

void setup(){
  size(500,500);
  frameRate(30);
  textFont(loadFont("ARBERKLEY-24.vlw"));

  String lines[] = loadStrings("words.txt");

  for(int i = 0; i < lines.length; i++){
    String parts[] = lines[i].split("\t");
    bigmap.put(parts[0]+"\t"+parts[1], new pile(parts));

  }

  String seedparts[] = lines[int(random(lines.length))].split("\t");
  a = seedparts[0];
  b = seedparts[1];

  posB = new Pos(posA.x+ textWidth(a+" "), posA.y);


  currentLine = a +" "+b;

  doFirst();

  
}

void doFirst(){
  String key = a+"\t"+b;
  p = (pile)bigmap.get(key);
  p.showme();

  
}
void doNext(String next){
  a = b;
  b = next;

  String key = a+"\t"+b;
  p = (pile)bigmap.get(key);
  p.showme();
  
}




void draw(){
  background(200);
  //text(a,posA.x,posA.y);
  //text(b,posB.x,posB.y);
  fill(0);
  for(int i = 0; i < oldLines.size(); i++){
    String s = (String)oldLines.get(i);
    text(s, 0,(i+1) * lineHeight);
  }  
  float currentLineY = (oldLines.size() +1)* lineHeight;

  text(currentLine,0,currentLineY);

  p.draw();





}


void mouseReleased(){
  if(mouseX > 250){
    String newWord = p.click();
    if(textWidth(currentLine + " "+newWord) > 250){
//      movers .add(new PosMover(newWord,new Pos(mouseX,mouseY),new Pos(textWidth(currentLine + " "),currentLineY));
    
      oldLines.add(currentLine);
      currentLine = newWord;
    } else {
       currentLine += " "+newWord; 
    }
    doNext(newWord);
    
  }
}





class pile {

  void showme(){
//     println("KEY:::: "+a+" "+b+"--"); 
  }
  
String a;
String b;
  int uniqs; 
  int tot;
  int upto;
  ArrayList valWords = new ArrayList();
  pile(String []parts){
     a = parts[0];
         b = parts[1];
    uniqs = Integer.parseInt(parts[2]);
    tot = Integer.parseInt(parts[3]);

    upto = 0;
    for(int i = 0; i < uniqs; i++){
      String word = parts[4 + (i * 2)];
      int count = Integer.parseInt(parts[5 + (i * 2)]);
      upto += count;
      valWords.add(new valAndWord(upto,word,count));

    }
    float lastBottom = 0;
    for(int i = 0; i < valWords.size();i++){
      valAndWord  v= (valAndWord) valWords.get(i);
      float thisHeight = (v.amount * 500) / upto;                
      v.boxtop = lastBottom;
      v.boxheight = thisHeight;

      float mid = lastBottom + (thisHeight / 2.0);
      lastBottom += thisHeight;
    }



  } 
  

  
  void draw(){
    int redo = -1;
    for(int i = 0; i < valWords.size();i++){
      valAndWord  v= (valAndWord) valWords.get(i);

      fill(255);
      if(mouseX > 250 && mouseY >= v.boxtop && mouseY < v.boxtop+v.boxheight){
        redo = i;
      }
      rect(250,v.boxtop,250,v.boxheight);
      float mid = v.boxtop + (v.boxheight/2.0)+12;
      fill(0);
      text(v.word,260,mid);

    }
    if(redo != -1){
      valAndWord  v= (valAndWord) valWords.get(redo);
        fill(255,200,200); 
        float doheight = max(24,v.boxheight);
       float dotop=min(v.boxtop,476);
      rect(250,dotop,250,doheight);

      fill(200,0,0);
            float mid =dotop + (doheight/2.0)+12;
      text(v.word,260,mid);

      
    }

    
    
  }


String click(){
    for(int i = 0; i < valWords.size();i++){
      valAndWord  v= (valAndWord) valWords.get(i);

    if(mouseX > 250 && mouseY >= v.boxtop && mouseY < v.boxtop+v.boxheight){
        return v.word;
      }
     
    }
  return "";
}


  String pickWord(){
    int r = int(random(upto))+1;

    for(int i = 0; i < valWords.size();i++){
      valAndWord  v= (valAndWord) valWords.get(i);
      if(v.val >= r){
        return v.word;
      } 
    }
    return "!!!!";
  }  
}

class valAndWord{
  int val;
  String word;
  float amount;
  float boxtop;
  float boxheight;
  valAndWord(int val, String word, float amount){
    this.val = val;
    this.word = word;
    this.amount = amount;
  }

}


class Pos{
  float x, y;
  Pos(float x, float y){
    this.x = x;
    this.y = y; 
  }
}