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

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.util.PBounds;

public class LayerButton extends PPath {
	private static final long serialVersionUID = 6319519523614403467L;

	PCamera cam;
	PBounds viewpb;
	PLayer layer;
	PPath handle;
	boolean isHover;
	boolean isPress;
	boolean toggleMode = false;
	boolean isSelected;
	Point hoverShift = new Point(1,1);
	Point pressShift = new Point(-2,-2);
	Point selectedShift = new Point(-1,-1);
	ActionListener clickAction;
	
	public LayerButton(double x, double y, double w, double h, PLayer l) {
		Rectangle2D rect = new Rectangle2D.Double(0,0,w,h);
		setPathTo(rect);
		cam = new PCamera();
		cam.setPickable(true);
		addChild(cam);
		cam.setBounds(this.getBounds());
		cam.addLayer(l);
		setOffset(x,y);

		setStroke(new BasicStroke(0));
		
		handle = new PPath(this.getPathReference());
		handle.addAttribute("moveTarget", this);
		handle.setTransparency(0.0f);
		
		GradientPaint gp1 = new GradientPaint(0,0,new Color(200,221,242), 0,60,Color.white,true);
//		handle.setPaint(Color.lightGray);
		setPaint(gp1);
		
		
		handle.setStrokePaint(Color.gray);
		handle.setStroke(new BasicStroke(2));
		handle.setPickable(true);
		addChild(handle);
		handle.moveToFront();

		PBasicInputEventHandler handler = new PBasicInputEventHandler() {
			private int dragcount;
			public void mouseEntered(PInputEvent aEvent){
//				LayerButton.this.setStrokePaint(Color.blue);
				LayerButton.this.isHover = true;
				resetCam();
			}
			public void mouseExited(PInputEvent aEvent){
//				LayerButton.this.setStrokePaint(Color.cyan);
				LayerButton.this.isHover = false;
				resetCam();
			}
			public void mousePressed(PInputEvent aEvent) {
//				LayerButton.this.setStrokePaint(Color.orange);
				printEventCoords(aEvent);
				aEvent.setHandled(true);
				LayerButton.this.isPress = true;
				dragcount = 0;
				resetCam();
			}

			public void mouseDragged(PInputEvent aEvent) {
//				Dimension2D delta = aEvent.getDeltaRelativeTo(LayerButton.this);
//				LayerButton.this.translate(delta.getWidth(), delta.getHeight());
//				printEventCoords(aEvent);
//				aEvent.setHandled(true);
				dragcount++;
				resetCam();
			}

			public void mouseReleased(PInputEvent aEvent) {
//				LayerButton.this.setStrokePaint(Color.green);
				printEventCoords(aEvent);
				aEvent.setHandled(true);
				LayerButton.this.isPress = false;
				if (toggleMode && dragcount < 3){
					isSelected = !isSelected; 
				}
				resetCam();
			}
			public void printEventCoords(PInputEvent aEvent) {
//				System.out.println("Canvas Location: " + aEvent.getCanvasPosition());
//				System.out.println("Global Location: " + aEvent.getGlobalLocation());
//				System.out.println("Local Location: " + aEvent.getPositionRelativeTo(LayerButton.this));
//				System.out.println("Canvas Delta: " + aEvent.getCanvasDelta());
				//System.out.println("Global Delta: " + aEvent.getGlobalDelta());
//				System.out.println("Local Delta: " + aEvent.getDeltaRelativeTo(LayerButton.this));
			}
			public void mouseClicked(PInputEvent aEvent){
				if (!toggleMode){
					doClick(aEvent);
				}
			}
		};
		handle.addInputEventListener(handler);
//		cam.addInputEventListener(handler);
	}

	/**
	 * サイズを与える。
	 * @param shape
	 */
	public LayerButton(Shape shape) {
		super(shape);
	}

	public LayerButton(Shape shape, Stroke stroke) {
		super(shape, stroke);
	}
	
	public void setToggleMode(boolean toggle){
		toggleMode = toggle;
	}
	
	public void addClickActionListener(ActionListener al){
		clickAction = al;
	}
	public void doClick(PInputEvent a){
		if (clickAction != null){
			clickAction.actionPerformed(new ActionEvent(LayerButton.this,a.getModifiersEx(),
					"LayerButton Clicked"));
		}
	}

	/**
	 * ターゲットレイアをセットしてから呼ぶ
	 */
	public void resetCam(){
		PBounds pb = viewpb;
		if (isHover) {
			pb.moveBy(hoverShift.x, hoverShift.y);
			handle.setTransparency(0.4f);
		} else {
			handle.setTransparency(0.1f);
			pb.moveBy(0, 0);
		}
		if (isPress){
			pb.moveBy(pressShift.x,pressShift.y);
		}
		if (isSelected){
			pb.moveBy(selectedShift.x,selectedShift.y);
			handle.setTransparency(0.5f);
			handle.setPaint(Color.pink);
			handle.setStrokePaint(Color.red);
		} else {
//			handle.setTransparency(0.5f);
			handle.setPaint(Color.lightGray);
			handle.setStrokePaint(Color.gray);
		}
//		cam.setViewBounds(pb);
		repaint();
	}
	public void resetCamByCurrentLocation(){
		viewpb = this.getGlobalBounds();
		cam.setViewBounds(viewpb);
		repaint();
	}

}
