import javabook.*;

/**
 * This class is the top-level controller object for managing
 * all other objects in the HiLo program.
 *
 * @author Dr. Caffeine
 *
 */
class HiLo        // Step 3: generate random numbers
{

//--------------------------------
//  Data Members
//--------------------------------
  
  private final int        MAX_GUESS_ALLOWED       = 6;

  private MainWindow       mainWindow;
  private MessageBox       messageBox;
  private OutputBox        outputBox; 
  private ResponseBox      responseBox;
  private InputBox         inputBox;
  
  private int              secretNumber;  

//--------------------------------
//  Constructors
//--------------------------------
  
  /**
   * Default constructor. The default window title is "Drawing Shape".
   */
  public HiLo( )
  {
      mainWindow    = new MainWindow  ("Let’s Play HiLo");
      outputBox     = new OutputBox   ( mainWindow );
      responseBox   = new ResponseBox ( mainWindow );
      messageBox    = new MessageBox  ( mainWindow );
      inputBox      = new InputBox    ( mainWindow );
    
      mainWindow.setVisible( true );
      outputBox.setVisible( true );
  }    

//-------------------------------------------------
//    Public Methods:
// 
//      void    start    (    )
//
//------------------------------------------------
  
  /**
   * Top level method that calls other private methods
   * to play the Hi-Lo games.   
   */
  public void start ( )
  {
      int answer;
      
      describeRules();
  
      answer = responseBox.prompt("Do you want to play a Hi-Lo game?");
  
      while ( answer == ResponseBox.YES ) {
  
          generateSecretNumber( );
    
          playGame();
    
          answer = responseBox.prompt
                               ("Do you want to play another Hi-Lo game?");
    }
  }

//-------------------------------------------------
//    Private Methods:
// 
//    void    describeRules         (      )
//    void    generateSecretNumber  (      )
//    void    playGame              (      )
//
//------------------------------------------------
  
  /**
   * Provides a brief explanation of the program to the user.   
   *
   */    
  private void describeRules( )
  {
      outputBox.printLine("Inside describeRules");         //TEMP
  }

  /**
   * Generates a random number between 1 and 100.   
   *
   */  
  private void generateSecretNumber( )
  {
      double X = Math.random();
  
      secretNumber = (int) Math.floor( X * 100 ) + 1;

      System.out.println("Secret Number: " + secretNumber); //TEMP
                              
  }
  
  
  /**
   * Plays one Hi-Lo game.   
   *
   */  
  private void playGame( )
  {
    
      int guessCount = 0;
      int guess;
    
      do {
          //get the next guess
          guess = getNextGuess();
      
          guessCount++;
      
          //check the guess
          if (guess < secretNumber) {
              messageBox.show("Your guess is LO");
          }
          else if (guess > secretNumber) {
              messageBox.show("Your guess is HI");
          }
    
      } while ( guessCount < MAX_GUESS_ALLOWED &&                         
                 guess != secretNumber );                    
    
      //output appropriate message
      if ( guess == secretNumber ) {
          messageBox.show("You guessed it in " 
                  + guessCount  + " tries.");
      }
      else {
          messageBox.show("You lost. Secret No. was " 
                  + secretNumber);
      }

  }
  
  /**
   * Gets the player's next guess.
   *
   * @return the next guess entered by the player
   */
  private int getNextGuess( )
  {
      int nextGuess;
      
      nextGuess = inputBox.getInteger("Enter Guess between 1 and 100");
    
      while (nextGuess < 1 || nextGuess > 100) {
          messageBox.show("Guess must be between 1 and 100");
          nextGuess = inputBox.getInteger("Your Guess:");
      }
    
      System.out.println("Guess: " + nextGuess); //TEMP
    
      return nextGuess;
  }
}