import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;

import fr.ocelet.runtime.List;

/**
 * Holds a list of list of Positions Every Mpoints can contain more than one
 * Line. On Line is a list of Positions
 * 
 * @author pdegenne@teledetection.fr
 */
public class MPoints {

	private List<Position> listOfPoints;

	public MPoints(final List<Position> llp) {
		super();
		this.listOfPoints = llp;
	}

	public MPoints() {
		super();
		listOfPoints = new List<Position>();
	}

	public MPoints(Geometry geom) {
		super();
		listOfPoints = new List<Position>();
		Coordinate[] coords = geom.getCoordinates();
		for (Coordinate coord : coords) {
			addPoint(new Position(coord.x, coord.y));
		}
	}

	/**
	 * Adds one line to a Mpoints
	 * 
	 * @param line
	 */
	public void addPoint(Position point) {
		listOfPoints.add(point);
	}

	/**
	 * Gives the number of Lines contained in this Mpoints
	 * 
	 * @return An int number
	 */
	public int nbPoints() {
		return listOfPoints.size();
	}

	/**
	 * Returns one Point from this Mpoints
	 * 
	 * @param index
	 *            Number of the required Point
	 * @return One Point in the form of a Position
	 */
	public Position getPoint(int index) {
		return listOfPoints.get(index);
	}

	/**
	 * Produces a JTS Geometry object from the content of this Mpoints
	 * 
	 * @return Either a LineString or a MultiLineString depending on the number
	 *         of geometry elements contained in the Mpoints
	 */
	public Geometry asGeometry() {
		int sz = listOfPoints.size();
		Geometry geom = null;
		Point[] points = new Point[sz];
		for (int lsi = 0; lsi < sz; lsi++) {
			Position ppos = listOfPoints.get(lsi);
			Coordinate coords[] = new Coordinate[1];
			coords[0] = new Coordinate(ppos.x, ppos.y);
			points[lsi] = new Point(new CoordinateArraySequence(coords),
					new GeometryFactory());
		}
		geom = new MultiPoint(points, new GeometryFactory());
		return geom;
	}

}
