package moppydesk.ui;

import moppydesk.Constants;
import moppydesk.MoppyUI;
import moppydesk.midputs.NoteFilter;

/**
 *
 * @author Sammy1Am
 */
public class FilterControls extends javax.swing.JPanel {

    private NoteFilter filter;
    private MoppyUI app;

    /**
     * This constructor is only for use in the NetBeans editor window.  The constructor
     * below that takes a {@link MoppyUI} as an argument should always be used.
     */
    public FilterControls() {
        initComponents();
    }

    public FilterControls(MoppyUI app) {
        this.app = app;

        initComponents();
        filter = new NoteFilter();

        loadSettings();
        updateFilter();
    }

    public NoteFilter getNoteFilter() {
        return filter;
    }

    public void connected() {
        saveSettings();
        setControlsEnabled(false);
    }

    public void disconnected() {
        filter.close();
        setControlsEnabled(true);
    }

    private void loadSettings() {
        constrainCB.setSelected(app.prefs.getBoolean(Constants.PREF_FILTER_CONSTRAIN, false));
        ignoreTenCB.setSelected(app.prefs.getBoolean(Constants.PREF_FILTER_IGNORETEN, false));

        updateFilter();
    }

    private void saveSettings() {
        app.prefs.putBoolean(Constants.PREF_FILTER_CONSTRAIN, constrainCB.isSelected());
        app.prefs.putBoolean(Constants.PREF_FILTER_IGNORETEN, ignoreTenCB.isSelected());

        app.savePreferences();
    }

    private void updateFilter() {
        filter.setAutoConstrain(constrainCB.isSelected());
        filter.setIgnoreTen(ignoreTenCB.isSelected());
    }

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

        constrainCB = new javax.swing.JCheckBox();
        ignoreTenCB = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setPreferredSize(new java.awt.Dimension(149, 149));

        constrainCB.setText("Constrain Notes");
        constrainCB.setToolTipText("Automatically transposes notes outside of the floppy drive range into playable range.");
        constrainCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                constrainCBActionPerformed(evt);
            }
        });

        ignoreTenCB.setText("Ignore Channel 10");
        ignoreTenCB.setToolTipText("MIDI channel 10 is generally reserved for percussion instruments and is unlikely to render correctly on floppy drives.");
        ignoreTenCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ignoreTenCBActionPerformed(evt);
            }
        });

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

    private void constrainCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_constrainCBActionPerformed
        updateFilter();
    }//GEN-LAST:event_constrainCBActionPerformed

    private void ignoreTenCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ignoreTenCBActionPerformed
        updateFilter();
    }//GEN-LAST:event_ignoreTenCBActionPerformed

    
    private void setControlsEnabled(boolean enabled){
        constrainCB.setEnabled(enabled);
        ignoreTenCB.setEnabled(enabled);
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox constrainCB;
    private javax.swing.JCheckBox ignoreTenCB;
    // End of variables declaration//GEN-END:variables
}
