package tmcsim.cadsimulator.viewer;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.WindowEvent;

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 tmcsim.cadsimulator.videocontrol.DVDStatusUpdate;
import tmcsim.cadsimulator.videocontrol.DVDTitleUpdate;
import tmcsim.cadsimulator.viewer.actions.ExitAction;
import tmcsim.common.CADEnums.PARAMICS_STATUS;
import tmcsim.common.CADEnums.SCRIPT_STATUS;
import tmcsim.common.RevisionNumber;

/**
 * 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 {

    /** Panel to display simulation information. */
    private SimulationStatusPanel simulationPanel;
    
    /** Panel to display media control information. */
    private MediaStatusPanel mediaPanel;
    
    /** Constructor. */
    public CADSimulatorViewer() {
        super("CAD Simulator");
        
        initComponents();
    }    
    
    /** 
     * @see SimulationStatusPanel
     */
    public void connectClient() {
        simulationPanel.connectClient();
    }
    
    /** 
     * @see SimulationStatusPanel
     */
    public void disconnectClient() {
        simulationPanel.disconnectClient();
    }
    
    /** 
     * @see SimulationStatusPanel
     */
    public void setSimManagerStatus(boolean connection) {
        simulationPanel.setSimManagerStatus(connection);        
    }
    
    /** 
     * @see SimulationStatusPanel
     */
    public void setTime(long seconds) {
        simulationPanel.setTime(seconds);
    }

    /** 
     * @see SimulationStatusPanel
     */
    public void setScriptStatus(SCRIPT_STATUS newStatus) {
        simulationPanel.setScriptStatus(newStatus);         
    }
    
    /** 
     * @see SimulationStatusPanel
     */
    public void setParamicsStatus(PARAMICS_STATUS newStatus) {
        simulationPanel.setParamicsStatus(newStatus);       
    }
    
    /** 
     * @see SimulationStatusPanel
     */
    public void setParamicsNetworkLoaded(String networkID) {
        simulationPanel.setParamicsNetworkLoaded(networkID);        
    }

    /** 
     * @see MediaStatusPanel
     */
    public void updateDVDStatus(DVDStatusUpdate update) {
        mediaPanel.updateDVDStatus(update);
    }

    /** 
     * @see MediaStatusPanel
     */
    public void updateDVDTitle(DVDTitleUpdate update) {
        mediaPanel.updateDVDTitle(update);
    }
    
    /**
     * 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();
        
        cadSimTabbedPane = new JTabbedPane();
        cadSimTabbedPane.addTab("Status", simulationPanel);
        cadSimTabbedPane.addTab("Media", mediaPanel);
        
        add(cadSimTabbedPane);      

        menubar = new JMenuBar();
        
        fileMenu = new JMenu("File");
        menubar.add(fileMenu);
        
        exitMenuItem = new JMenuItem(new ExitAction(this));
        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(false);
    }
    
    
    private JTabbedPane cadSimTabbedPane;
    
    private JMenuBar menubar;
    
    private JMenu fileMenu;
    
    private JMenuItem exitMenuItem;
    
}