package event.editor;

import java.awt.event.*;
import java.util.Observable;
import java.util.List;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ComboBoxModel;
import scriptbuilder.structures.events.I_ScriptEvent;
import scriptbuilder.structures.units.Unit;
import scriptbuilder.structures.events.UnitEvent;

/**
 *
 * @author nathaniellehrer
 * 
 * 
 */
public class UnitPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel
{

    private ActionListener removeListener;
    private UnitEvent event;
    private List<Unit> units;
    
    List<String> names = new ArrayList<String>();
    /**
     * Creates new form UnitPanel
     */
    public UnitPanel()
    {
        initComponents();
        
        //units = event.slice.getIncident().script.units;
        
    }

    /**
     * Loads the event information into the event editor window.
     * @param sei  Scriptevent that is being loaded in
     */
    @Override
    public void getEventObject(I_ScriptEvent sei)
    {
        
        event = (UnitEvent) sei;
        //loads the current unit list into the combobox on re-opening of unit event
        loadSelector();
        for (int i = 0; i < ActiveDropdown.getItemCount(); i++)
        {
            if (event.unitActive.equalsIgnoreCase((String) ActiveDropdown.getItemAt(i)))
            {
                ActiveDropdown.setSelectedIndex(i);
            }
        }
        
        //for each name in the names list
        for (int i = 0; i < names.size(); i++)
        {
            //which event equals which index in the names list?
            if (event.unitNum.equalsIgnoreCase((String) names.get(i)))
            {
                
                //set the unit selector to the data model value
                unitSelector.setSelectedIndex(i);
                
            }
        }

        for (int i = 0; i < PrimaryDropdown.getItemCount(); i++)
        {
            if (event.unitPrimary.equalsIgnoreCase((String) PrimaryDropdown.getItemAt(i)))
            {
                PrimaryDropdown.setSelectedIndex(i);
            }
        }

        boolean containsItem = false;
        for (int i = 0; i < StatusDropdown.getItemCount() && !containsItem; i++)
        {
            if (event.unitStatus.equalsIgnoreCase((String) StatusDropdown.getItemAt(i)))
            {
                StatusDropdown.setSelectedIndex(i);
                containsItem = true;
            }
        }
        if (!containsItem)
        {
            StatusDropdown.addItem(event.unitStatus);
            StatusDropdown.setSelectedItem(StatusDropdown.getItemCount() - 1);
        }

    }

    @Override
    public void update(Observable o, Object arg)
    {
        //todo: make supported
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean removeAssociatedEvent()
    {
        event.removeThis();
        event = null;
        return true;
    }
    
    
    private void loadSelector(){
        //checks if the event is exist
        if(event != null)
        {
            //loads the names list from the top data model
            loadNames();
            //asks if the unitselector already has some units added into it
            
            if(unitSelector.getModel().equals(new DefaultComboBoxModel(names.toArray())) || (unitSelector.getModel().getSize() == 0))
            {
                //if combobox empty, load combobox
                unitSelector.setModel(new DefaultComboBoxModel(names.toArray()));
            }
            
            
            
        }
        
    }
    
    /**
     * Loads the unit names from the unit list in SimulationScript
     */
    private void loadNames(){
        //checks if the event is exist
        if(event!=null)
        {
            //this sucks! try to change it sometime
            //grabs the unit list from the top of the SimulationScript and 
            //appends them to the names list
            units = event.slice.getIncident().script.units;
            
            for(Unit u : units)
            {
                names.add(u.UnitNum);
            }
        }
    }

    @Override
    /**
     * when window is closed, these values are set.
     */
    public void uponClose()
    {
        
        if (event != null && unitSelector.getItemCount() != 0)
        {
            event.unitNum = unitSelector.getSelectedItem().toString();
            event.unitActive = ActiveDropdown.getSelectedItem().toString();
            event.unitPrimary = PrimaryDropdown.getSelectedItem().toString();
            event.unitStatus = StatusDropdown.getSelectedItem().toString();
                
            //old logic for creating dummy unit
            /*if(event.slice.getIncident().script.hasUnit(txtUnitNumber.getText())){
                //checks to see if there is already a unit selected there
                //event.unitNum = (txtUnitNumber.getText());
                
            }else{
                //Creates a dummy unit here
                event.slice.getIncident().script.addDummyUnit(txtUnitNumber.getText());
                //event.unitNum = (txtUnitNumber.getText());
                event.unitActive = ActiveDropdown.getSelectedItem().toString();
                event.unitPrimary = PrimaryDropdown.getSelectedItem().toString();
                event.unitStatus = StatusDropdown.getSelectedItem().toString();
                //System.out.println("invalid unit 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() {

        jLabel2 = new javax.swing.JLabel();
        StatusDropdown = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        PrimaryDropdown = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        ActiveDropdown = new javax.swing.JComboBox();
        help = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        unitSelector = new javax.swing.JComboBox<>();

        jLabel2.setText("Status");

        StatusDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1098", "1097", "ENRT" }));

        jLabel3.setText("Primary");

        PrimaryDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" }));

        jLabel4.setText("Active");

        ActiveDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "True", "False" }));

        jLabel5.setText("Unit");

        unitSelector.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                unitSelectorAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        unitSelector.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                unitSelectorMouseEntered(evt);
            }
        });
        unitSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unitSelectorActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, help, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel4))
                        .add(43, 43, 43)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(ActiveDropdown, 0, 475, Short.MAX_VALUE)
                            .add(PrimaryDropdown, 0, 475, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel5))
                        .add(54, 54, 54)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(StatusDropdown, 0, 419, Short.MAX_VALUE)
                                .add(57, 57, 57))
                            .add(layout.createSequentialGroup()
                                .add(unitSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(82, 82, 82)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(unitSelector, 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(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(StatusDropdown, 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(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(PrimaryDropdown, 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(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(ActiveDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 100, Short.MAX_VALUE)
                .add(help, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void unitSelectorAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_unitSelectorAncestorAdded
        //loads the unitSelector on the initialization of the window
        loadSelector();
    }//GEN-LAST:event_unitSelectorAncestorAdded

    private void unitSelectorMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_unitSelectorMouseEntered
        
    }//GEN-LAST:event_unitSelectorMouseEntered

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


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ActiveDropdown;
    private javax.swing.JComboBox PrimaryDropdown;
    private javax.swing.JComboBox StatusDropdown;
    private javax.swing.JLabel help;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox<String> unitSelector;
    // End of variables declaration//GEN-END:variables

}
