import NodeList;
import Node;
import java.io.IOException;
import rebelsky.io.SimpleReader;
import rebelsky.io.SimpleOutput;
import rebelsky.util.ComparableInteger;
import rebelsky.util.NumberRangeException;

/**
 * An attempt to test the NodeList class.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 1998
 */
public class NodeListTester
{

  // +----------------+-----------------------------------------------------
  // | Static Methods |
  // +----------------+

  /**
   * Read a list of integers.
   */
  public static NodeList readIntList(SimpleReader in)
    throws IOException, NumberRangeException
  {
    // The next input value
    int val;
    // The list we're generating
    NodeList list = new NodeList();
    while (!in.eoln()) {
      val = in.readInt();
      list.addToEnd(new Node(new ComparableInteger(val)));
    }
    // That's it
    return list;
  } // readIntList

  /**
   * Give instructions for using this program.
   */
  public static void giveHelp(SimpleOutput out)
  {
    out.println("a val1 ... valn - append a list of one or more values");
    out.println("d - delete the first element");
    out.println("e val - add a single value to the end");
    out.println("h - get this help message");
    out.println("l - get the length");
    out.println("p - print the current list");
  } // giveHelp()


  // +------------+---------------------------------------------------------
  // | Attributes |
  // +------------+

  /**
   * Repeatedly prompt for what to do and then do it.
   */
  public static void main(String[] args)
  {
    // The object used for output.
    SimpleOutput out = new SimpleOutput();
    // The object used for input.
    SimpleReader in = new SimpleReader();
    // The node list we're testing
    NodeList list = new NodeList();
    // A response from the user
    String resp;
    // A single value entered by the user
    int val;
    // A list provided by the user
    NodeList otherlist;

    // Provide some basic instructions
    giveHelp(out);

    // Keep going until we hit the end of file or the user quits.
    // One exit is done using "break", and the other is done using
    // a catch of the end-of-file exception, so it looks as if our
    // loop continues indefinitely.
    try {
      LOOP:
      while (true)
      {
        // Print a prompt
        out.println();
        out.print("> ");
        // Get the first response
        resp = in.readString();
        // Make sure the response is non-empty
        if (resp.length() == 0) {
          out.println("You must give a non-empty response.");
          giveHelp(out);
          continue;
        }
        // Determine what to do based on the first character
        switch (resp.charAt(0)) {
          case 'a':				// append
            // Get the list and append it
            try {
              otherlist = readIntList(in);
              list.appendList(otherlist);
            }
            catch (Exception e) {
              out.println("Invalid input");
            }
            break;

          case 'd':				// delete the first element
            try {
              list.deleteFirst();
            }
            catch (Exception e) {
              out.println("Failed to delete the element (empty list?)");
            }
            break;
         
          case 'e':				// add to end
            // Get the value
            try {
              val = in.readInt();
              list.addToEnd(new Node(new ComparableInteger(val)));
            } // try
            catch (Exception e) {
              out.println("Invalid input");
            }
            break;

          case 'h':				// help
            giveHelp(out);
            break;

          case 'l':				// length
            out.println("The length is " + list.length());
            break;

          case 'p':				// print
            out.println(list.toString());
            break;

          case 'q':				// quit
            break LOOP;
          
          default:				// error
            out.println("'" + resp + "' is an invalid response.");
            giveHelp(out);
            break;
        } // switch
        // Skip anything else on the line
        in.readLine();
      } //while
    } // try doing the input
    catch (IOException e) {
      out.println("Program terminated.");
    } // catch(IOException)

  } // main
  
} // NodeListTester

