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

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

import jp.ac.kyutech.mns.ist.Sheet;
import jp.ac.kyutech.mns.ist.SimpleATN;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.util.PBounds;


public class Event_onRegionsSS extends PBasicInputEventHandler {
	RegionSS region;
	
	PInputEvent pressEv = null;
//	Point2D pressP = null;
//	Point2D originP = null;
	PBounds originPB = null;
	int dragCount = 0;
	
	boolean isRB = false;
	
	
	public Event_onRegionsSS(RegionSS _region){
		region = _region;
	}
	public void mouseEntered(final PInputEvent aEvent){
		region.checkSS();
	}
	public void mousePressed(final PInputEvent aEvent) {
		pressEv = aEvent;
//		originP = region.getBounds().getOrigin();
		originPB = region.getBounds();
		dragCount = 0;
		boundReset();

		if (isRB(aEvent)==5){
        	isRB = true;
        } else {
        	isRB = false;
        }
        aEvent.setHandled(true);
	}
	public void boundReset(){
		double tx = region.getTransform().getTranslateX();
		double ty = region.getTransform().getTranslateY();
		region.getTransformReference(true).setOffset(0, 0);

		PBounds pb = region.getBounds();
		pb.moveBy(tx, ty);
		region.setBounds(pb);
	}
	public void mouseDragged(final PInputEvent aEvent) {
		dragCount++;
		final Dimension2D delta = aEvent.getDeltaRelativeTo(region);
        if (!isRB)
        	region.move(delta.getWidth(),delta.getHeight());
        else {
        	region.enlarge(delta);
        
        }
        aEvent.setHandled(true);
	}
	public void mouseReleased(final PInputEvent aEvent) {
		 SimpleATN.theapp.popCursor();
		 aEvent.setHandled(true);
		 
		if (dragCount < 3){
			if (pressEv.getButton()==MouseEvent.BUTTON1){
//single click
				//				Rectangle2D globalb = region.getGlobalBounds();
//				SimpleATN.theapp.canvas.getCamera().animateViewToCenterBounds(globalb, true, 1000);
				boundReset();
				PNode parent = region.getParent();
				if (parent instanceof Sheet) {
					((Sheet)parent).focus(aEvent);
				}
			} else if (pressEv.getButton()==MouseEvent.BUTTON3){
				new PopupMenu_forRegionSS(region, pressEv, SimpleATN.theapp.getCanvas());
			}
		}
		boundReset();
		//バウンダリチェック．親の枠をはみでたら戻す
		if (isOutParent()){
			region.animateToBounds(originPB.getX(), originPB.getY(), originPB.getWidth(), originPB.getHeight(), 500);
		}
	}
	public boolean isOutParent(){
		PNode parent = region.getParent();
		if (parent.getBounds().contains(region.getBounds())){
			return false;
		} else {
			return true;
		}
	}
	
	 public void mouseMoved(final PInputEvent aEvent){
     	if (isRB(aEvent)==5){
     		SimpleATN.theapp.pushCursor(Cursor.SE_RESIZE_CURSOR);
     	} else {
     		SimpleATN.theapp.popCursor();
     	}
        // aEvent.setHandled(true);
     }
	 public void mouseExited(final PInputEvent aEvent){
		 SimpleATN.theapp.popCursor();
		 boundReset();
	 }
	
	public int isRB(PInputEvent aEvent){
    	RegionSS sg = region;
    	double x = sg.getX();
    	double y = sg.getY();
    	double xx = sg.getBounds().getMaxX();
    	double yy = sg.getBounds().getMaxY();
    	Point2D pos = aEvent.getPositionRelativeTo(sg);
    	Point2D lefttop = new Point2D.Double(x,y);
    	Point2D righttop = new Point2D.Double(xx,y);
    	Point2D leftbottom = new Point2D.Double(x,yy);
    	Point2D rightbottom = new Point2D.Double(xx,yy);

//    	SimpleATN.theapp.canvas.pushCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    	int thre = 30;
    	if (pos.distance(rightbottom)<thre){
    		return 5;
    	} else if (pos.distance(righttop)<thre){
    		return 3;
    	} else if (pos.distance(leftbottom)<thre){
    		return 7;
    	} else if (pos.distance(lefttop)<thre){
//    		SimpleATN.theapp.canvas.pushCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
    		return 1;
    	} else {
    	}
    	return 0;
    }
}
