package jp.ac.kyutech.mns.ist.util;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.geom.Point2D;

import jp.ac.kyutech.mns.ist.SimpleATN;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.PInputManager;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.util.PBounds;

public class WheelRotationHandler extends PBasicInputEventHandler {
	public int direction = -1;
	public void setDirection(int d){
		direction = d;
	}
	PCamera cam;
	PCanvas can;
	Point2D cursorpoint;

	Robot robot;

	public WheelRotationHandler(PCanvas _can, PCamera _cam){
		cam = _cam;
		can = _can;
		try {
			robot = new Robot();
		} catch (AWTException e) {
		}
		can.addInputEventListener(new PInputManager() {
			public void mouseMoved(PInputEvent e) {
				cursorpoint = e.getCanvasPosition();
				try {
					e.getPath().getPathTransformTo(can.getLayer()).inverseTransform(cursorpoint, cursorpoint);
				} catch (RuntimeException e2) {
					cursorpoint = e.getPosition(); // Camera付きのメニューボタンや，背景部分(PCamera)のとき
				}
				// TODO: 本来であればRuntimeExceptionを出さずに処理したいのだが．．
				//				e.getPickedNode().localToParent(cursorpoint);
				//				System.out.println(e.getPickedNode().getRoot().getClass().getName());
				//				if (e.getPickedNode() instanceof PCamera) 
				//				else cursorpoint = e.getPositionRelativeTo(getLayer());
			}
		});

	}
	public void mouseWheelRotated(PInputEvent e) {
		if (!e.isControlDown() && !e.isShiftDown() && !e.isAltDown()) {

			//			System.out.println("wheel rotation: " + pc.getAttribute("hoge"));
			PBounds pb = cam.getViewBounds();
			//			System.out.println(pb.toString());
			float f;
			if (SimpleATN.isMACOSX){ //TODO:Macでもマウスの場合はどうするの？
				f = 1.0f + (0.04f * direction * e.getWheelRotation());
			} else {
				f = 1.0f + (0.2f * direction * e.getWheelRotation());
			}
			//System.out.println(f);
			if (e.getWheelRotation() //下に回すと正になるので
					* direction < 0) {
				pb = zoomBounds_focusbyCursor(pb, f);
			} else {
				pb = BUtil.zoomBounds(pb, f);
			}
			if (pb.x == 0 && pb.y == 0) {
				System.out.println("ズームイン位置がまだ準備前");
				return;
			}
			cam.animateViewToCenterBounds(pb, true, 0);
			//			if (MyPCanvas.this.wheelListener != null) MyPCanvas.this.wheelListener.mouseWheelRotated(f);
		}
	}

	public PBounds zoomBounds_focusbyCursor(PBounds pb, double rate) {
		double x = pb.getX();
		double y = pb.getY();
		double w = pb.getWidth();
		double h = pb.getHeight();
		double nw = w * rate;
		double nh = h * rate;
		Point2D camcp = cam.getViewBounds().getCenter2D();
		double camcx = camcp.getX();
		double camcy = camcp.getY();
		//		System.out.println("camcp "+camcx+" "+camcy);
		double curx = cursorpoint.getX();
		double cury = cursorpoint.getY();
		//		System.out.println("cursor "+curx+" "+cury);
		double nx = x - (nw - w) / 2 + curx - camcx;
		double ny = y - (nh - h) / 2 + cury - camcy;
		PBounds ret = new PBounds(nx, ny, nw, nh);
		cursorpoint = ret.getCenter2D();
		if (true) {
			moveCursorPointCenter();
		}
		return ret;
	}
	public void moveCursorPointCenter() {
		Point canvasglobalp = can.getLocationOnScreen();
		int canvasw = can.getWidth();
		int canvash = can.getHeight();
		if (robot != null)
			robot.mouseMove((int) (canvasglobalp.getX() + canvasw / 2),
					(int) (canvasglobalp.getY() + canvash / 2));
	}
}
