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

/**
 * A track.
 
 * YOU WILL USE THIS CLASS.  
 * THERE IS NO NEED FOR YOU TO MODIFY THIS CLASS.
 */
public class Track {

    public final static int EW = 1; // direction east/west
    public final static int NS = 0; // direction north/south

    // The direction of the track.
    private int direction;
    
    // The location of the track
    // measured in characters from the frame border.
    private int location;
    
    // The colour of the train.
    private Color colour; 
    
    // The train on this track.
    private Train myTrain;
    
    // Representation invariants:
    //
    // - direction is either EW or NS.
    // - location is between 0 and rightEdge for an NS track 
    //   or between 0 and bottomEdge for an EW track.
    // - colour is Color.lightGrey.
    // - myTrain is the train that runs on this track.

    /**
     * Constructs a track.
     */
 
     public Track() {

        colour = Color.lightGray; 
    }    
    
    /**
     * Sets the instance variables for a track.
     *
     * @param  in  MyStreamTokenizer containing tokens for direction
     * and location.
     *
     * After setting up the train on this track any remaining 
     * tokens on the current input line are discarded.
     * 
     * (The complete format of the file is describe in the 
     * TrainSetDriver class file.)
     */
    
    public void addDetails(MyStreamTokenizer in) throws 
        IOException {
    
        int tokenType = in.nextToken();

        if (in.sval.equals("east/west"))
            direction = EW;
        else 
            direction = NS;

        tokenType = in.nextToken();
        location = in.nval.intValue();
        
        myTrain = new Train(this);
        myTrain.addDetails(in);
    }
    
    /*
     *  Gets this track's direction.
     *
     *  @return  The direction of this track.
     */
    public int getDirection() {
    
        return direction;
    }
    
    /*
     *  Gets this track's location.
     *
     *  @return  The location of this track.
     */
    public int getLocation() {
    
        return location;
    }
    
    /*
     * Starts the train on this track.
     */
     
    public void startTrain() {
        
        myTrain.start();
    }
    
    /*
     * Stops the train on this track.
     */
     
    public void stopTrain() {
    
        myTrain.stop();
    }
    
    /*
     * Moves the train on this track one space.
     */
     
    public void moveTrain() {
    
        myTrain.move();
    }


    /**
     * Draws this track.
     *
     * @param  g  the graphics context in which to draw this track.
     */
     
    public void draw(Graphics g) {
        g.setColor(colour);
        
        switch (direction) {
        
        case EW:
            for (int i=0; i<TrainSetDriver.frame.rightEdge; i++)
                TrainSetDriver.frame.drawString(g, "#",i, location);
            break;
        case NS:
            for (int i=0; i<TrainSetDriver.frame.bottomEdge; i++)
                TrainSetDriver.frame.drawString(g, "#",location, i);
        }
        
        myTrain.draw(g);
    }

}
