/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.geotools.GpsMark;

import java.awt.Color;
import java.net.*;

import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;

import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;



import java.io.File;

import org.geotools.data.DataUtilities;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;


/**
 *
 * @author BURKAY
 */
public class GpsMark extends Thread{
    
    public static void main(String[] args) throws Exception {
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }
        File file2 = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file2 == null) {
            return;
        }
        File file3 = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file3 == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();

        FileDataStore store2 = FileDataStoreFinder.getDataStore(file2);
        FeatureSource featureSource2 = store2.getFeatureSource();

        FileDataStore store3 = FileDataStoreFinder.getDataStore(file3);
        FeatureSource featureSource3 = store3.getFeatureSource();

        Style metropolitanStyle = SLD.createPointStyle("Star", Color.blue, Color.red, 1, 10, "Metro", null);
        Style trainsStyle = SLD.createPointStyle("Circle", Color.red, Color.blue, 1, 10);
        Style trainLineStyle = SLD.createLineStyle(Color.red, 2);
        Style cityLineStyle = SLD.createLineStyle(Color.black, 1);

        final SimpleFeatureType TYPE = DataUtilities.createType(
            "Location",                      // <- the name for our feature type
            "location:Point:srid=4326," +    // <- the geometry attribute: Point type
            "name:String"                    // <- a String attribute
            );

        FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
            FeatureCollections.newCollection();

        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

        MapContext map = new DefaultMapContext();
        map.setTitle("Train Tracking");
        map.addLayer(featureSource, cityLineStyle);
        map.addLayer(featureSource2, trainLineStyle);
        map.addLayer(featureSource3, metropolitanStyle);


        //map.addMapLayerListListener(null);
        //map.addPropertyChangeListener(null);
        //collection.addListener(null);
        map.addLayer(collection, trainsStyle);
        JMapFrame.showMap(map);


        try {            

            DatagramSocket socket = new DatagramSocket(5000);
            byte[] buf = new byte[256];

            while(true) {

                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                String received = new String(packet.getData(), 0, packet.getLength());
                //System.out.println(received);
                
                String tokens[] = received.split("\\|");

                double longitude = Double.parseDouble(tokens[3]);
                double latitude = Double.parseDouble(tokens[4]);
                String name = tokens[0].trim();
                //System.out.println(longitude+" "+latitude);

                Point point = geometryFactory.createPoint(new Coordinate(latitude, longitude));

                featureBuilder.add(point);
                featureBuilder.add(name);
                SimpleFeature feature = featureBuilder.buildFeature(null);
                FeatureCollection<SimpleFeatureType, SimpleFeature> collection2 =
                    FeatureCollections.newCollection();
                collection2.add(feature);
                map.removeLayer(2);
                map.addLayer(collection2, trainsStyle);
                //System.out.println(collection.size());
                //sleep(4000);
            }
        } catch(Exception ex){
            ex.printStackTrace();
        }

        // Now display the map


    }
}
