package aimmotionnotifier.ui;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import com.buglabs.bug.module.gps.pub.IPositionProvider;
import com.buglabs.bug.module.gps.pub.LatLon;
import com.buglabs.bug.module.lcd.pub.IModuleDisplay;
import com.buglabs.bug.module.motion.pub.IMotionObserver;
import com.buglabs.bug.module.motion.pub.IMotionSubject;
import com.heatonresearch.aol.toc2.JavaTOC2;
import com.heatonresearch.aol.toc2.Chatable;

/**
 * AIMMotionNotifier is the main application class. It displays a user interface
 * to log into AOL Instant Messenger and a field to input the destination of the
 * motion alert. When it detects motion via the IMotionObserver interface it
 * sends an IM to the given screen name with geo information.
 * 
 * Requires three services: IModuleDisplay - To display the authentication UI.
 * IMotionSubject - To listen for motion events. IPositionProvider - To retrieve
 * lattitude and longitude from gps.
 * 
 * Requires a valid AOL Instant Messenger account and password, as well as a valid signed-on
 * screen name to alert.
 * 
 * @author John Connolly - jeconnol@gmail.com
 * 
 */

public class AIMMotionNotifierApp implements IMotionObserver, Chatable {

	/**
	 * Handle to AWT java.awt.Frame returned by IModuleDisplay.
	 */
	private Frame frame;

	/**
	 * Handle to the Display service.
	 */
	private IModuleDisplay disp;
	/**
	 * Handle to the GPS service.
	 */
	private IPositionProvider position;

	/**
	 * Handle to the Motion service.
	 */
	private IMotionSubject motion;

	/**
	 * Text field used to retrieve user's login.
	 */
	private TextField loginField;

	/**
	 * Text field used to retrieve user's password.
	 */
	private TextField passwordField;

	/**
	 * Text field used for user's screen name TO which the IM notification
	 * should be sent.
	 */
	private TextField destField;

	/**
	 * String representation of user's screen name TO which the IM notification
	 * should be sent.
	 */
	private String destSN;

	/**
	 * Handle to the TOC object which does the gritty work of IMing.
	 */
	private JavaTOC2 toc;

	/**
	 * AIMMotionNotifierApp constructor. Requires valid IModuleDisplay,
	 * IMotionSubject, and IPositionProvider services.
	 * 
	 * @param disp
	 * @param motion
	 * @param position
	 */
	public AIMMotionNotifierApp(IModuleDisplay disp, IMotionSubject motion,
			IPositionProvider position) {
		this.disp = disp;
		this.position = position;
		this.motion = motion;
		toc = new JavaTOC2(this);
	}

	/**
	 * Requests an AWT Frame from IModuleDisplay service and populates it to
	 * request user authentication information.
	 * 
	 */
	public void createUI() {
		this.frame = disp.getFrame();
		frame.setLayout(new GridLayout(6, 1));

		Label lblLogin = new Label("ScreeName: ");
		loginField = new TextField();

		Label lblPass = new Label("Password: ");
		passwordField = new TextField();
		passwordField.setEchoChar('*');

		Label lblDest = new Label("TO Screen name: ");
		destField = new TextField();

		frame.add(lblLogin);
		frame.add(loginField);
		frame.add(lblPass);
		frame.add(passwordField);
		frame.add(lblDest);
		frame.add(destField);

		Button loginBtn = new Button("Log In");
		loginBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {

				try {
					toc.login(loginField.getText(), passwordField.getText());
					destSN = destField.getText();
					
					//no more need for the GUI
					frame.dispose();
					System.out.println("Logged in Successfully");
					//start the JavaTOC2 thread listening for IMs
					toc.start();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		frame.add(loginBtn);
	}

	// override for Chatable interface
	public void unknown(String str) {
		//Uncomment this if you want to see all of the buddy list
		//and preferences and all that AIM garbage printed to the console.
		//System.out.println(str);
	}

	// override for Chatable interface
	public void error(String str, String var) {
		System.out.println("Error:" + str + ":" + var);

	}

	// override for Chatable interface, what to do if we receive an IM
	public void im(String from, String message) {
		toc.send(from, 
				"This is a BugLabs bug bot "
				+ "running a motion sensor program designed by John Connolly.");
	}

	/**
	 * Displays the frame.
	 * 
	 */
	public void show() {
		frame.show();
	}

	/**
	 * Invoked when motion is detected by the IMotionSubject.
	 * 
	 * http://en.wikipedia.org/wiki/Observer_pattern
	 * 
	 */
	public void motionDetected() {
		LatLon latLon = position.getLatitudeLongitude();
		toc.send(destSN, "Motion Detected at Latitude: "
				+ Double.toString(latLon.latitude).substring(0, 7) + " by Longitude: "
				+ Double.toString(latLon.longitude).substring(0, 8));
	}

}