package jp.ac.kyutech.mns.ist;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.activities.PActivity;
import edu.umd.cs.piccolo.util.PBounds;

public class LayoutByIDHorizontal extends LayoutAbstract {
	public LayoutByIDHorizontal(PCamera cam){
		super(cam);
	}
	public void layout(int wait, LayoutTarget target){
		class LayoutTaskH extends TimerTask{
			ArrayList<LayoutContent> cnt;
			LayoutTaskH(ArrayList<LayoutContent> _cnt){ cnt = _cnt; }
			public void run(){
				ArrayList<PActivity> palist = moveH(cnt);
				long mtime = System.currentTimeMillis();
				for (PActivity pact : palist) if (pact != null) pact.setStartTime(mtime);
			}
		}
		Timer t = new Timer();
		t.schedule(new LayoutTaskH(target.getLayoutContents()), wait);
	}
	//横方向にならべるレイアウト
	public ArrayList<PActivity> moveH(ArrayList<LayoutContent> cnt){
		int horizontalNum = getBestFitLayoutH(cnt, camera.getViewBounds(), 0, 0);
		ArrayList<PActivity> palist = new ArrayList<PActivity>();

		int ix=0,iy=0;
		double x = 0, y = 0, ymax = 0, h = 0;
		double xgap = 0, ygap = 0;
		int horizonCount = 0;
		matrix.removeAll();
		for(LayoutContent pn: cnt){
			PActivity pa = pn.animateToPositionScaleRotation(x, y, 1, 0, 1000);
			//			System.out.println("x y "+x+" "+y);
			matrix.put(ix, iy, pn);
			palist.add(pa);
			x += pn.mysize().getWidth() + xgap;
			ix++;
			if (ymax < pn.mysize().getHeight()) ymax = pn.mysize().getHeight();
			horizonCount++;
			if (horizonCount == horizontalNum){
				horizonCount = 0;
				h = h + ymax + ygap;
				y = h;
				x = 0;
				ymax = 0;
				iy++;
				ix=0;
			}
		}
		return palist;
	}

	public PBounds contentregion(LayoutTarget target){
		ArrayList<LayoutContent> cnt = target.getLayoutContents();
		int horizontalNum = getBestFitLayoutH(cnt, camera.getViewBounds(), 0, 0);
		PBounds returnPB = new PBounds();
		double x = 0, y = 0, ymax = 0, h = 0;
		double xgap = 0, ygap = 0;
		int horizonCount = 0;
		for(LayoutContent pn: cnt){
//			if (!pn.hasContent()) continue;
			if (returnPB != null) {
				PBounds aPB = new PBounds(pn.mysize());
				aPB.setOrigin(x,y);
				returnPB.add(aPB);
			}
			x += pn.mysize().getWidth() + xgap;
			//			x += pn.getFullBoundsReference().getWidth() + xgap;
			if (ymax < pn.mysize().getHeight()) ymax = pn.mysize().getHeight();
//			if (xmax < pn.getFullBounds().getWidth()) xmax = pn.getFullBounds().getWidth();
			horizonCount++;
			if (horizonCount == horizontalNum){
				horizonCount = 0;
				h = h + ymax + ygap;
				y = h;
				x = 0;
				ymax = 0;
			}
		}
		return returnPB;
	}
	public int getBestFitLayoutH(ArrayList<LayoutContent> rects2,
			Rectangle2D availableScreenSize, double xgap, double ygap) {
		ArrayList<Rectangle2D> dims = new ArrayList<Rectangle2D>();
		for(LayoutContent fr: rects2){
			dims.add(fr.mysize());
		}
		int num = dims.size();
		double parentrate = availableScreenSize.getWidth()/availableScreenSize.getHeight();
		if (Double.isNaN(parentrate)) {
			parentrate = 1.444;
		}
//		System.out.println("LayoutByIDHori Parent "+parentrate);
		TreeMap<Double,Integer> resultRates = new TreeMap<Double,Integer>();

		for(int i=1;i<=num;i++){
			double fitrate = fitrateH(dims, i, xgap, ygap);
			resultRates.put(Math.abs(parentrate - fitrate), i);
//			System.out.println(i+"/"+num+" "+Math.abs(parentrate - fitrate)+" "+fitrate+" parent="+parentrate+"  fitrateH");
		}
		ArrayList<Integer> intres = new ArrayList<Integer>(resultRates.values());
		rownum_or_colnum = intres.get(0);
//		System.out.println("LayByIDH 採用"+rownum_or_colnum);

		return intres.get(0);
	}

}
