package atmsdriver;

import atmsdriver.model.Highways;
import atmsdriver.model.TrafficEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JWindow;

/**
 * Skeleton for ATMS Driver that reads a "batch" file of highway status update
 * commands.  A console display of the highway network is output
 * for each event.
 * @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
    {
        // Initialize the highway model
        incidents = new HashMap<String, List<TrafficEvent>>();
        highways = new Highways(
                "config/vds_data/highways_fullmap.txt",
                //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM
                "localhost", 8080);

        // READ THE BATCH FILE OF COMMANDS and put in a queue
        readBatchFile();
    }
    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();
        }
    }

    private void readBatchFile()
    {
        FileInputStream fis;
        try
        {
            fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt");
            eventQueue = new LinkedList<TrafficEvent>();
            // Read all lines from the file of events
            Scanner scan = new Scanner(fis);
            while (scan.hasNext())
            {
                // Read a line and add it to the event queue
                String line = scan.nextLine().trim();
                if (line.charAt(0) != '#')
                {
                    TrafficEvent evt;
                    try
                    {
                        evt = new TrafficEvent(line);
                        eventQueue.add(evt);
                        String incident = evt.incident;
                        // Add the line to the list for the corresponding incident
                        List evtList;
                        if (incidents.containsKey(evt.incident))
                        {
                            evtList = incidents.get(evt.incident);
                        }
                        else
                        {
                            evtList = new ArrayList<String>();
                        }
                        evtList.add(evt);
                        // and put it back in the map
                        incidents.put(incident, evtList);
                    }
                    catch (ParseException ex)
                    {
                        Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex);
                        System.out.println("Wrong format data in batch event file: " + line + " \nskipping.");
                        System.out.println("Skipping badly formatted event.");
                    }
                }
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Events file read, " + eventQueue.size() + " events queued.");
        // Put the events in chronological order
        Collections.sort(eventQueue);
    }

    /**
     * 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);
        }

    }

}
