#!/bin/node

var path = require('path'),
    rs = require('recursive-search'),
    fs = require('fs'),
    polyline = require('polyline'),
    matchTrace = require('../src/match_trace.js'),
    async = require('async'),
    OSRM = require('osrm'),
    OSRMClient = require('osrm-client');

if (process.argv.length < 3) {
  console.error("Usage: node traces2geojson.js DATA_DIRECTORY [OSRM_DATA.osrm]");
  console.error("Will print geojson of the matched traces on stdout.");
  process.exit(1);
}

var directory = process.argv[2],
    // TODO fallback to osrm-client if no data is given
    data = process.argv.length > 3 && path.normalize(process.argv[3]) || undefined,
    osrm = data && new OSRM(data) || new OSRMClient('http://127.0.0.1:5000');


function encode_linestring(coordinates, type, file, confidence) {
    var data = {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": coordinates,
        },
        "properties": {
            "type": type
        },
    };

    if (file) data.properties.file = file;
    if (confidence) data.properties.confidence = confidence;
    return data;
}

function latLngToLngLat(coord) {
    return [coord[1], coord[0]];
}

function getGeoJSON(file, callback) {
  matchTrace(osrm, file, function (err, response) {
    if (err || !response.matchings) {
      console.error(err);
      callback(null, []);
      return;
    }

    var polylines = [];

    response.matchings.forEach(function(submatching) {
      var matched_points = polyline.decode(submatching.geometry, 6).map(latLngToLngLat);
      polylines.push(encode_linestring(matched_points, "matching", file, submatching.confidence));
    });
    polylines.push(encode_linestring(response.trace.coordinates.map(latLngToLngLat), "trace", file));

    callback(null, polylines);
  });
}

console.error("Loading files...");
var files = rs.recursiveSearchSync(/(.gpx|.csv)$/, directory);

console.error("Getting traces...");
async.mapLimit(files, 4, getGeoJSON, function (err, polyGroup) {
  var polylines = [];
  // flatten
  for (var i = 0; i < polyGroup; i++) {
    for (var j = 0; j < polyGrou.length; j++) {
      polylines.push(polyGroup[j]);
    }
  }
  console.error("Serializing...");
  console.log(JSON.stringify(polylines));
});


@@ -1369,19 +1369,42 @@
 e.error(
-err
+%22Could not match: %22 + file
 );%0A     
@@ -1681,20 +1681,20 @@
 ching%22, 
-file
+null
 , submat
@@ -1717,53 +1717,74 @@
 ));%0A
+%0A
     
-%7D);%0A    polylines.push(encode_linestring(
+  var subtrace = submatching.indices.map(function(i) %7Breturn 
 resp
@@ -1805,16 +1805,22 @@
 rdinates
+%5Bi%5D;%7D)
 .map(lat
@@ -1835,24 +1835,105 @@
 Lat)
-, %22trace%22, file)
+;%0A      polylines.push(encode_linestring(subtrace, %22trace%22, null, submatching.confidence));%0A    %7D
 );%0A%0A
@@ -2133,9 +2133,9 @@
 es, 
-4
+5
 , ge
@@ -2238,16 +2238,23 @@
 olyGroup
+.length
 ; i++) %7B
@@ -2286,16 +2286,20 @@
 polyGrou
+p%5Bi%5D
 .length;
@@ -2337,16 +2337,19 @@
 lyGroup%5B
+i%5D%5B
 j%5D);%0A   

Fix the geojson generation