/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmcsim.client.cadclientgui.screens;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;

import tmcsim.client.cadclientgui.enums.CADDataEnums.INC_TABLE;

/**
 *
 * @author Stuart
 */
public class PowerlineUI extends javax.swing.JFrame {

    public static enum HINT{
        II  ("II <Incident> Comment"),
        UI              ("UI <Incident> Comment"),
        RCARD       ("RCARD SearchText[NEW]"),
        CARD        ("CARD"),
        OFF ("OFF");
        
        public String tag;
        
        private HINT(String t) {
            tag = t;
        }
    }
    
    public static enum DESCRIPTION{
        II  ("Inquire Incident/Update Incident"),
        UI              ("Update Incident Details"),
        RCARD       ("Recall: Cardfile Search"),
        CARD        ("Open Cardfile"),
        OFF ("Exit System");
        
        public String tag;
        
        private DESCRIPTION(String t) {
            tag = t;
        }
    }
    
    /**
     * Creates new form PrototypeUI
     */
    public PowerlineUI(boolean visible, int number) {
        initComponents(visible,number);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents(boolean visible, int number) {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jToolBar1 = new javax.swing.JToolBar();
        cmdLabel = new javax.swing.JLabel();
        cmdComboField = new javax.swing.JComboBox();
        hintLabel = new javax.swing.JLabel();
        descriptionLabel = new javax.swing.JLabel();

        jToolBar1.setRollover(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Powerline");

        cmdLabel.setText("CMD:");
        
        cmdComboField.setEditable(true);
        ((javax.swing.text.JTextComponent) cmdComboField.getEditor().getEditorComponent())
            .getDocument().addDocumentListener(new DocumentListener(){
                
            public void changedUpdate(DocumentEvent e) {
                updateHint(e);
            }
            public void insertUpdate(DocumentEvent e) {
                updateHint(e);
            }
            public void removeUpdate(DocumentEvent e) {
                updateHint(e);
            }
         });
        
        cmdComboField.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(((String)cmdComboField.getEditor().getItem()).equals("OFF")){
                    System.exit(0);
                }
                else if(((String)cmdComboField.getEditor().getItem()).equals("CARD")){
                    ScreenManager.openCardfile();
                }
                else if(((String)cmdComboField.getEditor().getItem()).startsWith("RCARD")){
                    Scanner scanner = new Scanner((String)cmdComboField.getEditor().getItem());
                    scanner.next();
                    if(!scanner.hasNext()){
                        return;
                    }
                    String search = scanner.next();
                    ScreenManager.createPowerlineSearch(search);
                    
                }
                else if(((String)cmdComboField.getEditor().getItem()).startsWith("II")){
                    Scanner scanner = new Scanner((String)cmdComboField.getEditor().getItem());
                    scanner.next();
                    if(!scanner.hasNextInt()){
                        return;
                    }
                    int incidentId = scanner.nextInt();
                    ScreenManager.openIncidentViewer(incidentId);
                }
                else if(((String)cmdComboField.getEditor().getItem()).startsWith("UI")){
                    Scanner scanner = new Scanner((String)cmdComboField.getEditor().getItem());
                    scanner.next();
                    if(!scanner.hasNextInt()){
                        return;
                    }
                    int incidentId = scanner.nextInt();
                    try {
                        if(ScreenManager.theCoordinator.checkForValidIncidentID(incidentId)){
                            if(!scanner.hasNext()){
                                return;
                            }
                            String comment = scanner.next();
                            DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                       DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
                       String[] fields = {dateFormat.format(new Date()),
                                                  timeFormat.format(new Date()),
                                                  "","",comment};
                            try {
                                ScreenManager.theCoordinator.addCadDataIncidentTable(
                                 INC_TABLE.COMMENTS_NOTES, incidentId, fields);
                            } catch (RemoteException e1) {
                                e1.printStackTrace();
                            }
                            ScreenManager.openIncidentViewer(incidentId);
                            cmdComboField.getEditor().setItem("");
                        }
                    } catch (RemoteException e1) {
                        e1.printStackTrace();
                    }
                }
                
                cmdComboField.getEditor().setItem("");
                hintLabel.setText("");
                descriptionLabel.setText("");
            }
        });
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdComboField, 0, 454, Short.MAX_VALUE)
                .addContainerGap())
             .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(hintLabel))
             .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(descriptionLabel))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdLabel)
                    .addComponent(cmdComboField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(hintLabel)
            .addComponent(descriptionLabel)
            .addContainerGap(35, Short.MAX_VALUE))
        );

        setTitle("<" + number + ">Powerline");
        if (visible){
            setVisible(true);
        }else{
            setVisible(false);
        }
        pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int)(dim.getWidth() - this.getWidth()),0);
    }// </editor-fold>//GEN-END:initComponents

    public void updateHint(DocumentEvent e){
        try {
            if(e.getDocument().getLength() > 0){
                String firstLetter = e.getDocument().getText(0, 1);
                if(firstLetter.equals("O")){
                    hintLabel.setText(HINT.OFF.tag);
                    descriptionLabel.setText(DESCRIPTION.OFF.tag);
                }
                else if(firstLetter.equals("I")){
                    hintLabel.setText(HINT.II.tag);
                    descriptionLabel.setText(DESCRIPTION.II.tag);
                }
                else if(firstLetter.equals("U")){
                    hintLabel.setText(HINT.UI.tag);
                    descriptionLabel.setText(DESCRIPTION.UI.tag);
                }
                else if(firstLetter.equals("R")){
                    hintLabel.setText(HINT.RCARD.tag);
                    descriptionLabel.setText(DESCRIPTION.RCARD.tag);
                }
                else if(firstLetter.equals("C")){
                    hintLabel.setText(HINT.CARD.tag);
                    descriptionLabel.setText(DESCRIPTION.CARD.tag);
                }
                else {
                    hintLabel.setText("");
                    descriptionLabel.setText("");
                }
            }
            
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }
    
    /*
     * Clears text.
     */
    public void clearText(){
     cmdComboField.getEditor().setItem("");
    }
    
    /*
     * Puts "RCARD" in textfield.
     */
    public void putRCARD(){
     cmdComboField.getEditor().setItem("RCARD");
    }
    
    /* 
     * Makes screen visible.
     */
    public void open(){
        setVisible(true);
    }
    
    /*
     * Hides screen.
     */
    public void close(){
        setVisible(false);
    }
    
    /*public void submitCmd() {     
        if (jComboBox1.getSelectedItem() instanceof String) {
            try {
                Document cmdDoc = DocumentBuilderFactory.newInstance()
                 .newDocumentBuilder().newDocument();
                Element cmdElem = cmdDoc.createElement(CAD_CLIENT_CMD.TERMINAL_CMD_LINE.type);
                
                parser.parseCommand(cmdElem, (String)jComboBox1.getSelectedItem());
                cmdDoc.appendChild(cmdElem);
            
                theModel.transmitCommand(cmdDoc);
            }
            catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmdComboField;
    private javax.swing.JLabel cmdLabel;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel hintLabel;
    private javax.swing.JLabel descriptionLabel;
    // End of variables declaration//GEN-END:variables
}
