package foo.bar;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.GML;
import org.geotools.data.DataUtilities;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.wfs.v1_0.WFS;
import org.geotools.wfs.v1_0.WFSConfiguration;
import org.geotools.xml.Encoder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class GmlTest {
	private String typeName = "Exif2Gis";
	private final SimpleFeatureType TYPE;
	private String filename;
	private List<SimpleFeature> features = null;
	private GeometryFactory geometryFactory = null;
	private SimpleFeatureBuilder featureBuilder = null;
	private int id = 1;

	public GmlTest(String fileName, String typeName, String typeSpec) throws SchemaException {
		this.filename = fileName;
		TYPE = DataUtilities.createType(typeName, typeSpec);
		features = new ArrayList<SimpleFeature>();
		geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
        featureBuilder = new SimpleFeatureBuilder(TYPE);
	}

	public void addFeature(double longitude, double latitude, Object[] attributes) {
		Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
		
		featureBuilder.add(point);
		for (Object attribute : attributes) {
			featureBuilder.add(attribute);
		}
		SimpleFeature feature = featureBuilder.buildFeature(String.format("%08d", id++));
		features.add(feature);
	}
	
	public void writeGmlFeatures() {
		try {
			File locationFile = new File(filename + ".xsd");
			locationFile = locationFile.getCanonicalFile();
			locationFile.createNewFile();

			URL locationURL = locationFile.toURI().toURL();
			URL baseURL = locationFile.getParentFile().toURI().toURL();

			FileOutputStream xsd = new FileOutputStream(locationFile);

			GML encode = new GML(GML.Version.GML2);
			encode.setBaseURL(baseURL);
			encode.setNamespace("location", locationURL.toExternalForm());
			encode.encode(xsd, TYPE);

			xsd.close();

			SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);

			FileOutputStream xml = new FileOutputStream(filename + ".gml");

			Encoder encoder = new Encoder(new WFSConfiguration());
			encoder.encode(collection, WFS.FeatureCollection, xml);

			xml.close();

		}
		catch (IOException ioe) {
			Logger.getLogger(ShapeFileProducer.class.getName()).log(Level.SEVERE, null, ioe);
		}
	}

	public static void main(String[] args) throws SchemaException {
		String epsgCode = "4326";
		String typeSpec = "geometry:Point:srid=" + epsgCode + ",id:int,datetime:String,make:String,model:String,alt:double,image:String";
		GmlTest gmlTest = new GmlTest("gmltest","Exif2Gis", typeSpec);
		gmlTest.addFeature(51.58343, 11.4936, new Object[] {1, "2012.10.06 15:35:27", "Foo", "Bar", 242, "image_0001.jpg"});
		gmlTest.addFeature(51.58343, 11.4936, new Object[] {2, "2012.10.07 12:29:24", "Foo", "Bar", 253, "image_0001.jpg"});
		gmlTest.addFeature(51.58343, 11.4936, new Object[] {3, "2012.10.09 07:53:07", "Foo", "Bar", 280, "image_0001.jpg"});
		gmlTest.writeGmlFeatures();
	}
}
