package tmcsim.highwaymodel;

import tmcsim.highwaymodel.Highways;
import tmcsim.highwaymodel.TrafficEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
import tmcsim.cadsimulator.managers.TrafficModelManager;
import tmcsim.common.SimulationException;

/**
 * OBSOLETE: An interactive version is now available:
 *    atmsdriver.TrafficEventsAnimator.java
 * Read and process all Traffic Events in a file and show
 * resulting highway network. 
 * A console display of the highway network is output
 * for each event.
 * This application is used by Traffic Event authors to assist
 * in verifying the correctness of their data file.
 * Because the output is sent immediately to the console the
 * author doesn't have to sit through simulation time to
 * observe dots changing on ATMS client.
 * @author jdalbey
 */
public class TrafficModelEventDriver 
{
    /**
     * Error logger.
     */
    private static Logger logger = Logger.getLogger("trafficmodeleventdriver");

    /**
     * Highways in traffic network
     */
    final private Highways highways;

    /**
     * LinkedList of batch events
     */
    private LinkedList<TrafficEvent> eventQueue;
    /**
     * Map of incidents to events
     */
    private Map<String, List<TrafficEvent>> incidents;

    /**
     * Constructor. Load highways and events.
     *
     */
    public TrafficModelEventDriver() throws RemoteException, SimulationException
    {
        // Initialize the highway model
        incidents = new HashMap<String, List<TrafficEvent>>();
        highways = new Highways(
                "config/vds_data/highways_fullmap.txt");
        final String CONFIG_FILE_NAME = "traffic_model_config.properties";
        String propertiesFile = "config" + System.getProperty("file.separator") 
                 + CONFIG_FILE_NAME;
        Properties props = TrafficModelManager.loadProperties(propertiesFile);

        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(props.getProperty("Events_File"));
        } catch (FileNotFoundException ex)
        {
            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, 
                    "Missing Traffic Events file " + props.getProperty("Events_File"));
            System.exit(-1);
        }
        Scanner fileScanner = new Scanner(fis);        
        // Read all lines from the file of events and put in a queue
        eventQueue = TrafficModelManager.readBatchFile(fileScanner);
    }
    public void run()
    {
        // If we have any events left to process
        while (!eventQueue.isEmpty())
        {
            // Get next event
            TrafficEvent nextEvent = eventQueue.peek();
            System.out.println("LAUNCHING EVENT: " + nextEvent.toString());
            // apply colorization to highways
            highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir,
                    nextEvent.postmile, nextEvent.range, nextEvent.color);
            System.out.println(highways.toString());
            // Remove this event from the queue, we're done with it.
            eventQueue.remove();
        }
    }

    /**
     * local main to launch the app.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args)
    {
        try
        {
            TrafficModelEventDriver driver = new TrafficModelEventDriver();
            driver.run();
        }
        catch (Exception e)
        {
            logger.logp(Level.SEVERE, "Traffic Model Event Driver", "Main",
                    "Error initializing application.");

            JOptionPane.showMessageDialog(new JWindow(), e.getMessage(),
                    "Error - Program Exiting", JOptionPane.ERROR_MESSAGE);

            System.exit(-1);
        }

    }

}
