package jp.ac.kyutech.mns.ist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Timer;
import java.util.TimerTask;

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

public class LayoutByAmountHorizontal extends LayoutByIDHorizontal {
	public LayoutByAmountHorizontal(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);
			}
		}
		ArrayList<LayoutContent> thefr = new ArrayList<LayoutContent>();
		thefr.addAll(target.getLayoutContents());
		LayoutContent frary[] = thefr.toArray(new LayoutContent[]{});
		Arrays.sort(frary, new Comparator<LayoutContent>(){
			@Override
			public int compare(LayoutContent o1, LayoutContent o2) {
				int o1am = o1.drawingAmount();
				int o2am = o2.drawingAmount();
				return (o2am - o1am); //多い順にならべる
			}                		
		});
		ArrayList<LayoutContent> rects_sorted = new ArrayList<LayoutContent>();
		for(LayoutContent f: frary) rects_sorted.add(f);
		
		Timer t = new Timer();
		t.schedule(new LayoutTaskH(rects_sorted), wait);
	}

}
