import java.awt.event.*;
import java.io.*;

/**
 * A train set simulation.  
 *
 * This program displays tracks and trains.
 *
 * THERE IS NO NEED TO MODIFY THIS CLASS.
 *
 * File Initialization:
 * --------------------
 * The main routine prompts the user for a filename from which to
 * read the initialization data.  Each line of this file describes
 * a track and the train on that track.  The tokens for a single
 * line of input, along with a description of their meaning, 
 * are listed below:
 
 *   direction  is a string, either north/south or east/west.
 *   location  indicates the distance of the track from the top
 *      border for east/west tracks or from the left border for 
 *      north/south tracks. It is measured in characters.
 *   startLocation  is the position of the front of the first car 
 *      in the train on the track. It is measured from the border.
 *   intialDirectionOfMovement  is a string either forward or backward.
 *   numberOfCars  is the count of the number of cars in the train.
 *      (In the starter version of the program there is always only one 
 *      car in each train.)
 *   listOfColours  is a list of colours, one for each of the cars in the
 *      train.
 */
 
public class TrainSetDriver {

    public static BufferedReader inKey = 
        new BufferedReader(new InputStreamReader(System.in));

    // The file from which trainset data is read.
    public static MyStreamTokenizer inFile;
    
    // The window that displays the trainset.
    public static TrainFrame frame = new TrainFrame();
    
    // The trainset.
    public static TrainSet theTrainSet = new TrainSet();    

    public static void main(String[] pars) throws IOException {

        // The window in which the trainset is displayed.
        
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // Open text file reader. 
        while(true) {
            System.out.print("Enter initialization filename : ");
            //Read from standard input (eg. console window)
            String inFileName = inKey.readLine().trim();
            try {
                inFile = new MyStreamTokenizer(inFileName);
                break;
            } catch (FileNotFoundException e) {
                System.out.println("Can't find file: "+inFileName);
                System.out.println("Try again.");
            }
        }

        System.out.println("Reading initialization file...");
        
        theTrainSet.setup(inFile);

        System.out.println("Closing file: "+inFile.fileName);
        inFile.close();

        frame.setSize(750, 500);
        frame.setLocation(10,10);
        frame.setVisible(true);
        
        theTrainSet.runSimulation(frame);
        
        System.out.println("Done");

    }
}
