package jp.ac.kyutech.mns.ist;

import java.awt.Color;
import java.awt.Dimension;

import jp.ac.kyutech.mns.ist.sheetmap.BUtil;
import jp.ac.kyutech.mns.ist.util.CameraFitConstraint;
import jp.ac.kyutech.mns.ist.util.CameraFitConstraint.PositionEnum;
import edu.umd.cs.piccolo.PCamera;
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.nodes.PPath;
import edu.umd.cs.piccolo.util.PBounds;
import edu.umd.cs.piccolo.util.PDimension;

public class InsideToolbar_ZoomHandle extends PPath implements Runnable {
	private static final long serialVersionUID = -7048236395728223700L;
	PCamera cont;
//	Point2D pressP;
	double diffsum = 0;
	PPath[] tabs = new PPath[3];
	Thread thread;
	//	ArrayList<PPath> palettes;
	public InsideToolbar_ZoomHandle(PCamera _cam){
		cont = _cam;

//		int pos = 0;
		for(int i=0;i<tabs.length;i++){
			PPath p = PPath.createRectangle(0, 0, 20, 35);
			p.setPaint(Color.blue);
			p.setTransparency(0.4f);
			p.setOffset(0, i*5);
			p.setPickable(true);
			p.setVisible(true);
			addChild(p);
			tabs[i]=p;
			p.addAttribute("nozoom", true);
		}

		//		satn.getCanvas().getCamera().addChild(this);
		setBounds(getFullBoundsReference());
		//		fitToFR();

		CameraFitConstraint cfc = new CameraFitConstraint(this, cont, PositionEnum.RIGHTCENTER, new Dimension(0,0));
		cfc.fitTo(cont.getBoundsReference());
		cfc.setAlwaysShow(true);
		cont.addPropertyChangeListener(cfc);

		setTransparency(0.8f);
		setVisible(true);
		setPickable(true);
		
		addInputEventListener(new PBasicInputEventHandler(){
//			public void mouseEntered(PInputEvent e){
//				PNode pn = e.getPickedNode();
//				if (pn instanceof PPath){
//					System.out.println("Enter");
//					e.setHandled(true);
//				}
//			}
			public void mousePressed(PInputEvent e){
				PNode pn = e.getPickedNode();
				if (pn instanceof PPath){
//					System.out.println(e.getPositionRelativeTo(cont));
//					pressP = e.getPositionRelativeTo(cont);
					e.setHandled(true);
					diffsum = 0;
					thread = new Thread(InsideToolbar_ZoomHandle.this);//スレッドスタート
					thread.start();
				}
			}
			public void mouseDragged(PInputEvent e){
				PNode pn = e.getPickedNode();
				if (pn instanceof PPath){
//					Point2D dragP = e.getPositionRelativeTo(cont);
					PDimension diff = e.getDeltaRelativeTo(InsideToolbar_ZoomHandle.this);
					InsideToolbar_ZoomHandle.this.offset(0,diff.height);
//実際のズーム変化はrun()で行う．
					diffsum += diff.height;
					e.setHandled(true);

				}
			}
			public void mouseReleased(PInputEvent e){
				PNode pn = e.getPickedNode();
				if (pn instanceof PPath){
					InsideToolbar_ZoomHandle.this.offset(0,-diffsum);
					thread = null; //スレッドストップ
					e.setHandled(true);
				}
			}
		});
	}
	public void run(){
		while(thread != null){
			PBounds pb = cont.getViewBounds();
			PBounds pb2 = BUtil.zoomBounds(pb, (100.0-diffsum)/100.0);
			cont.setViewBounds(pb2);
			cont.repaint();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public void setVisible(boolean f){
		super.setVisible(f);
		updateStatus();
	}
	public void updateStatus(){
	}
}
