/**
 * @author Sebastien Paumier (paumier@univ-mlv.fr)
 * Copyright Universite Paris-Est Marne-la-Vallee
 */
package fr.umlv.ig.td5;

import java.awt.*;
import java.util.*;
import java.util.List;

public class ShapeModelImpl implements ShapeModel {
	
	private static final int MAX_X = 300;
	private static final int MAX_Y = 300;

	private Random random=new Random();
	
	private ArrayList<Shape> list=new ArrayList<Shape>();
	
	/**
	 * @return the list of shapes sorted by depth
	 */
	public List<? extends Shape> getShapes() {
		return Collections.unmodifiableList(list);
	}
	
	/**
	 * Generates a random Shape and insert it in the model
	 */
	public void generateShape() {
		int x=random.nextInt(MAX_X);
		int y=random.nextInt(MAX_Y);
		int depth=random.nextInt(1000);
		Color c=new Color(random.nextInt());
		ShapeType type=ShapeType.types[random.nextInt(ShapeType.types.length)];
		int size=20+random.nextInt(150);
		Shape shape=new Shape(type,c,x,y,depth,size);
		int position=getPosition(depth);
		list.add(position,shape);
	}

	private int getPosition(int depth) {
		int max=list.size();
		if (max==0) return 0;
		int min=0;
		int current;
		while (min<max) {
			current=(min+max)/2;
			int d=list.get(current).getDepth();
			if (d==depth) return current;
			if (depth>d) {
				min=current+1;
			} else {
				max=current;
			}
		}
		return min;
	}

	/**
	 * Removes the shape #i.
	 */
	public void remove(int i) {
		/* A completer... */
	}
	

}
