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 LayoutByTeinei extends LayoutByIDHorizontal {
	public LayoutByTeinei(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) {
				double o1am = o1.teineiLevel();
				double o2am = o2.teineiLevel();
				if (o2am > o1am) return -1;
				else if (o2am == o1am) return 0;
				else return 1;
//				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);
	}

}
