
package atmsdriver.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Traffic Event represents some occurrence in the traffic on 
 * a highway network.  A traffic event occurs at a particular time
 * and belongs to a unique simulation incident.  The event occurs on 
 * a specified section of a given highway route.  Each event specifies
 * the level of traffic congestion by a color.  Events are comparable
 * by the time of their occurrence.
 * @author jdalbey
 */
public final class TrafficEvent implements Comparable<TrafficEvent>
{
    public final String incident;
    public final String eventTime;
    public final Date eventDate;  // for convenience
    public final int routeNumber;
    public final LoopDetector.DOTCOLOR color;
    public final Station.DIRECTION dir;
    public final double postmile;
    public final double range;
    public final String rawString;
    private final static SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

    /** Create an event from a string as in this example:
     * 181 00:12:30 405 S 0.6 11.0 G
     * @param eventString
     * @return traffic event
     * @throws Scanner exception if string improperly formatted
     */
    public TrafficEvent(String eventString) throws ParseException
    {
        this.rawString = eventString;
        Scanner lineScan = new Scanner(eventString);
        this.incident = lineScan.next();
        this.eventTime = lineScan.next(); // time field
        // may throw parseexception
        this.eventDate = formatter.parse(eventTime);
        this.routeNumber = lineScan.nextInt();
        this.dir = Station.DIRECTION.toDirection(lineScan.next());
        this.postmile = lineScan.nextDouble();
        this.range = lineScan.nextDouble();
        this.color = LoopDetector.DOTCOLOR.toDotColor(lineScan.next());    
    }

    @Override
    public int compareTo(TrafficEvent o)
    {
        return eventDate.compareTo(o.eventDate);
    }
    
    @Override
    public String toString()
    {
        return rawString;
    }
}
