package event.editor.frame;

import event.editor.I_ScriptEventEditorPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import scriptbuilder.gui.IncidentEditorFrame;
import scriptbuilder.gui.ScriptBuilderFrame;
import scriptbuilder.gui.ScriptBuilderGuiConstants;
import scriptbuilder.gui.panels.IncidentTimelinePanel;
import scriptbuilder.structures.ScriptEvent;
import scriptbuilder.structures.ScriptIncident;
import scriptbuilder.structures.TimeSlice;
import scriptbuilder.structures.events.*;

public class Editor extends javax.swing.JFrame implements Observer
{

    IncidentEditorFrame topFrame = null;

    ScriptIncident incident = null;

    TimeSlice slice = null;

    private PropertyModel model = new PropertyModel();

    public void addEvent(Properties property, I_ScriptEvent se)
    {
        model.addEventPanel(property, se);
    }
    /**
     * adds all of the events from the timeslice to the incidentTimelinePanel
     * @param ts the timeslice to be added
     */
    public void setSlice(TimeSlice ts)
    {
        slice = ts;

        if (slice != null)

        {
            for (I_ScriptEvent se : slice.events)
            {
                
                this.addEvent(IncidentTimelinePanel.eventTypeToPropertyMap.get(se.getScriptEventType()), se);

            }
            System.out.println("Current time = " + slice.getTime() + " seconds");
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            String eventTime = df.format(new Date(slice.getTime() * 1000));
            System.out.println("[" + eventTime + "]");
            
            List<Integer> timesS = timeGenerator(eventTime,"s");
            List<Integer> timesM = timeGenerator(eventTime,"m");
            List<Integer> timesH = timeGenerator(eventTime,"h");
            timeHourComboSelector.setModel(new DefaultComboBoxModel(timesH.toArray()));
            timeMinuteComboSelector.setModel(new DefaultComboBoxModel(timesM.toArray()));
            timeSecondComboSelector.setModel(new DefaultComboBoxModel(timesS.toArray()));
            timeHourComboSelector.setSelectedItem(slice.getTime()/3600);
            timeMinuteComboSelector.setSelectedItem((slice.getTime()%3600)/60);
            timeSecondComboSelector.setSelectedItem(slice.getTime()%60);
        }

    }
    
    private List<Integer> timeGenerator(String time,String type)
    {
        List<Integer> times = new ArrayList<Integer>();
        String[] timeArray = time.split(":");
        int currTime = 0;
        switch(type)
        {
            case "h":
            {
                currTime = Integer.parseInt(timeArray[0]);
                for(int i = 0;i< ScriptBuilderGuiConstants.MAX_SIMULATION_LENGTH/3600;i++)
                {
                    times.add(i);
                }
                break;
            }
            case "m":
            {
                currTime = Integer.parseInt(timeArray[1]);
                for(int i = 0; i<60;i++)
                {
                    times.add(i);
                }
                
                break;
            }
            case "s":
            {
                //includes times 0,20,40,80
                currTime = Integer.parseInt(timeArray[2]); 
                for(int i = 0; i<60;i+=ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION)
                {
                    times.add(i);
                }
                break;
            }
            default:
            {
            }
                
        }
        
        
        //times.add(time);
        
        return times;
    }
    
    

    private ActionListener optionalChangeListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent evt)
        {
            JCheckBoxMenuItem src = (JCheckBoxMenuItem) evt.getSource();
            Properties property = Properties.valueOf(src.getText().replaceAll(" ", ""));

            if (src.isSelected())
            {
                model.addEventFromDropdown(property, incident, slice);
            }
            else
            {
                model.removeProperty(property);
            }
        }
    };

    private ActionListener multipleChangeListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent evt)
        {
            JMenuItem src = (JMenuItem) evt.getSource();
            Properties property = Properties.valueOf(src.getText().replaceAll(" ", ""));
            model.addEventFromDropdown(property, incident, slice);
        }
    };

    /**
     * Blank constructor for a new editor.
     */
    public Editor(IncidentEditorFrame frame)
    {
        topFrame = frame;
        initComponents();

        incident = frame.getIncident();

        model.addObserver(this);

        // For each menu
        for (int menuCtr = 0; menuCtr < editorMenuBar.getMenuCount(); menuCtr++)
        {
            // for each menu item
            for (java.awt.Component comp : editorMenuBar.getMenu(menuCtr).getMenuComponents())
            {
                JMenuItem item = (JMenuItem) comp;

                String itemName = item.getText().replaceAll(" ", "");

                Properties property = Properties.valueOf(itemName);

                if (property.getType() == PropertyTypes.Multiple)
                {
                    item.addActionListener(multipleChangeListener);
                }
                else if (property.getType() == PropertyTypes.Optional)
                {
                    item.addActionListener(optionalChangeListener);
                }
                else
                {
                    throw new RuntimeException("Property type not accounted for");
                }
            }

        }
        //this eventTime might need to be initialized to the correct time
        String eventTime = "";

        
        this.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                //Add previous offset back in
                //If we didn't adjust the offset, this will just set it to the old value
                //If we deleted the first event(s), this will add the offsets,
                //to ensure that events stay at the correct times
                Object[] options = {"Cancel","OK"};
                int result = JOptionPane.showOptionDialog(null,
                        "Are you sure you want to exit without saving?",
                        "Confirm Exit",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        options,
                        options[1]);
                // Take action depending on user choice
                switch (result)
                {
                    //OK: should just close
                    case 1:
                        closeWindow();
                        break;
                    // Cancel: should do nothing
                    case 0:                            
                        break;
                    default:
                        break;
                }
            }
        });
    }
    private void closeWindow(){
        this.dispose();
    }

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

        eventTabsPane = new javax.swing.JTabbedPane();
        bottomFramePanel = new javax.swing.JPanel();
        saveCloseBtn = new javax.swing.JButton();
        btnRemoveCurrentEvent = new javax.swing.JButton();
        timeSecondComboSelector = new javax.swing.JComboBox<>();
        timeMinuteComboSelector = new javax.swing.JComboBox<>();
        timeHourComboSelector = new javax.swing.JComboBox<>();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        editorMenuBar = new javax.swing.JMenuBar();
        menuAutoData = new javax.swing.JMenu();
        Audio = new javax.swing.JMenuItem();
        CADLog = new javax.swing.JMenuItem();
        CCTV = new javax.swing.JMenuItem();
        CHPRadio = new javax.swing.JCheckBoxMenuItem();
        Paramics = new javax.swing.JMenuItem();
        Tow = new javax.swing.JMenuItem();
        Unit = new javax.swing.JMenuItem();
        Witness = new javax.swing.JMenuItem();
        menuInstructor = new javax.swing.JMenu();
        MaintenanceRadio = new javax.swing.JCheckBoxMenuItem();
        TMTRadio = new javax.swing.JCheckBoxMenuItem();
        Telephone = new javax.swing.JCheckBoxMenuItem();
        menuEvaluations = new javax.swing.JMenu();
        ATMS = new javax.swing.JCheckBoxMenuItem();
        ActivityLog = new javax.swing.JCheckBoxMenuItem();
        CAD = new javax.swing.JCheckBoxMenuItem();
        CMS = new javax.swing.JMenuItem();
        Facilitator = new javax.swing.JCheckBoxMenuItem();
        Radio = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Event Editor");

        org.jdesktop.layout.GroupLayout bottomFramePanelLayout = new org.jdesktop.layout.GroupLayout(bottomFramePanel);
        bottomFramePanel.setLayout(bottomFramePanelLayout);
        bottomFramePanelLayout.setHorizontalGroup(
            bottomFramePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 70, Short.MAX_VALUE)
        );
        bottomFramePanelLayout.setVerticalGroup(
            bottomFramePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 45, Short.MAX_VALUE)
        );

        saveCloseBtn.setText("Save and Close");
        saveCloseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCloseBtnActionPerformed(evt);
            }
        });

        btnRemoveCurrentEvent.setText("Remove This Event");
        btnRemoveCurrentEvent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveCurrentEventActionPerformed(evt);
            }
        });

        timeSecondComboSelector.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Second" }));
        timeSecondComboSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timeSecondComboSelectorActionPerformed(evt);
            }
        });

        timeMinuteComboSelector.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Minute" }));
        timeMinuteComboSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timeMinuteComboSelectorActionPerformed(evt);
            }
        });

        timeHourComboSelector.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Hour" }));
        timeHourComboSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timeHourComboSelectorActionPerformed(evt);
            }
        });

        jLabel1.setText("Hour");

        jLabel2.setText("Minute");

        jLabel3.setText("Second");

        menuAutoData.setText("Automated Events");

        Audio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        Audio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Audio.png"))); // NOI18N
        Audio.setText("Audio");
        Audio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AudioActionPerformed(evt);
            }
        });
        menuAutoData.add(Audio);

        CADLog.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        CADLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CAD.png"))); // NOI18N
        CADLog.setText("CAD Log");
        menuAutoData.add(CADLog);

        CCTV.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        CCTV.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CCTV.png"))); // NOI18N
        CCTV.setText("CCTV");
        menuAutoData.add(CCTV);

        CHPRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        CHPRadio.setText("CHP Radio");
        CHPRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CHPRadio.png"))); // NOI18N
        CHPRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuAutoData.add(CHPRadio);

        Paramics.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        Paramics.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Paramics.png"))); // NOI18N
        Paramics.setText("Paramics");
        menuAutoData.add(Paramics);

        Tow.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        Tow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Tow.png"))); // NOI18N
        Tow.setText("Tow");
        menuAutoData.add(Tow);

        Unit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        Unit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Unit.png"))); // NOI18N
        Unit.setText("Unit");
        menuAutoData.add(Unit);

        Witness.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        Witness.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Witness.png"))); // NOI18N
        Witness.setText("Witness");
        menuAutoData.add(Witness);

        editorMenuBar.add(menuAutoData);

        menuInstructor.setText("Instructor Actions");

        MaintenanceRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        MaintenanceRadio.setText("Maintenance Radio");
        MaintenanceRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/MaintenanceRadio.png"))); // NOI18N
        MaintenanceRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuInstructor.add(MaintenanceRadio);

        TMTRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        TMTRadio.setText("TMT Radio");
        TMTRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/TMTRadio.png"))); // NOI18N
        TMTRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuInstructor.add(TMTRadio);

        Telephone.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        Telephone.setText("Telephone");
        Telephone.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Telephone.png"))); // NOI18N
        Telephone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuInstructor.add(Telephone);

        editorMenuBar.add(menuInstructor);

        menuEvaluations.setText("Evaluation Actions");

        ATMS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        ATMS.setText("ATMS");
        ATMS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ATMSEval.png"))); // NOI18N
        ATMS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multipleChange(evt);
            }
        });
        menuEvaluations.add(ATMS);

        ActivityLog.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
        ActivityLog.setText("Activity Log");
        ActivityLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ActivityLogEval.png"))); // NOI18N
        ActivityLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multipleChange(evt);
            }
        });
        menuEvaluations.add(ActivityLog);

        CAD.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        CAD.setText("CAD");
        CAD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CADEval.png"))); // NOI18N
        CAD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multipleChange(evt);
            }
        });
        menuEvaluations.add(CAD);

        CMS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        CMS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CMSEval.png"))); // NOI18N
        CMS.setText("CMS");
        CMS.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                multipleChangeListener(evt);
            }
        });
        CMS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multipleChange(evt);
            }
        });
        menuEvaluations.add(CMS);

        Facilitator.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        Facilitator.setText("Facilitator");
        Facilitator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/FacilitatorEval.png"))); // NOI18N
        Facilitator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuEvaluations.add(Facilitator);

        Radio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        Radio.setText("Radio");
        Radio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/RadioEval.png"))); // NOI18N
        Radio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionalChange(evt);
            }
        });
        menuEvaluations.add(Radio);

        editorMenuBar.add(menuEvaluations);

        setJMenuBar(editorMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, eventTabsPane)
            .add(layout.createSequentialGroup()
                .add(84, 84, 84)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(timeHourComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(timeMinuteComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(timeSecondComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(bottomFramePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnRemoveCurrentEvent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(saveCloseBtn)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(eventTabsPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 523, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bottomFramePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(timeSecondComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(timeMinuteComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(timeHourComboSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel1)
                        .add(jLabel2)
                        .add(jLabel3))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(saveCloseBtn)
                        .add(btnRemoveCurrentEvent))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void multipleChangeListener(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_multipleChangeListener

    }//GEN-LAST:event_multipleChangeListener

    private void multipleChange(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_multipleChange

    }//GEN-LAST:event_multipleChange

    private void optionalChange(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optionalChange

    }//GEN-LAST:event_optionalChange

    private void btnRemoveCurrentEventActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnRemoveCurrentEventActionPerformed
    {//GEN-HEADEREND:event_btnRemoveCurrentEventActionPerformed

        int index = eventTabsPane.getSelectedIndex();
        //checks if there is a component at the tab selected, if so, then remove the associated event from the data model,
        //and if it is removed, then remove the associated editor window.
        if (index >= 0 && eventTabsPane.getTabComponentAt(index) != null)
        {
            JPanel removable = (JPanel) eventTabsPane
                    .getSelectedComponent();
            PropertyPanel removedProperty = this.model.properties.getProperty(removable);
            
            if(((I_ScriptEventEditorPanel) removedProperty.getPanel()).removeAssociatedEvent())
            {
                this.model.properties.removeProperty(removable);
            }

        }
        
        //prevents an "empty editor" from appearing
        if(eventTabsPane.getSelectedComponent() == null)
        {
            updateEventTime();
            this.closeWindow();
        }

    }//GEN-LAST:event_btnRemoveCurrentEventActionPerformed

    private void AudioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_AudioActionPerformed
    {//GEN-HEADEREND:event_AudioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_AudioActionPerformed

    private void saveCloseBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCloseBtnActionPerformed
        // TODO add your handling code here:
        //closes the current frame
        
        //updateEventTime();
        
        updateEventTime();
        
        model.closePanels();
        closeWindow();
        //this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_saveCloseBtnActionPerformed

    private void timeSecondComboSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timeSecondComboSelectorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_timeSecondComboSelectorActionPerformed

    private void timeMinuteComboSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timeMinuteComboSelectorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_timeMinuteComboSelectorActionPerformed

    private void timeHourComboSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timeHourComboSelectorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_timeHourComboSelectorActionPerformed

    private void updateEventTime()
    {
        
//        String[] tokens = txtEventStart.getText().split(":");
//        
//        int hrs = Integer.parseInt(tokens[0]);
//        int mins = Integer.parseInt(tokens[1]);
//        int secs = Integer.parseInt(tokens[2]);
        int hrs = Integer.parseInt(timeHourComboSelector.getSelectedItem().toString());
        int mins = Integer.parseInt(timeMinuteComboSelector.getSelectedItem().toString());
        int secs = Integer.parseInt(timeSecondComboSelector.getSelectedItem().toString());
        

        int newTime = (3600 * hrs) + (60 * mins) + secs;

        int index = eventTabsPane.getSelectedIndex();

        if (index >= 0 && eventTabsPane.getTabComponentAt(index) != null)
        {
            JPanel removable = (JPanel) eventTabsPane
                    .getSelectedComponent();
            I_ScriptEvent ise = this.model.eventMap.get(removable);
            if (newTime != slice.getTime() && !(ise instanceof AudioEvent) && incident.changeEventStart(ise, slice.getTime(), newTime))
            {
                this.model.properties.removeProperty(removable);

            }
//            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
//            df.setTimeZone(TimeZone.getTimeZone("GMT"));
//            String eventTime = df.format(new Date(slice.getTime() * 1000));
//            txtEventStart.setText(eventTime);
        }
        slice.checkEmpty();
    }
//
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[])
//    {
//        java.awt.EventQueue.invokeLater(new Runnable()
//        {
//            public void run()
//            {
//                new Editor(new IncidentEditorFrame(
//                        new ScriptIncident(100, null, null, null), 
//                        new ScriptBuilderFrame())).setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBoxMenuItem ATMS;
    private javax.swing.JCheckBoxMenuItem ActivityLog;
    private javax.swing.JMenuItem Audio;
    private javax.swing.JCheckBoxMenuItem CAD;
    private javax.swing.JMenuItem CADLog;
    private javax.swing.JMenuItem CCTV;
    private javax.swing.JCheckBoxMenuItem CHPRadio;
    private javax.swing.JMenuItem CMS;
    private javax.swing.JCheckBoxMenuItem Facilitator;
    private javax.swing.JCheckBoxMenuItem MaintenanceRadio;
    private javax.swing.JMenuItem Paramics;
    private javax.swing.JCheckBoxMenuItem Radio;
    private javax.swing.JCheckBoxMenuItem TMTRadio;
    private javax.swing.JCheckBoxMenuItem Telephone;
    private javax.swing.JMenuItem Tow;
    private javax.swing.JMenuItem Unit;
    private javax.swing.JMenuItem Witness;
    private javax.swing.JPanel bottomFramePanel;
    private javax.swing.JButton btnRemoveCurrentEvent;
    private javax.swing.JMenuBar editorMenuBar;
    private javax.swing.JTabbedPane eventTabsPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu menuAutoData;
    private javax.swing.JMenu menuEvaluations;
    private javax.swing.JMenu menuInstructor;
    private javax.swing.JButton saveCloseBtn;
    private javax.swing.JComboBox<String> timeHourComboSelector;
    private javax.swing.JComboBox<String> timeMinuteComboSelector;
    private javax.swing.JComboBox<String> timeSecondComboSelector;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object arg)
    {

        final PropertyUpdate update = (PropertyUpdate) arg;
        final ImageIcon image = update.getPanel().getProperty().getImage();
        final String caption = update.getPanel().title();

        final JLabel title = new JLabel(caption, image, JLabel.CENTER);

        /*
         final BorderLayout layout = new BorderLayout();
         final JPanel title = new JPanel(layout);
         title.setOpaque(false);
         title.add(new JLabel(image), BorderLayout.WEST);
         title.add(new JLabel(caption), BorderLayout.EAST);
         */
        if (update.getType() == UpdateType.Add)
        {
            eventTabsPane.insertTab(null, null,
                    update.getPanel().getPanel(), null, update.getPosition());
            eventTabsPane.setTabComponentAt(update.getPosition(), title);
            eventTabsPane.setSelectedIndex(update.getPosition());
            topFrame.repaint();
        }
        else if (update.getType() == UpdateType.Remove)
        {
            eventTabsPane.remove(update.getPanel().getPanel());
            topFrame.repaint();
        }
        else if (update.getType() == UpdateType.TitleChange)
        {
            final int index = eventTabsPane.indexOfComponent(
                    update.getPanel().getPanel());

            new Thread(new Runnable()
            {
                public void run()
                {
                    Color c = eventTabsPane.getForegroundAt(index);
                    eventTabsPane.setForegroundAt(index, Color.blue);
                    try
                    {
                        Thread.sleep(350);
                    }
                    catch (Exception e)
                    {
                    }
                    if (index < eventTabsPane.getTabCount())
                    {
                        eventTabsPane.setTabComponentAt(index, title);
                    }
                    try
                    {
                        Thread.sleep(350);
                    }
                    catch (Exception e)
                    {
                    }
                    if (index < eventTabsPane.getTabCount())
                    {
                        eventTabsPane.setForegroundAt(index, c);
                    }
                }
            }).start();
        }
        else
        {
            throw new RuntimeException("UpdateType not accounted for");
        }
    }

}
