package jp.ac.kyutech.mns.ist;

/*
 * memo
 * SSGroup()
 * 	mousePressed(final PInputEvent aEvent)
 * 	isRB(PInputEvent aEvent)
 * 	mouseDragged(final PInputEvent aEvent)
 * 	mouseReleased(final PInputEvent aEvent)
 * 	mouseMoved(final PInputEvent aEvent)
 * 	printEventCoords(final PInputEvent aEvent)
 * 	enlarge(PInputEvent aEvent, Dimension2D delta)
 * 
 * method
 * 
 * transformation(double dx,double dy)
 * test(double dx,double dy)
 * move(double dx , double dy)
 * xenhance(double dx)
 * yenhance(double dy)
 * add(ShortStroke ss)
 * ShortStroke get(int i)
 * move(float dx ,float dy)
 * rebuild()
 */

import java.awt.Color;
import java.awt.geom.Dimension2D;
import java.util.ArrayList;

import jp.ac.kyutech.mns.ist.util.Util;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.util.PBounds;

public class SSGroup extends PPath {
	private static final long serialVersionUID = -7889536769896715472L;
	//member
	ArrayList<ShortStroke> sslist;
	
	Sheet sheet;
	//constructor
	public SSGroup(){
		sslist = new ArrayList<ShortStroke>();
		
		setStroke(Note.getCachedBasicStroke(10f));
		setPaint(Color.gray);
		setTransparency(0.05f);
		
		addInputEventListener(new Event_onSSGroup(this));//??TODO
		
	}
	public void setSheet(Sheet _s){
		sheet = _s;
	}
	protected void enlarge(Dimension2D delta) {
		//System.out.println(delta);
		PBounds pb = getBounds();
		pb.setSize(pb.getWidth()+delta.getWidth(), pb.getHeight()+delta.getHeight());
		setBounds(pb); // setPathToだと失敗する！
	}
	public void enlarge(double width, double height) {
	}
	//method
	
	public void transformation(Dimension2D delta){//拡大縮小の変形
		
		double wakux,wakuy,migix,sitay,plusx,plusy;
		SSGroup sg = SSGroup.this;
    	double x = sg.getX();
    	double y = sg.getY();
    	double xx = sg.getBounds().getMaxX();
    	double yy = sg.getBounds().getMaxY();
		
		wakux=xx-x;
		wakuy=yy-y;
		
		
		PBounds pb = getBounds();
		System.out.println(pb.getX());
	
		for(ShortStroke ss: sslist){
			for(int i=0;i<ss.sx.length;i++){
				System.out.println(ss.sx[i]);
				migix = wakux - (ss.sx[i]-x);
				plusx = delta.getWidth() / (migix+(ss.sx[i]-x)) * (ss.sx[i]-x);
				ss.sx[i] += plusx;
			}
		}
		for(ShortStroke ss: sslist){
			for(int i=0;i<ss.sy.length;i++){
				System.out.println(ss.sy[i]);
				sitay = wakuy - (ss.sy[i]-y);
				plusy = delta.getHeight() / (sitay+(ss.sy[i]-y)) * (ss.sy[i]-y);
				ss.sy[i] += plusy;
			}
			ss.rebuildStroke();
		}
	}
	
	public void move(double dx , double dy){
		 this.translate(dx, dy);
         for(ShortStroke n: sslist){
         	n.translate(dx, dy);    
         }
	}
	public void locate(double x, double y){
		//TODO: x
		double yoffset = y - getY();
		this.setOffset(getXOffset(), yoffset);
	}
	public void xenhance(double dx){
		PBounds pb = getBounds();
		System.out.println(pb.getX());
	
		for(ShortStroke ss: sslist){
			for(int i=0;i<ss.sx.length;i++){
				System.out.println(ss.sx[i]);
				ss.sx[i] += dx;
			}
			ss.rebuildStroke();
		}
		System.out.println("w "+pb.getWidth()); //getHeight();
		
//		sslist.get(0).sx[0] += dx;
//		sslist.get(0).rebuildStroke();
		
	}
	
	public void yenhance(double dy){
		PBounds pb = getBounds();
		System.out.println(pb.getX());
	
		for(ShortStroke ss: sslist){
			for(int i=0;i<ss.sy.length;i++){
				System.out.println(ss.sy[i]);
				ss.sy[i] += dy;
			}
			ss.rebuildStroke();
		}
		System.out.println("w "+pb.getWidth()); //getHeight();
	}
	
	
	public void add(ShortStroke ss){
		sslist.add(ss);
	}
	public ShortStroke get(int i){
		if (i >= sslist.size()) return null;
		return sslist.get(i);
	}
	
	public void move(float dx ,float dy){
		
		for(ShortStroke ss : sslist){
			ss.move(dx,dy);
			
		}
		
	}
	public void rebuild(){
		PBounds pb = new PBounds();
		for(ShortStroke ss: sslist){
			pb.add(ss.getBounds());
		}
		//pb = Util.getGapPBounds(5, 5, pb);
		pb = Util.getGapPBounds(0, 0, pb);//とりあえず余白をなくす（拡大縮小のため）
		//this.setBounds(pb);
		setPathTo(pb);
	}
	
}
