
package org.mote.wiimote.whiteboard.mouse.rightclick;

import java.awt.Point;

import org.mote.wiimote.whiteboard.mouse.Mouse;
import org.mote.wiimote.whiteboard.preferences.WWPreferences;

public abstract class AbstractRightClick implements RightClickStrategy {

	private long lightStartTime = 0;
	private Point lightStartPoint = null;
	private boolean active = false;

	protected abstract void activate();
	protected abstract void deactivate();

	public void process(Point p) {
		if (p != null) {
			// reset start point for potential right click
			if (!active && (lightStartPoint == null || lightStartPoint.distance(p) > WWPreferences.PIXEL_MOVE_TOLERANCE)) {
				lightStartPoint = p;
				lightStartTime = System.currentTimeMillis();
			}
		} else {
			lightStartPoint = null;
			if (active) {
				active = false;
				deactivate();
			}
		}
	}

	public boolean trigger() {
		// light hasn't moved in the same area for long enough to trigger right click
		if (lightStartPoint != null && System.currentTimeMillis() - lightStartTime > WWPreferences.getPreferences().getRightClickDelay()) {
			if (!active) {
				active = true;
				Mouse.LEFT_BUTTON.setPressed(false);
				activate();
			}
			return true;
		}
		return false;
	}

}
