/**
 * A SysInventory object keeps track of computer systems for a department
 *
 * @see SystemInventoryMain
 * @see ITSystem
 */

import java.util.Scanner;

public class SysInventoryWithHostName
{
  private String name;             // the name of this SysInventory 
  private Scanner scanner;         // for getting input from the user   
  private ITSystemWithHostName [] systems;     // the systems we're tracking
  private static final String HELPSTRING = "Actions: exit, help, print, record-problem";
  
  /**
   * Construct a SysInventory with the given name.
   * @param name the name of the SysInventory.
   */
  
  public SysInventoryWithHostName( String name )
  {
    this.name = name;
  }
  
  /**
   * open the SysInventory: get it set up with IT systems
   * @param input for user input.
   */
  public void open( Scanner input)
  {
    scanner = input;
    System.out.println( "Welcome to " + name );
    
    // Construct ITSystems systems. (Eventually replace by reading a file)
    int n = promptInt( "Enter how many systems: " );
    systems = new ITSystemWithHostName[n + 1];  // avoid "system 0"
    for (int id = 1; id <= n; id++) {
      int r = promptInt( "Enter the system's room no: " );
      String hostName = prompt( "Enter host name: ");
      systems[id] = new ITSystemWithHostName( id, r , hostName, 0 );  // assiume 0 problems to start
      System.out.println(" System is assigned number " + id);  
    }   
  }
  
  /**
   * process commands for the SysInventory
   * 
   */
  public void doCommands() {
    while ( true ) {
      ITSystemWithHostName sys = whichITSystem();
      if ( sys == null ) {
        break; // all done
      }
      else {
        processActionsForITSystem(sys);
      }
    }       
    System.out.println( "Goodbye from " + name );
  }
  
  // Prompt the user for an system number and return the
  // corresponding ITSystem object. Return null when
  // the user is done.
  
  private ITSystemWithHostName whichITSystem()
  {
    while (true) {
      int  idNumber = 0; 
      System.out.println("Enter system inventory number, 0 to quit or host xx: ");
      if (scanner.hasNextInt()) {
        // String x = scanner.next();   // for quick look at what came in   
        //System.out.println("got x = " + x);
        idNumber = scanner.nextInt();
        // old code--     
        if (idNumber > 0 && idNumber < systems.length) {
          return systems[idNumber];  // found matching id
        }
        else if ( idNumber == 0 ) {
          return null;  // user says to quit
        }
        else {
          System.out.println( "No system numbered " + 
                             idNumber + "; try again." );
        }
      } else {
        //System.out.println("other case"); // temp. output      
        String token = scanner.next();
        //System.out.println(" got token = " + token);
        if (token.equals("host")) {
          String hostname = scanner.next();
          // now find system with hostname:
          for (int i = 1; i < systems.length; i++) {
            if (systems[i].getHostName().equals(hostname)) {
              return systems[i]; // found hostname, return ITSystem object
            }
          }
          // Here when no match found
          System.out.println(" No system with hostname " + hostname + "found, try again"); 
        }
      }
    }
  }
  
  // Prompt the user for action to process.
  // and do it,
  
  private void processActionsForITSystem( ITSystemWithHostName system) 
  {
    System.out.println("System " + system.getId() + " selected");
    System.out.println( HELPSTRING );
    
    boolean moreActions = true;
    while ( moreActions ) {
      String command = prompt( "Enter action: " );
      if ( command.equals( "exit" ) ) {
        moreActions = false;
      }
      else if ( command.equals( "help" ) ) {
        System.out.println( HELPSTRING );
      }
      else if ( command.equals( "print" ) ) {
        System.out.println(system);  // calls toString()
      }
      else if ( command.equals( "record-problem" ) ) {
        system.recordProblem( );
        System.out.println("The system now has " + system.getProblemCount() + " recorded problems");
      }
      else{
        System.out.println("sorry, unknown action" );
      }
    }
  }
  
  // helper methods using the private Scanner  
  private String prompt( String prompt) {
    System.out.print(prompt);
    return scanner.next();
  }
  
  private int promptInt( String prompt) {
    System.out.print(prompt);
    return scanner.nextInt();
  }
}

