/**
 * 
 * WiiUseJTest 0.1
 * 
 * Jorge Iván Meza Martínez <jimezam@gmail.com>
 * http://jorgeivanmeza.com/
 * 
 * Demostración del uso de la librería WiiRemoteJ (con BlueCove).
 * 
 * javac -classpath ../wiiusej.jar:. WiiUseJTest.java
 * java  -classpath ../wiiusej.jar:. -Djava.library.path=. WiiUseJTest
 * 
 */

import javax.swing.*;
import java.awt.event.*;

import wiiusej.*;
import wiiusej.wiiusejevents.*;
import wiiusej.wiiusejevents.utils.*;
import wiiusej.wiiusejevents.physicalevents.*;
import wiiusej.wiiusejevents.wiiuseapievents.*;

public class WiiUseJTest extends JFrame 
                         implements ActionListener, WiimoteListener
{
	private JTextArea text    = null;
	private Wiimote   wiimote = null;
	
	public WiiUseJTest()
	{
		this.setSize(320, 320);
		this.setResizable(false);
		this.setVisible(true);
		this.setTitle("WiiRemoteJTest");
		
		this.setLayout(null);
		JButton b = new JButton("Buscar Wiimote");
		b.setActionCommand("search");
		b.setBounds(60, 30, 200, 30);
		b.addActionListener(this);
		this.add(b);
		
		text = new JTextArea();
		JScrollPane scroll = new JScrollPane(text);
		scroll.setBounds(10, 100, 290, 180);
		this.add(scroll);
		
		System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String sel = e.getActionCommand();
		
		if(sel == "search")
		{
			onSearchWiimote();
		}
	}
	
	protected void onSearchWiimote()
	{
		this.log("Searching for a wiimote ... press buttons 1 and 2 ...");
		
		try 
		{
			// @param	Int		- The count of Wiimotes to searach.
			// @param	Boolean	- True rumble on connect.
			
			Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
			
			wiimote = wiimotes[0];
			// wiimote.activateIRTRacking();
			// wiimote.activateMotionSensing();
			wiimote.addWiiMoteEventListeners(this);
					
			this.log("I found to " + wiimote.getId());
		}
		catch(Exception e) 
		{
			this.log(e.getMessage());
			this.log("Failed to connect remote. Trying again.");
		}
	}
	
	public void log(String message)
	{
		String data = "> " + message + "\n";
		
		text.setText(data + text.getText());
		System.out.println(data);
	}
	
	public static void main(String[] args)
	{
		WiiUseJTest wTest = new WiiUseJTest();
	}
	
	////////////////////////////////////////////////////////////////////

	public void onButtonsEvent(WiimoteButtonsEvent arg0) 
	{
		if (arg0.isButtonAPressed())
			this.log("A");
		
		if (arg0.isButtonBPressed())
			this.log("B");
		
		if (arg0.isButtonDownPressed())
			this.log("Down");
		
		if (arg0.isButtonHomePressed())
			this.log("Home");
		
		if (arg0.isButtonLeftPressed())
			this.log("Left");
		
		if (arg0.isButtonMinusPressed())
			this.log("Minus");
		
		if (arg0.isButtonOnePressed())
			this.log("1");
		
		if (arg0.isButtonPlusPressed())
			this.log("Plus");
		
		if (arg0.isButtonRightPressed())
			this.log("Right");
		
		if (arg0.isButtonTwoPressed())
			this.log("2");
		
		if (arg0.isButtonUpPressed())
			this.log("Up");
     }
	
	public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent e) 
	{
		this.log("Classic Controller Extension Inserted on Wiimote #" + e.getWiimoteId());
	}
	
	public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent e)
	{
		this.log("Classic Controller Extension Removed on Wiimote #" + e.getWiimoteId());
	}

    public void onIrEvent(IREvent arg0) 
	{
		this.log("IR Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onMotionSensingEvent(MotionSensingEvent arg0) 
	{
		this.log("Motion Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onExpansionEvent(ExpansionEvent arg0) 
	{
		this.log("Expansion Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onStatusEvent(StatusEvent arg0) 
	{
		this.log("Status Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onDisconnectionEvent(DisconnectionEvent arg0) 
	{
		this.log("Disconnection Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) 
	{
        this.log("Nunchuk Inserted Event on Wiimote #" + arg0.getWiimoteId());
    }

    public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) 
	{
		this.log("Nunchuk Removed Event on Wiimote #" + arg0.getWiimoteId());
    }
	
	public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent e) 
	{
		this.log("Guitar Hero Inserted Event on Wiimote #" + e.getWiimoteId());
	}
	
	public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent e) 
	{
		this.log("Guitar Hero Removed Event on Wiimote #" + e.getWiimoteId());
	}
	
	////////////////////////////////////////////////////////////////////
}

