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.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
 * Simulator.
 *
 * @see SimulationStatusPanel
 * @see MediaStatusPanel
 * @author Matthew Cechini
 * @version $Revision: 1.3 $ $Date: 2006/06/06 20:46:41 $
 */
@SuppressWarnings("serial")
public class CADSimulatorViewer extends JFrame implements CADViewer
{

    /**
     * Panel to display simulation information.
     */
    private SimulationStatusPanel simulationPanel;
    /**
     * Panel to display media control information.
     */
    private MediaStatusPanel mediaPanel;
    /**
     * Panel to display configuration files.
     */
    private ConfigStatusPanel configPanel;

    /**
     * Constructor.
     */
    public CADSimulatorViewer()
    {
        super();
        setTitle("CAD Simulator " + 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 ConfigStatusPanel();

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

        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");
        javax.swing.JMenuItem aboutItem = new javax.swing.JMenuItem("About");

        aboutItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                String ver = "";//RevisionNumber.getString();
                JOptionPane.showMessageDialog(rootPane, "Version: " + ver, "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        helpMenu.add(aboutItem);
        menubar.add(helpMenu);

        setJMenuBar(menubar);

        setPreferredSize(new Dimension(500, 575));
        pack();
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private JTabbedPane cadSimTabbedPane;
    private JMenuBar menubar;
    private JMenu fileMenu;
    private JMenuItem exitMenuItem;

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