int GAME_TITLE = 1;
int GAME_PLAY = 2;
int gameMode = GAME_TITLE;


boolean wonIt = false;
boolean doBackArrow = false;
color GOLD = color(255,212,0);

int SCREENCENTER = 250;


boolean hintShowing;



Puzzle currentPuzzle ;

ParentPuzzle parentPuzzle ;

   String[] text3_parent,text3,text5,text7;
PImage titleart;
void setup(){
  titleart = loadImage("microrail.png");
    frameRate(20);
  smooth();
   size(500,500);
  
  loadSounds();
  startMusic();
   text3_parent = loadStrings("3x3_parent.txt");   
   text3 = loadStrings("3x3.txt");
   text5 = loadStrings("5x5.txt");
   text7 = loadStrings("7x7.txt");
   
   resetPuzzle();
   
  // currentPuzzle = new Puzzle("5 5 25 0 1 1 1 0 1 2 1 1 0 1 1 1 1 1 1 1 3 1 0 0 1 1 1 0 8 11 16 16 17 13 18 1 2 36 11 12 10 15 7 12 5 10 2 6 3 7 3 8 1 5 8 14 13 14 18 23 22 23 15 21 21 22 1 2 13 18 16 17 11 16");
//   currentPuzzle = new Puzzle("7 7 49 0 0 1 1 1 1 0 0 1 2 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 3 1 0 0 0 1 1 1 1 0 24 32 33 33 34 20 26 30 37 22 30 17 24 5 11 21 22 37 38 19 20 3 9 11 12 72 31 38 31 32 24 25 18 25 17 23 16 23 10 18 46 47 45 46 44 45 39 40 40 47 36 44 8 15 15 16 21 29 29 36 26 27 27 34 12 19 4 5 4 10 2 3 2 8 11 12 3 9 19 20 37 38 21 22 5 11 17 24 22 30 30 37 20 26 33 34 32 33");
}
boolean doHand;
void draw(){

      doHand = false;
 
 
     
  
     
     background(100,200,100); 
  
    if(doBackArrow && isInArrow()){
     doHand = true;
    }
    if(gameMode == GAME_PLAY) currentPuzzle.draw();  
    else doTitle();
    
    
    
     stroke(0);
    // line(0,0,250,250);
    // line(500,0,250,250);
    
    if(doBackArrow){
       noStroke();
       //flash arrow between gold and gray, otherwise gray
       if(currentPuzzle.trainMoving){
         float r = lerp(200,255,.5 +currentPuzzle.moveVal/2);
         float g = lerp(200,215,.5+currentPuzzle.moveVal/2);
         float b = lerp(200,0,.5+currentPuzzle.moveVal/2);
         fill(r,g,b);
       } else fill(200);
      rect(40,40,20,20);
      triangle(40,30,
              40,70,
              20,50); 
              
       if(showArrowHint){
          fill(GOLD);
          textAlign(LEFT);
          text("CLICK TO RETURN",70,55);
       }       
              
    }
    
    if(currentPuzzle != parentPuzzle && ! seenInstructions){
       textAlign(LEFT);
       fill(0);
       text(
       "MICROTOWN MICRORAIL NEEDS YOU! CLICK ON THE GRAY LINES TO LAY TRACK BETWEEN NODES. CLICK AGAIN TO REMOVE.\n\n"+
       
       "THE GOAL IS TO CONSTRUCT A SINGLE RAIL LINE PASSING THROUGH "+
"ALL THE STATIONS, WITH THE BLUE DOTS AS ENDS OF THE LINE. "+
"[I.E. WHEN SOLVED, EVERY WHITE STATION WILL HAVE 2 RAILS CONNECTED "+
"TO IT, AND THE BLUE STATIONS WILL HAVE ONLY A SINGLE RAIL EACH]\n\n"+
       "CLICK 'HINT' FOR SUGGESTIONS.",
       
       20,335,440,150);
    }
    
    if(currentPuzzle != parentPuzzle && gameMode == GAME_PLAY){
       textAlign(CENTER);
       fill(200);
       rect(210,475,80,20);
      fill(0);
       text("HINT",250,490); 
       
       if(overHint()){
          doHand = true; 
       }
       
    }
    
    
    
  if(mousePressed && overHint() && gameMode == GAME_PLAY && currentPuzzle != parentPuzzle){
    currentPuzzle.showHint();
    if(!hintShowing) currentPuzzle.hintCount++;
    hintShowing = true;


  } else {
    hintShowing = false;
  }  
    
    if(doHand) cursor(HAND); 
     else cursor(ARROW);   
    
    if(wonIt && gameMode == GAME_PLAY ){
          fill(GOLD);
        textAlign(CENTER);
         text("EXCELLENT! YOU ARE THE KING OF THE MICRORAIL!\n(You used "+parentPuzzle.howManyHints()+" hints)\nPRESS SPACE TO PLAY AGAIN",250,30); 
 
    }
    
//     text(round(frameRate),0,480);
}

boolean overHint(){
return  mouseX >= 210 && mouseX <=  290 && mouseY >= 475 && mouseY <= 495 ;
}

boolean isInArrow(){
  return (mouseX > 20 && mouseX < 60 && mouseY > 30 && mouseY < 70);
}

void restoreParent(){
    KidPuzzle kid = (KidPuzzle)currentPuzzle; 
    currentPuzzle.startZoom(SCREENCENTER,SCREENCENTER,parentPuzzle.getScreenX(kid.x,kid.y),parentPuzzle.getScreenY(kid.x,kid.y),120,30);
    currentPuzzle = parentPuzzle;
    //currentPuzzle.startZoom(CENTER,CENTER,CENTER,CENTER,400,120);
    doBackArrow = false; 
    showArrowHint = false;
}


void mousePressed(){
  
  if(gameMode == GAME_TITLE) {
      gameMode = GAME_PLAY; 
    stopMusic();  
    startGame();
    return;
  }
  
  boolean handled = false;
  if(doBackArrow){
     if(isInArrow()){
         restoreParent();
         fx_outwoosh();
         handled = true;
         seenInstructions = true;
     }
  }
  
 
  if(! handled) currentPuzzle.checkClick(); 
}



void doTitle(){
    image(titleart,0,0);
    textAlign(RIGHT);
 

}

void keyPressed(){
  if(key == ' '){
   gameMode = GAME_TITLE;     
   resetPuzzle();
   if(!musicPlaying) startMusic();
  }
}


void resetPuzzle(){
  parentPuzzle = new ParentPuzzle(text3_parent[int(random(text3_parent.length))]); 
  currentPuzzle = parentPuzzle;
}

boolean seenClickHere,seenInstructions,seenArrowHint,   showArrowHint;
void startGame(){
  seenClickHere = false;
  seenInstructions = false;
  seenArrowHint = false;
  wonIt = false;
  showArrowHint = false;
}