package csa.tiletools.map;


import java.util.*;

public class GridSettingsDataPanel extends javax.swing.JPanel {
    private GridSettingsData mGridSettingsData = new GridSettingsData();
    private GridSettingsDataPool mGridSettingsDataPool;
    private int mClassSetting=0;

    /** Creates new form GridSettingsDataPanel */
    public GridSettingsDataPanel() {
        initComponents();
        mGridSettingsDataPool = new GridSettingsDataPool();
        resetGridConfigPool(false, "");
    }

    private void resetGridConfigPool(boolean select, String klasseToSet) /* allneeded*/
    {
        mClassSetting++;
        Collection<String> collectionKlasse = mGridSettingsDataPool.getKlassenHashMap().values();
        Iterator<String> iterKlasse = collectionKlasse.iterator();
        int i = 0;
        String klasse = "";

        jComboBoxGridKlasse.removeAllItems();
        while (iterKlasse.hasNext())
        {
            String item = iterKlasse.next();
            jComboBoxGridKlasse.addItem(item);
            if (select)
            {
                if (klasseToSet.length()==0)
                {
                    if (i==0)
                    {
                        jComboBoxGridKlasse.setSelectedIndex(i);
                        jTextFieldGridKlasse.setText(item);
                        klasse = item;
                    }
                }
                else
                {
                    if (klasseToSet.equalsIgnoreCase(item))
                    {
                        jComboBoxGridKlasse.setSelectedIndex(i);
                        jTextFieldGridKlasse.setText(item);
                        klasse = item;
                    }
                }
            }
            i++;
        }
        if ((select) && (klasse.length()==0))
        {
            if (jComboBoxGridKlasse.getItemCount()>0)
            {
                jComboBoxGridKlasse.setSelectedIndex(0);
                jTextFieldGridKlasse.setText(jComboBoxGridKlasse.getSelectedItem().toString());
                klasse = jComboBoxGridKlasse.getSelectedItem().toString();
            }
        }
        if (!select)  jComboBoxGridKlasse.setSelectedIndex(-1);

        Collection<GridSettingsData> colC = mGridSettingsDataPool.getMapForKlasse(klasse).values();
        Iterator<GridSettingsData> iterC = colC.iterator();

        jComboBoxGridName.removeAllItems();
        i = 0;
        while (iterC.hasNext())
        {
            GridSettingsData item = iterC.next();
            jComboBoxGridName.addItem(item.mName);
            if ((i==0) && (select))
            {
                jComboBoxGridName.setSelectedIndex(0);
                mGridSettingsData = mGridSettingsDataPool.get(item.mName);
                setGridAllFromCurrent();
            }
            i++;
        }
        if (!select)  jComboBoxGridName.setSelectedIndex(-1);
        mClassSetting--;
    }

    private void clearGridAll() /* allneeded*/
    {
        mClassSetting++;
        mGridSettingsData = new GridSettingsData();
        setGridAllFromCurrent();
        mClassSetting--;
    }

    private void setGridAllFromCurrent() /* allneeded*/
    {
        mClassSetting++;
        jComboBoxGridKlasse.setSelectedItem(mGridSettingsData.mClass);
        jTextFieldGridKlasse.setText(mGridSettingsData.mClass);
        jComboBoxGridName.setSelectedItem(mGridSettingsData.mName);
        jTextFieldGridName.setText(mGridSettingsData.mName);

        /* TO DO: Fill in needed Fields!
         *
         */
        mClassSetting--;
    }

    private void readGridAllToCurrent() /* allneeded*/
    {
        mGridSettingsData.mClass = jTextFieldGridKlasse.getText();
        mGridSettingsData.mName = jTextFieldGridName.getText();

        /* TO DO: Fill in needed Fields!
         *
         */
    }
    /** 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();
        jComboBoxGridKlasse = new javax.swing.JComboBox();
        jComboBoxGridName = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldGridName = new javax.swing.JTextField();
        jTextFieldGridKlasse = new javax.swing.JTextField();
        jButtonGridNew = new javax.swing.JButton();
        jButtonGridSave = new javax.swing.JButton();
        jButtonSaveAsNew = new javax.swing.JButton();
        jButtonGridDelete = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jComboBoxGridKlasse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxGridKlasseActionPerformed(evt);
            }
        });

        jComboBoxGridName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxGridNameActionPerformed(evt);
            }
        });

        jLabel3.setText("Name");

        jLabel4.setText("Class");

        jButtonGridNew.setText("New");
        jButtonGridNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGridNewActionPerformed(evt);
            }
        });

        jButtonGridSave.setText("Save");
        jButtonGridSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGridSaveActionPerformed(evt);
            }
        });

        jButtonSaveAsNew.setText("Save as new");
        jButtonSaveAsNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveAsNewActionPerformed(evt);
            }
        });

        jButtonGridDelete.setText("Delete");
        jButtonGridDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGridDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldGridName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                    .addComponent(jTextFieldGridKlasse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxGridKlasse, 0, 146, Short.MAX_VALUE)
                    .addComponent(jComboBoxGridName, 0, 146, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonGridSave)
                    .addComponent(jButtonGridNew))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSaveAsNew)
                    .addComponent(jButtonGridDelete))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonGridDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonGridSave)
                            .addComponent(jButtonSaveAsNew)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jComboBoxGridKlasse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButtonGridNew))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBoxGridName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(jTextFieldGridKlasse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(jTextFieldGridName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 531, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 215, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGridNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGridNewActionPerformed
        mClassSetting++;
        mGridSettingsData = new GridSettingsData();
        clearGridAll();
        resetGridConfigPool(false, "");
        mClassSetting--;
}//GEN-LAST:event_jButtonGridNewActionPerformed

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

        readGridAllToCurrent();
        mGridSettingsDataPool.put(mGridSettingsData);
        mGridSettingsDataPool.save();
        mClassSetting++;
        String klasse = jTextFieldGridKlasse.getText();
        resetGridConfigPool(true, klasse);
        jComboBoxGridName.setSelectedItem(mGridSettingsData.mName);
        mClassSetting--;
    }//GEN-LAST:event_jButtonGridSaveActionPerformed

    private void jButtonSaveAsNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveAsNewActionPerformed
        mGridSettingsData = new GridSettingsData();
        readGridAllToCurrent();
        mGridSettingsDataPool.putAsNew(mGridSettingsData);
        mGridSettingsDataPool.save();
        mClassSetting++;
        String klasse = jTextFieldGridKlasse.getText();
        resetGridConfigPool(true,klasse);
        jComboBoxGridName.setSelectedItem(mGridSettingsData.mName);
        mClassSetting--;
    }//GEN-LAST:event_jButtonSaveAsNewActionPerformed

    private void jButtonGridDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGridDeleteActionPerformed
        readGridAllToCurrent();
        mGridSettingsDataPool.remove(mGridSettingsData);
        mGridSettingsDataPool.save();
        mClassSetting++;
        String klasse = jTextFieldGridKlasse.getText();
        resetGridConfigPool(true,klasse);

        if (jComboBoxGridName.getSelectedIndex() == -1)
        {
            clearGridAll();
        }

        String key = jComboBoxGridName.getSelectedItem().toString();
        mGridSettingsData = mGridSettingsDataPool.get(key);
        setGridAllFromCurrent();

        mClassSetting--;
}//GEN-LAST:event_jButtonGridDeleteActionPerformed

    private void jComboBoxGridKlasseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxGridKlasseActionPerformed
        if (mClassSetting >0 ) return;
        mClassSetting++;;

        String selected = jComboBoxGridKlasse.getSelectedItem().toString();
        clearGridAll();
        resetGridConfigPool(true, selected);
        jTextFieldGridKlasse.setText(jComboBoxGridKlasse.getSelectedItem().toString());
        String key = jComboBoxGridName.getSelectedItem().toString();
        mGridSettingsData = mGridSettingsDataPool.get(key);
        setGridAllFromCurrent();
        mClassSetting--;
    }//GEN-LAST:event_jComboBoxGridKlasseActionPerformed

    private void jComboBoxGridNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxGridNameActionPerformed
        if (mClassSetting > 0 ) return;
        String key = jComboBoxGridName.getSelectedItem().toString();
        mGridSettingsData = mGridSettingsDataPool.get(key);
        setGridAllFromCurrent();
    }//GEN-LAST:event_jComboBoxGridNameActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGridDelete;
    private javax.swing.JButton jButtonGridNew;
    private javax.swing.JButton jButtonGridSave;
    private javax.swing.JButton jButtonSaveAsNew;
    private javax.swing.JComboBox jComboBoxGridKlasse;
    private javax.swing.JComboBox jComboBoxGridName;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextFieldGridKlasse;
    private javax.swing.JTextField jTextFieldGridName;
    // End of variables declaration//GEN-END:variables

}
