/**
 * This file is part of WiiuseJ.
 *
 *  WiiuseJ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  WiiuseJ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with WiiuseJ.  If not, see <http://www.gnu.org/licenses/>.
 */
package wiiusej.test;

import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.utils.ClassicControllerButtonsEventPanel;
import wiiusej.wiiusejevents.physicalevents.ClassicControllerEvent;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

/**
 * This frame is used to display events from a classic controller.
 * 
 * @author guiguito
 */
public class ClassicControllerGuiTest extends javax.swing.JFrame implements WiimoteListener {

    private Wiimote wiimote;
    private static int MAX_SHOULDER = 100;

    /** Creates new form ClassicControllerGuiTest */
    public ClassicControllerGuiTest(Wiimote wiimote) {
        initComponents();
        this.wiimote = wiimote;
        registerListeners();
        leftShoulderBar.setMaximum(MAX_SHOULDER);
        rightShoulderBar.setMaximum(MAX_SHOULDER);
    }

    private void registerListeners() {
        wiimote.addWiiMoteEventListeners(this);
        wiimote.addWiiMoteEventListeners((ClassicControllerButtonsEventPanel) classicControllerPanel);
    }

    public void unRegisterListeners() {
        wiimote.removeWiiMoteEventListeners(this);
        wiimote.removeWiiMoteEventListeners((ClassicControllerButtonsEventPanel) classicControllerPanel);
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shouldersPanel = new javax.swing.JPanel();
        leftShoulderBar = new javax.swing.JProgressBar();
        rightShoulderBar = new javax.swing.JProgressBar();
        classicControllerPanel = new wiiusej.utils.ClassicControllerButtonsEventPanel();

        setTitle("WiiuseJ Classic Controller Test GUI");
        setResizable(false);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        shouldersPanel.setMaximumSize(new java.awt.Dimension(350, 16));
        shouldersPanel.setMinimumSize(new java.awt.Dimension(350, 16));
        shouldersPanel.setPreferredSize(new java.awt.Dimension(350, 16));
        shouldersPanel.setLayout(new javax.swing.BoxLayout(shouldersPanel, javax.swing.BoxLayout.LINE_AXIS));
        shouldersPanel.add(leftShoulderBar);
        shouldersPanel.add(rightShoulderBar);

        getContentPane().add(shouldersPanel);

        classicControllerPanel.setMaximumSize(new java.awt.Dimension(350, 182));
        classicControllerPanel.setMinimumSize(new java.awt.Dimension(350, 182));

        javax.swing.GroupLayout classicControllerPanelLayout = new javax.swing.GroupLayout(classicControllerPanel);
        classicControllerPanel.setLayout(classicControllerPanelLayout);
        classicControllerPanelLayout.setHorizontalGroup(
            classicControllerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
        );
        classicControllerPanelLayout.setVerticalGroup(
            classicControllerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 182, Short.MAX_VALUE)
        );

        getContentPane().add(classicControllerPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void onButtonsEvent(WiimoteButtonsEvent arg0) {
    // nothing to do
    }

    public void onIrEvent(IREvent arg0) {
    // nothing to do
    }

    public void onMotionSensingEvent(MotionSensingEvent arg0) {
    // nothing to do
    }

    public void onExpansionEvent(ExpansionEvent arg0) {
        if (arg0 instanceof ClassicControllerEvent) {
            ClassicControllerEvent classicController = (ClassicControllerEvent) arg0;
            float leftShoulder = classicController.getLeftShoulder();
            float rightShoulder = classicController.getRightShoulder();
            leftShoulderBar.setValue(Math.round(leftShoulder * MAX_SHOULDER));
            rightShoulderBar.setValue(Math.round(rightShoulder * MAX_SHOULDER));
        }
    }

    public void onStatusEvent(StatusEvent arg0) {
    // nothing to do
    }

    public void onDisconnectionEvent(DisconnectionEvent arg0) {
    // nothing to do
    }

    public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
    // nothing to do
    }

    public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
    // nothing to do
    }

    public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {
    // nothing to do
    }

    public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {
    // nothing to do
    }

    public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent arg0) {
    // nothing to do
    }

    public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent arg0) {
    // nothing to do
    }

    public static void main(String[] args) {
        Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
        ClassicControllerGuiTest gui = null;
        if (wiimotes.length > 0) {
            gui = new ClassicControllerGuiTest(wiimotes[0]);
        }
        gui.setDefaultCloseOperation(WiiuseJGuiTest.EXIT_ON_CLOSE);
        gui.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel classicControllerPanel;
    private javax.swing.JProgressBar leftShoulderBar;
    private javax.swing.JProgressBar rightShoulderBar;
    private javax.swing.JPanel shouldersPanel;
    // End of variables declaration//GEN-END:variables
}
