// This module calculates all the metrics for a given changeset

// require metric functions
var country = require('./metrics/country');
var roadCount = require('./metrics/road_count');
var roadCountMod = require('./metrics/road_count_mod');
var buildingCount = require('./metrics/building_count');
var buildingCountMod = require('./metrics/building_count_mod');
var waterwayCount = require('./metrics/river_count');
var poiCount = require('./metrics/poi_count');
var roadLength = require('./metrics/road_length');
var roadLengthMod = require('./metrics/road_length_mod');
var waterwayLength = require('./metrics/river_length');
var extentBuffer = require('./metrics/geo_extent_buffer');

var isNotRelation = (element) => element.type !== 'relation';
var hasTags = (element) => element.hasOwnProperty('tags');
var hasTag = (element, tag) => element.tags.hasOwnProperty(tag);
var isValidWay = (element) => {
  return (element.type === 'way') &&
  (hasTag(element, 'waterway') ||
    hasTag(element, 'highway') ||
    hasTag(element, 'building'));
};
var isValidNode = (element) => (element.type === 'node') && (hasTag(element, 'amenity'));

module.exports = function (changeset, precision) {
  changeset.elements = changeset.elements.filter((element) => {
    return isNotRelation(element) &&
      hasTags(element) &&
      (isValidWay(element) || isValidNode(element));
  });

  if (changeset.elements.length > 0) {
    var metadata = changeset.metadata;
    var buf = extentBuffer(500)(changeset);
    return {
      id: +metadata.id,
      hashtags: metadata.comment.split(' '),
      countries: country(buf),
      user: {
        id: +metadata.uid,
        name: metadata.user,
        avatar: '?', // todo: add avatar lookup
        geo_extent: buf
      },
      metrics: {
        road_count: roadCount(changeset),
        road_count_mod: roadCountMod(changeset),
        building_count: buildingCount(changeset),
        building_count_mod: buildingCountMod(changeset),
        waterway_count: waterwayCount(changeset),
        poi_count: poiCount(changeset),
        road_km: roadLength(changeset),
        road_km_mod: roadLengthMod(changeset),
        waterway_km: waterwayLength(changeset)
        // todo: add GPS trace lookup; placeholder functions return 0
      },
      editor: metadata.created_by,
      created_at: metadata.created_at
    };
  } else {
    return {};
  }
};

@@ -702,16 +702,24 @@
 ation = 
+function
 (element
@@ -712,34 +712,42 @@
 nction(element) 
-=%3E
+%7B%0A  return
  element.type !=
@@ -760,16 +760,18 @@
 ation';%0A
+%7D%0A
 var hasT
@@ -776,16 +776,24 @@
 sTags = 
+function
 (element
@@ -794,18 +794,26 @@
 lement) 
-=%3E
+%7B%0A  return
  element
@@ -833,24 +833,26 @@
 ty('tags');%0A
+%7D%0A
 var hasTag =
@@ -852,16 +852,24 @@
 asTag = 
+function
 (element
@@ -875,18 +875,26 @@
 t, tag) 
-=%3E
+%7B%0A  return
  element
@@ -920,16 +920,18 @@
 y(tag);%0A
+%7D%0A
 var isVa
@@ -939,16 +939,24 @@
 idWay = 
+function
 (element
@@ -952,27 +952,24 @@
 ion(element)
- =%3E
  %7B%0A  return 
@@ -1119,16 +1119,24 @@
 dNode = 
+function
 (element
@@ -1137,18 +1137,26 @@
 lement) 
-=%3E
+%7B%0A  return
  (elemen
@@ -1206,16 +1206,18 @@
 nity'));
+%0A%7D
 %0A%0Amodule
@@ -1310,16 +1310,24 @@
 .filter(
+function
 (element
@@ -1331,11 +1331,8 @@
 ent)
- =%3E
  %7B%0A 

remove fat arrow syntax to work with node v0.10