/**
 * 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.Wiimote;
import wiiusej.utils.AccelerationExpansionEventPanel;
import wiiusej.utils.AccelerationPanel;
import wiiusej.utils.GForceExpansionEventPanel;
import wiiusej.utils.GForcePanel;
import wiiusej.utils.NunchukJoystickEventPanel;
import wiiusej.utils.OrientationExpansionEventPanel;
import wiiusej.utils.OrientationPanel;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.NunchukButtonsEvent;
import wiiusej.wiiusejevents.physicalevents.NunchukEvent;
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 nunchuk.
 * 
 * @author guiguito
 */
public class NunchukGuiTest extends javax.swing.JFrame implements
		WiimoteListener {

	private Wiimote wiimote;
	private boolean isThresholdsRequested = true;

	/** Creates new form NunchukGuiTest */
	public NunchukGuiTest(Wiimote wiimote) {
		initComponents();
		this.wiimote = wiimote;
		registerListeners();
	}

	private void registerListeners() {
		wiimote.addWiiMoteEventListeners(this);
		wiimote.addWiiMoteEventListeners((OrientationPanel) orientationPanel);
		wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel);
		wiimote
				.addWiiMoteEventListeners((AccelerationPanel) rawAccelerationPanel);
		wiimote
				.addWiiMoteEventListeners((NunchukJoystickEventPanel) joystickEventsPanel);
	}

	public void unRegisterListeners() {
		wiimote
				.removeWiiMoteEventListeners((OrientationPanel) orientationPanel);
		wiimote.removeWiiMoteEventListeners((GForcePanel) gForcePanel);
		wiimote
				.removeWiiMoteEventListeners((AccelerationPanel) rawAccelerationPanel);
		wiimote
				.removeWiiMoteEventListeners((NunchukJoystickEventPanel) joystickEventsPanel);
		wiimote.removeWiiMoteEventListeners(this);
	}

	public void requestThresholdsUpdate() {
		isThresholdsRequested = true;
	}

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

		topPanels = new javax.swing.JPanel();
		joystickEventsPanel = new NunchukJoystickEventPanel();
		motionSensingEventsPanel = new javax.swing.JPanel();
		motionSensingEventsTabbedPanels = new javax.swing.JTabbedPane();
		rawAccelerationPanel = new AccelerationExpansionEventPanel();
		orientationPanel = new OrientationExpansionEventPanel();
		gForcePanel = new GForceExpansionEventPanel();
		setNunchukValuesPanel = new javax.swing.JPanel();
		nunchukButtonsEventPanel = new javax.swing.JPanel();
		cButton = new javax.swing.JButton();
		zButton = new javax.swing.JButton();
		nunchukOrientationPanel = new javax.swing.JPanel();
		nunchukOrientationTextField = new javax.swing.JTextField();
		nunchukOrientationButton = new javax.swing.JButton();
		nunchukAccelerationPanel = new javax.swing.JPanel();
		nunchukAccelerationTextField = new javax.swing.JTextField();
		nunchukAccelerationButton = new javax.swing.JButton();
		messagePanel = new javax.swing.JPanel();
		messageText = new javax.swing.JLabel();

		setTitle("WiiuseJ Nunchuk Test GUI");
		setMinimumSize(new java.awt.Dimension(400, 400));
		getContentPane().setLayout(
				new javax.swing.BoxLayout(getContentPane(),
						javax.swing.BoxLayout.Y_AXIS));

		topPanels.setMinimumSize(new java.awt.Dimension(400, 200));
		topPanels.setPreferredSize(new java.awt.Dimension(400, 200));
		topPanels.setLayout(new javax.swing.BoxLayout(topPanels,
				javax.swing.BoxLayout.LINE_AXIS));

		joystickEventsPanel.setBackground(new java.awt.Color(0, 0, 0));
		joystickEventsPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder(new javax.swing.border.LineBorder(
						new java.awt.Color(51, 153, 0), 2, true),
						"Joystick View",
						javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
						javax.swing.border.TitledBorder.DEFAULT_POSITION,
						new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(
								204, 102, 0)));
		joystickEventsPanel.setToolTipText("JoystickEvent");
		joystickEventsPanel.setMinimumSize(new java.awt.Dimension(200, 200));

		javax.swing.GroupLayout joystickEventsPanelLayout = new javax.swing.GroupLayout(
				joystickEventsPanel);
		joystickEventsPanel.setLayout(joystickEventsPanelLayout);
		joystickEventsPanelLayout.setHorizontalGroup(joystickEventsPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 601, Short.MAX_VALUE));
		joystickEventsPanelLayout.setVerticalGroup(joystickEventsPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 174, Short.MAX_VALUE));

		topPanels.add(joystickEventsPanel);
		joystickEventsPanel.getAccessibleContext()
				.setAccessibleName("Joystick");

		motionSensingEventsPanel
				.setMinimumSize(new java.awt.Dimension(200, 200));

		rawAccelerationPanel.setToolTipText("Nunchuk MotionSensingEvent");

		javax.swing.GroupLayout rawAccelerationPanelLayout = new javax.swing.GroupLayout(
				rawAccelerationPanel);
		rawAccelerationPanel.setLayout(rawAccelerationPanelLayout);
		rawAccelerationPanelLayout
				.setHorizontalGroup(rawAccelerationPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGap(0, 597, Short.MAX_VALUE));
		rawAccelerationPanelLayout.setVerticalGroup(rawAccelerationPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 175, Short.MAX_VALUE));

		motionSensingEventsTabbedPanels.addTab("RawAcceleration",
				rawAccelerationPanel);

		javax.swing.GroupLayout orientationPanelLayout = new javax.swing.GroupLayout(
				orientationPanel);
		orientationPanel.setLayout(orientationPanelLayout);
		orientationPanelLayout.setHorizontalGroup(orientationPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 597, Short.MAX_VALUE));
		orientationPanelLayout.setVerticalGroup(orientationPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 175, Short.MAX_VALUE));

		motionSensingEventsTabbedPanels.addTab("Orientation", orientationPanel);

		javax.swing.GroupLayout gForcePanelLayout = new javax.swing.GroupLayout(
				gForcePanel);
		gForcePanel.setLayout(gForcePanelLayout);
		gForcePanelLayout.setHorizontalGroup(gForcePanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 597, Short.MAX_VALUE));
		gForcePanelLayout.setVerticalGroup(gForcePanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 175, Short.MAX_VALUE));

		motionSensingEventsTabbedPanels.addTab("GForce", gForcePanel);

		javax.swing.GroupLayout motionSensingEventsPanelLayout = new javax.swing.GroupLayout(
				motionSensingEventsPanel);
		motionSensingEventsPanel.setLayout(motionSensingEventsPanelLayout);
		motionSensingEventsPanelLayout
				.setHorizontalGroup(motionSensingEventsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(motionSensingEventsTabbedPanels,
								javax.swing.GroupLayout.DEFAULT_SIZE, 602,
								Short.MAX_VALUE));
		motionSensingEventsPanelLayout
				.setVerticalGroup(motionSensingEventsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(motionSensingEventsTabbedPanels,
								javax.swing.GroupLayout.DEFAULT_SIZE, 200,
								Short.MAX_VALUE));

		topPanels.add(motionSensingEventsPanel);

		getContentPane().add(topPanels);

		setNunchukValuesPanel.setMinimumSize(new java.awt.Dimension(400, 200));
		setNunchukValuesPanel
				.setPreferredSize(new java.awt.Dimension(400, 200));
		setNunchukValuesPanel.setLayout(new javax.swing.BoxLayout(
				setNunchukValuesPanel, javax.swing.BoxLayout.Y_AXIS));

		nunchukButtonsEventPanel.setToolTipText("Nunchuk ButtonsEvent");
		nunchukButtonsEventPanel
				.setMinimumSize(new java.awt.Dimension(100, 100));
		nunchukButtonsEventPanel.setPreferredSize(new java.awt.Dimension(100,
				100));
		nunchukButtonsEventPanel.setLayout(new javax.swing.BoxLayout(
				nunchukButtonsEventPanel, javax.swing.BoxLayout.LINE_AXIS));

		cButton.setText("C");
		cButton.setMaximumSize(new java.awt.Dimension(50, 50));
		cButton.setMinimumSize(new java.awt.Dimension(50, 50));
		cButton.setPreferredSize(new java.awt.Dimension(50, 50));
		nunchukButtonsEventPanel.add(cButton);

		zButton.setText("Z");
		zButton.setMaximumSize(new java.awt.Dimension(50, 50));
		zButton.setMinimumSize(new java.awt.Dimension(50, 50));
		zButton.setPreferredSize(new java.awt.Dimension(50, 50));
		nunchukButtonsEventPanel.add(zButton);

		setNunchukValuesPanel.add(nunchukButtonsEventPanel);

		nunchukOrientationTextField.setPreferredSize(new java.awt.Dimension(60,
				20));
		nunchukOrientationPanel.add(nunchukOrientationTextField);

		nunchukOrientationButton.setText("Set Orientation Threshold");
		nunchukOrientationButton
				.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mousePressed(java.awt.event.MouseEvent evt) {
						nunchukOrientationButtonMousePressed(evt);
					}
				});
		nunchukOrientationPanel.add(nunchukOrientationButton);

		setNunchukValuesPanel.add(nunchukOrientationPanel);

		nunchukAccelerationTextField.setPreferredSize(new java.awt.Dimension(
				60, 20));
		nunchukAccelerationPanel.add(nunchukAccelerationTextField);

		nunchukAccelerationButton.setText("Set Acceleration Threshold");
		nunchukAccelerationButton
				.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mousePressed(java.awt.event.MouseEvent evt) {
						nunchukAccelerationButtonMousePressed(evt);
					}
				});
		nunchukAccelerationPanel.add(nunchukAccelerationButton);

		setNunchukValuesPanel.add(nunchukAccelerationPanel);

		messageText.setText("Message:");

		javax.swing.GroupLayout messagePanelLayout = new javax.swing.GroupLayout(
				messagePanel);
		messagePanel.setLayout(messagePanelLayout);
		messagePanelLayout
				.setHorizontalGroup(messagePanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGap(0, 1216, Short.MAX_VALUE)
						.addGroup(
								messagePanelLayout
										.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
												messagePanelLayout
														.createSequentialGroup()
														.addGap(0, 0,
																Short.MAX_VALUE)
														.addComponent(
																messageText)
														.addGap(0, 0,
																Short.MAX_VALUE))));
		messagePanelLayout
				.setVerticalGroup(messagePanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGap(0, 34, Short.MAX_VALUE)
						.addGroup(
								messagePanelLayout
										.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
												messagePanelLayout
														.createSequentialGroup()
														.addGap(0, 0,
																Short.MAX_VALUE)
														.addComponent(
																messageText)
														.addGap(0, 0,
																Short.MAX_VALUE))));

		setNunchukValuesPanel.add(messagePanel);

		getContentPane().add(setNunchukValuesPanel);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void nunchukOrientationButtonMousePressed(
			java.awt.event.MouseEvent evt) {// GEN-FIRST:event_nunchukOrientationButtonMousePressed
		try {
			float nb = Float.parseFloat(nunchukOrientationTextField.getText());
			wiimote.setNunchukOrientationThreshold(nb);
			messageText.setText("Nunchuk orientation threshold set to " + nb);
		} catch (NumberFormatException e) {
			messageText
					.setText("Number is not an integer, nunchuk orientation threshold not set !");
		}
	}// GEN-LAST:event_nunchukOrientationButtonMousePressed

	private void nunchukAccelerationButtonMousePressed(
			java.awt.event.MouseEvent evt) {// GEN-FIRST:event_nunchukAccelerationButtonMousePressed
		try {
			int nb = Integer.parseInt(nunchukAccelerationTextField.getText());
			wiimote.setNunchukAccelerationThreshold(nb);
			messageText.setText("Nunchuk acceleration threshold set to " + nb);
		} catch (NumberFormatException e) {
			messageText
					.setText("Number is not an integer, nunchuk acceleration threshold not set !");
		}
	}// GEN-LAST:event_nunchukAccelerationButtonMousePressed

	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 NunchukEvent) {
			NunchukEvent nunchuk = (NunchukEvent) arg0;
			NunchukButtonsEvent buttons = nunchuk.getButtonsEvent();
                        
                        //C button
			if (buttons.isButtonCJustPressed()) {
				cButton.setEnabled(false);
			}else if (buttons.isButtonCJustReleased()) {
				cButton.setEnabled(true);
			}
                        
                        //Z button
                        if(buttons.isButtonZJustPressed()) {
				zButton.setEnabled(false);
			}else if (buttons.isButtonZJustReleased()) {
				zButton.setEnabled(true);
			}
                        
                        
			if (isThresholdsRequested) {
				MotionSensingEvent evt = nunchuk.getNunchukMotionSensingEvent();
				nunchukAccelerationTextField.setText(evt
						.getAccelerationThreshold()
						+ "");
				nunchukOrientationTextField.setText(evt
						.getOrientationThreshold()
						+ "");
				isThresholdsRequested = false;
			}
		}
	}

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

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

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

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

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

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

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

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

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cButton;
	private javax.swing.JPanel gForcePanel;
	private javax.swing.JPanel joystickEventsPanel;
	private javax.swing.JPanel messagePanel;
	private javax.swing.JLabel messageText;
	private javax.swing.JPanel motionSensingEventsPanel;
	private javax.swing.JTabbedPane motionSensingEventsTabbedPanels;
	private javax.swing.JButton nunchukAccelerationButton;
	private javax.swing.JPanel nunchukAccelerationPanel;
	private javax.swing.JTextField nunchukAccelerationTextField;
	private javax.swing.JPanel nunchukButtonsEventPanel;
	private javax.swing.JButton nunchukOrientationButton;
	private javax.swing.JPanel nunchukOrientationPanel;
	private javax.swing.JTextField nunchukOrientationTextField;
	private javax.swing.JPanel orientationPanel;
	private javax.swing.JPanel rawAccelerationPanel;
	private javax.swing.JPanel setNunchukValuesPanel;
	private javax.swing.JPanel topPanels;
	private javax.swing.JButton zButton;
	// End of variables declaration//GEN-END:variables
}
