package jp.ac.kyutech.mns.ist;

import java.awt.geom.Point2D;

import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

public class Event_onSheet_Draw extends PBasicInputEventHandler {
	Sheet sheet;

	PInputEvent pressEv = null;
	Point2D pressP = null;
	int dragCount = 0;
	//	DrawPenToolbar pentoolbar;
	ShortStroke squiggle;

	public Event_onSheet_Draw(Sheet _sheet) {
		sheet = _sheet;
		//		pentoolbar = _ptb;
	}
	public void mouseEntered(PInputEvent aEvent){
		sheet.isshowImage(true);
	}
	public void mouseExited(PInputEvent aEvent){
		sheet.isshowImage(false);
	}

	public void mousePressed(final PInputEvent aEvent) {
		pressEv = aEvent;
		pressP = aEvent.getPositionRelativeTo(sheet);
		dragCount = 0;
	}
	public void mouseDragged(final PInputEvent aEvent) {
		dragCount++;
		if (squiggle == null) {
			squiggle = new ShortStroke();

			squiggle.color = SimpleATN.theapp.drawToolbar.color.getRGB();
			squiggle.width = SimpleATN.theapp.drawToolbar.width;
			squiggle.penid = sheet.getNote().penid;
			squiggle.paperid = sheet.num;
			squiggle.time = System.currentTimeMillis();
			squiggle.addPoint(pressP);
			squiggle.applyTempPtsToAry();
			squiggle.moveTo((float) pressP.getX(), (float) pressP.getY() );
			squiggle.setTransparency(0.7f);
			squiggle.rebuildStroke();

			sheet.addChild(squiggle);

		} else {
			Point2D inkp = aEvent.getPositionRelativeTo(sheet);
			squiggle.addPoint(inkp);
			squiggle.lineTo((float) inkp.getX(),(float) inkp.getY());
		}
	}
	public void mouseReleased(final PInputEvent aEvent) {

		if (dragCount < 3){

		} else {
			Point2D inkp = aEvent.getPositionRelativeTo(sheet);
			if (sheet.getBounds().contains(inkp)){
				squiggle.applyTempPtsToAry();
				if (!SimpleATN.theapp.isNoDB){
					SimpleATN.theapp.mysqldb.insertStroke(squiggle);
				} else {
					sheet.getNote().addStroke(squiggle);//DBからの読み込みが期待できないので，かわりに追加する（これしないとファイル保存ができない）
				}
			}
			//			squiggle.removeFromParent();

			squiggle = null;
		}
	}	

}
