package tmcsim.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import tmcsim.cadmodels.BlankScreenModel;
import tmcsim.cadmodels.CADScreenModel;
import tmcsim.cadmodels.IncidentBoardModel;
import tmcsim.cadmodels.IncidentInquiryModel;
import tmcsim.cadmodels.IncidentSummaryModel;
import tmcsim.cadmodels.RoutedMessageModel;
import tmcsim.client.cadscreens.IB_IncidentBoard;
import tmcsim.client.cadscreens.II_IncidentInquiry;
import tmcsim.client.cadscreens.SA_IncidentSummary;
import tmcsim.client.cadscreens.TO_RoutedMessage;
import tmcsim.client.cadscreens.view.CADCommandLineView;
import tmcsim.client.cadscreens.view.CADFooterView;
import tmcsim.client.cadscreens.view.CADMainView;
import tmcsim.common.ObserverMessage;
import tmcsim.common.CADEnums.ARROW;
import tmcsim.common.CADEnums.CADScreenNum;
import tmcsim.common.CADEnums.CAD_ERROR;
import tmcsim.common.CADEnums.CAD_KEYS;
import tmcsim.common.CADProtocol.CAD_CLIENT_CMD;
import tmcsim.cadsimulator.viewer.model.CADSimulatorStatus;        

/**
 * The CADClientView class is the view component to the CAD Client application.
 *
 * This view class observers the CADClientModel, listening for updates
 * from the CAD Simulator.  Updates includes the current 
 * CAD time  */
@SuppressWarnings("serial")
public class CADClockView extends JFrame implements KeyListener, Observer {
    
    /** Error Logger. */
    private static Logger cadLogger = Logger.getLogger("tmcsim.client");
    
    /** Reference to the CADClient model object. */
    private CADClientModel theModel = null;
              
    /** Current CAD Screen number. */
    private CADScreenNum currentScreenNum = null;
    private JPanel mainPane;
    private JLabel currentTime;
    
    /**
     * Constructor. Build panes, add key listeners, and set up observer
     * relationship between the footer and main panes.
     * 
     * @param position
     *            The CAD position for this client terminal.
     */
    public CADClockView(CADClientModel mod) {
        super("CAD Client");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        theModel = mod;
        currentTime = new JLabel("0:00:00");
        CADSimulatorStatus status = new CADSimulatorStatus();
        String simtime = status.getCurrentTime();
        currentTime.setText(simtime);
        currentTime.setAlignmentX(Box.CENTER_ALIGNMENT);
        currentTime.setFont(new Font("Geneva", Font.BOLD, 70));        
        mainPane = new JPanel();
        setSize(new Dimension(730, 455));
        setMaximumSize(new Dimension(730, 455));
        setMinimumSize(new Dimension(730, 455));
        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
        mainPane.setBorder(BorderFactory.createLineBorder(Color.black));   
        mainPane.setBackground(Color.LIGHT_GRAY);
        mainPane.add(currentTime);
        add(mainPane); 
        pack();
    }

    /**
     * Observable update method.  The CADClientView class is an observer of the 
     * CADClientModel.  If the model sends a null object, it is signifying that
     * it has shut down.  In this case, an error message should be shown to prompt
     * the user to restart the CAD Client.  If the update object is an
     * ObserverMessage object, the following actions are to be taken:
     * 
     *<table cellpadding="2" cellspacing="2" border="1"
     * style="text-align: left; width: 250px;">
     *  <tbody>
     *    <tr>
     *      <th>Message Type</th>
     *      <th>Message Data</th>
     *      <th>Action Taken</th>
     *    </tr>
     *    <tr>
     *      <td>TIME_UPDATE<br></td>
     *      <td>Time String<br></td>
     *      <td>Update the footer pane with the new time.</td>
     *    </tr>
     *  </tbody>
     *</table>
     */
    public void update(Observable o, Object arg) {
        
        
        if(arg == null) 
        {
            JOptionPane.showMessageDialog(this, 
                    "Connection to the CAD Simulator has been lost.  " +
                    "Restart the CAD Client.", "Connection Error", 
                    JOptionPane.ERROR_MESSAGE); 
            return;
        }
        
        ObserverMessage oMessage = (ObserverMessage)arg;    
        
        switch(oMessage.type) {
            // Time updates occur once a minute
            case TIME_UPDATE:
                currentTime.setText("" + (String)oMessage.value);
                break;
        }           
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
   
    
}    
