/**
 * A SysInventory object keeps track of computer systems for a department
 *
 * @see SystemInventoryMain
 * @see ITSystem
 */

import java.util.Scanner;

public class SysInventory
{
    private String name;             // the name of this SysInventory 
    private Scanner scanner;         // for getting input from the user   
    private ITSystem [] 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 SysInventory( 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 ITSystem[n + 1];  // avoid "system 0"
        for (int id = 1; id <= n; id++) {
          int r = promptInt( "Enter the system's room no: " );
          systems[id] = new ITSystem( id, r , 0 );  // assiume 0 problems to start
          System.out.println(" System is assigned number " + id);  
        }   
    }
    
     /**
     * process commands for the SysInventory
     * 
     */
    public void doCommands() {
      while ( true ) {
        ITSystem 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 ITSystem whichITSystem()
    {
      while (true) {
        int  idNumber = 
          promptInt("Enter system inventory number, 0 to quit: ");
        
        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." );
        }
      }
    }

    // Prompt the user for action to process.
    // and do it,

    private void processActionsForITSystem( ITSystem 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();
    }
}


