package tmcsim.cadsimulator.viewer;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Observable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import tmcsim.cadsimulator.managers.TrafficModelManager;
import tmcsim.cadsimulator.viewer.actions.ExitAction;
import tmcsim.cadsimulator.viewer.model.CADMediaStatus;
import tmcsim.cadsimulator.viewer.model.CADSimulatorStatus;
import tmcsim.interfaces.CADViewer;

/**
 * This class provides a GUI to view current status information for the CAD
 * Server.
 *
 * @see SimulationStatusPanel
 * @see MediaStatusPanel
 * @author Matthew Cechini
 * @version $Revision: 1.3 $ $Date: 2006/06/06 20:46:41 $
 */
@SuppressWarnings("serial")
public class CADServerViewer extends JFrame implements CADViewer
{
    private JTabbedPane cadSimTabbedPane;
    private JMenuBar menubar;
    private JMenu fileMenu;
    private JMenuItem exitMenuItem;

    /**
     * Panel to display simulation information.
     */
    private SimulationStatusPanel simulationPanel;
    /**
     * Panel to display media control information.
     */
    private MediaStatusPanel mediaPanel;
    /**
     * Panel to display configuration files.
     */
    private ConfigStatusTab configPanel;
    /*
     * Panel to display Traffic Model Event Queue
    */
    private TrafficModelViewPanel trafficPanel;
    
    /**
     * Constructor.
     */
    public CADServerViewer()
    {
        super();
        setTitle("CAD Server " + getAppVersion());

        initComponents();
    }

    /**
     * Read the version number from the application properties. The file
     * 'application.properties' is generated by build.xml.
     *
     * @return a version string obtained from application.properties file, or
     * "Version: unknown" if an IOerror prevents us from reading the file.
     */
    private String getAppVersion()
    {
        String propfilename = "/tmcsim/application.properties";
        String propKey = "Application.revision";
        String version = "unknown";
        try
        {
            Properties props = new Properties();
            props.load(this.getClass().getResourceAsStream(propfilename));
            version = (String) props.get(propKey);
        } catch (IOException ex)
        {
            Logger.getLogger("tmcsim/cadsimulator").log(Level.SEVERE,
                    "CADSimulatorView.getAppVersion()."
                    + " IOError reading " + propfilename);
        } catch (NullPointerException npe)
        {
            Logger.getLogger("tmcsim/cadsimulator").log(Level.SEVERE,
                    "CADSimulatorView.getAppVersion().load."
                    + " Missing file: " + propfilename);
        }
        return "revision: " + version;
    }

    /**
     * Method calls the processEvent() method with a WINDOW_CLOSING WindowEvent
     * to start the application closing process.
     */
    public void closeViewer()
    {
        processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }

    /**
     * Overloads the processEvent method. If the AWTEvent is a WINDOW_CLOSING
     * event, prompt the user to confirm the action. If confirmed, close the
     * application.
     */
    protected void processEvent(AWTEvent evt)
    {

        if (evt.getID() == WindowEvent.WINDOW_CLOSING)
        {
//            int option = JOptionPane.showConfirmDialog(null,
//                    "Closing the CAD Simulator will stop the current "
//                    + "simulation.  Do you wish to continue exiting?",
//                    "Confirm Exit",
//                    JOptionPane.YES_NO_OPTION);
//
//            if (option != JOptionPane.NO_OPTION)
//            {
            System.exit(0);
//            }
        }
    }

    /**
     * Initialize GUI Components
     */
    private void initComponents()
    {
        simulationPanel = new SimulationStatusPanel();
        mediaPanel = new MediaStatusPanel();
        configPanel = new ConfigStatusTab();
        trafficPanel = new TrafficModelViewPanel();

        cadSimTabbedPane = new JTabbedPane();
        cadSimTabbedPane.addTab("Status", simulationPanel);
        cadSimTabbedPane.addTab("Media", mediaPanel);
        cadSimTabbedPane.addTab("Config", configPanel);
        cadSimTabbedPane.addTab("Traffic", trafficPanel);

        add(cadSimTabbedPane);

        menubar = new JMenuBar();

        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menubar.add(fileMenu);

        exitMenuItem = new JMenuItem(new ExitAction(this));
        exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_X, ActionEvent.ALT_MASK));
        fileMenu.add(exitMenuItem);

        javax.swing.JMenu helpMenu = new javax.swing.JMenu("Help");

        menubar.add(helpMenu);

        setJMenuBar(menubar);

        setPreferredSize(new Dimension(500, 575));
        pack();
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void update(Observable obs, Object obj)
    {
        if (obs instanceof CADSimulatorStatus)
        {
            simulationPanel.refresh(obs);
        }
        if (obs instanceof CADMediaStatus)
        {
            mediaPanel.refresh(obs);
        }
        if (obs instanceof TrafficModelManager)
        {
            trafficPanel.update(obs, obj);
        }
    }
}
