package event.editor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import scriptbuilder.gui.IncidentEditorFrame;
import scriptbuilder.structures.ScriptIncident;
import scriptbuilder.structures.events.*;

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

    IncidentEditorFrame topFrame = null;

    private PropertyModel model = new PropertyModel();

//    public PropertyModel getPropertyModel()
//    {
//        return model;
//    }
    public void addProperty(Properties property, I_ScriptEvent se)
    {
        model.addProperty(property, se);
    }

    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.addProperty(property, new CCTVEvent());
            }
            else
            {
                model.removeProperty(property);
            }
        }
    };

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

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

        model.addObserver(this);

        // For each menu
        for (int menuCtr = 0; menuCtr < jMenuBar1.getMenuCount(); menuCtr++)
        {
            // for each menu item
            for (java.awt.Component comp : jMenuBar1.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 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()
    {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        btnRemoveCurrentEvent = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = 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();
        JMenu2 = new javax.swing.JMenu();
        MaintenanceRadio = new javax.swing.JCheckBoxMenuItem();
        TMTRadio = new javax.swing.JCheckBoxMenuItem();
        Telephone = new javax.swing.JCheckBoxMenuItem();
        jMenu3 = 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();

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

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

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

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(701, Short.MAX_VALUE)
                .add(btnRemoveCurrentEvent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(btnRemoveCurrentEvent)
        );

        jMenu1.setText("Evaluations");

        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);
            }
        });
        jMenu1.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);
            }
        });
        jMenu1.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);
            }
        });
        jMenu1.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);
            }
        });
        jMenu1.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);
            }
        });
        jMenu1.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);
            }
        });
        jMenu1.add(Radio);

        jMenuBar1.add(jMenu1);

        JMenu2.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);
            }
        });
        JMenu2.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);
            }
        });
        JMenu2.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);
            }
        });
        JMenu2.add(Telephone);

        jMenuBar1.add(JMenu2);

        jMenu3.setText("Automated Data");

        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");
        jMenu3.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");
        jMenu3.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");
        jMenu3.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);
            }
        });
        jMenu3.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");
        jMenu3.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");
        jMenu3.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");
        jMenu3.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");
        jMenu3.add(Witness);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        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, jTabbedPane1)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        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 = jTabbedPane1.getSelectedIndex();

        if (index > 0 && jTabbedPane1.getTabComponentAt(index) != null)
        {
            JPanel removable = (JPanel) jTabbedPane1
                    .getSelectedComponent();
            this.model.properties.removeProperty(removable);
        }

    }//GEN-LAST:event_btnRemoveCurrentEventActionPerformed

    /**
     * @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))).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.JMenu JMenu2;
    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.JButton btnRemoveCurrentEvent;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // 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)
        {
            jTabbedPane1.insertTab(null, null,
                    update.getPanel().getPanel(), null, update.getPosition());
            jTabbedPane1.setTabComponentAt(update.getPosition(), title);
            jTabbedPane1.setSelectedIndex(update.getPosition());
            topFrame.repaint();
        }
        else if (update.getType() == UpdateType.Remove)
        {
            jTabbedPane1.remove(update.getPanel().getPanel());
            if (update.getPanel().getPanel() instanceof I_ScriptEventEditorPanel)
            {
                ((I_ScriptEventEditorPanel) update.getPanel().getPanel()).removeAssociatedEvent();
            }
            topFrame.repaint();
        }
        else if (update.getType() == UpdateType.TitleChange)
        {
            final int index = jTabbedPane1.indexOfComponent(
                    update.getPanel().getPanel());

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

}
