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

/**
 * A car.
 
 * You will modify this class.
 */

public class Car {

    // The length of a railcar.
    public final static int CARLENGTH = 7;

    // The direction this car is moving.
    private int movement;
    
    // The train this car is part of.
    private Train myTrain;
       
    // The location of this car on its track
    // measured in character locations from 
    // the top or left edge of the frame border.
    private int locationOnTrack;
    
    // The colour of the car.
    private Color colour; 
    
    // Representation invariants:
    
    // - movement is FORWARD, BACKWARD or STOPPED.
    // - myTrain is the train this car is part of.
    // - locationOnTrack is between CARLENGTH and TrainFrame.bottomEdge 
    //   for a car in a train on a NS track or between CARLENGTH and 
    //   TrainFrame.rightEdge for a car in a train on an EW track. 
    // - colour is either Color.green, Color.blue, Color.magenta, 
    //   Color.red, Color.orange, Color.yellow or Color.black

        
    /**
     * Constructs a new car.
     
     * @param  location  The location of this car on its track.
     * @param  train  The train this car is part of.
     */

    public Car(int location, Train train) {
    
        locationOnTrack = location;
        myTrain = train;
        movement = Train.STOPPED;
    }
    

    /**
     * Sets the instance variables for this car using input file data.
     *
     * @param  in  MyStreamTokenizer containing tokens describing
     *         a car.
     * 
     * A token for the following is read:
     * colour of this car

     * (The complete format of the file is describe in the 
     * TrainSetDriver class file.)
     */
     
    public void addDetails(MyStreamTokenizer in) throws
        IOException {
                
        int tokenType = in.nextToken();
        colour = ColourIndex.findColor(in.sval);
    }
    
    /**
     * Returns the location of this car on a track. 
     * @return  Location of this car on its track.
     */
     
    public int getLocation() {
    
        return locationOnTrack;
    }
        
    /**
     * Draws this car.
     *
     * @param  g  the graphics context in which to draw this car.
     */    
     
    public void draw(Graphics g) {
    
        switch(getMyTrackDirection()){
        
        case Track.EW:
            g.setColor(Color.white);
            TrainSetDriver.frame.drawString(g, " #####", locationOnTrack-CARLENGTH, getMyTrackLocation());
            g.setColor(colour);
            TrainSetDriver.frame.drawString(g, " _____", locationOnTrack-CARLENGTH, getMyTrackLocation() - 2);
            TrainSetDriver.frame.drawString(g, "|_____|", locationOnTrack-CARLENGTH, getMyTrackLocation() - 1);
            g.setColor(Color.black);
            TrainSetDriver.frame.drawString(g, " oo oo", locationOnTrack-CARLENGTH, getMyTrackLocation());
            break;
        
        case Track.NS:
            g.setColor(Color.white);
            TrainSetDriver.frame.drawString(g, "#", getMyTrackLocation(), locationOnTrack-5);
            TrainSetDriver.frame.drawString(g, "#", getMyTrackLocation(), locationOnTrack-4);
            TrainSetDriver.frame.drawString(g, "#", getMyTrackLocation(), locationOnTrack-3);
            TrainSetDriver.frame.drawString(g, "#", getMyTrackLocation(), locationOnTrack-2);
            TrainSetDriver.frame.drawString(g, "#", getMyTrackLocation(), locationOnTrack-1);
            g.setColor(colour);
            TrainSetDriver.frame.drawString(g, "-", getMyTrackLocation()-1, locationOnTrack-6);
            TrainSetDriver.frame.drawString(g, "||", getMyTrackLocation()-2, locationOnTrack-5);
            TrainSetDriver.frame.drawString(g, "||", getMyTrackLocation()-2, locationOnTrack-4);
            TrainSetDriver.frame.drawString(g, "||", getMyTrackLocation()-2, locationOnTrack-3);
            TrainSetDriver.frame.drawString(g, "||", getMyTrackLocation()-2, locationOnTrack-2);
            TrainSetDriver.frame.drawString(g, "||", getMyTrackLocation()-2, locationOnTrack-1);
            TrainSetDriver.frame.drawString(g, "-", getMyTrackLocation()-1, locationOnTrack);
            g.setColor(Color.black);
            TrainSetDriver.frame.drawString(g, "o", getMyTrackLocation(), locationOnTrack-5);
            TrainSetDriver.frame.drawString(g, "o", getMyTrackLocation(), locationOnTrack-4);
            TrainSetDriver.frame.drawString(g, "o", getMyTrackLocation(), locationOnTrack-2);
            TrainSetDriver.frame.drawString(g, "o", getMyTrackLocation(), locationOnTrack-1);
        }

    }
    
    /*
     * Sets this car's direction of movement.
     * @param  movingDirection  int Indicates the
     *         direction this car is moving, 
     *         either stopped, forward or backwards.
     */
     
    public void updateMovementDirection(int movingDirection) {
        movement = movingDirection;
    }
    
    
    /*
     * Moves this car along one step.
     */
    public void move() {
        locationOnTrack = locationOnTrack + movement;
    }
    
    
    /**
     * Helper method to get my tracks direction.
     
     * @return  The direction of my track.
     */
    
    private int getMyTrackDirection() {
        return myTrain.getTrackDirection();
    }
     
    /**
     * Helper method to get my track's location.
     
     * @return  The location of my track.
     */
     
    private int getMyTrackLocation() {
        return myTrain.getTrackLocation();
    }
                         
}
