<commit_before>'use strict';

/**
 * @ngdoc function
 * @name globiProtoApp.controller:MapCtrl
 * @description
 * # MapCtrl
 * Controller of the globiProtoApp
 */
angular.module('globiProtoApp')
  .controller('MapCtrl', function ($scope, $state, taxonInteractionDetails) {

    var buildMarkers = function(data) {
      var result = {};
      var allMarkers = data.map(function(item) {
        var obj = {
          lat: item.latitude,
          lng: item.longitude,
          message: item.study,
          focus: true,
          draggable: false
        };
        return obj;
      });
      // TODO Filter by unique lat/lng/msg and keep count of how many of each and modify message accordingly
      for(var i=0; i<allMarkers.length; i++) {
        if (allMarkers[i].lat && allMarkers[i].lng) {
          result['marker'+i] = allMarkers[i];
        }
      }
      return result;
    };

    taxonInteractionDetails.query({
      interactionType: $state.params.interactionType,
      sourceTaxon: $state.params.sourceTaxon,
      targetTaxon: $state.params.targetTaxon
    }, function(response) {
      $scope.markers = buildMarkers(response);
      // TODO calc center based on avg lat and lng of markers
      // TODO If no markers found - display error and/or redirect back to Learn
    }, function(err) {
      console.dir(err);
    });

    $scope.center = {
        lat: 45,
        lng: 18,
        zoom: 2
    };

    // $scope.markers = {
    //   osloMarker: {
    //     lat: 45,
    //     lng: 18,
    //     // message: "I want to travel here!",
    //     focus: true,
    //     draggable: false
    //   }
    // };

  });<commit_msg>calculate map center based on avg observation lat/lngs<commit_after>'use strict';

/**
 * @ngdoc function
 * @name globiProtoApp.controller:MapCtrl
 * @description
 * # MapCtrl
 * Controller of the globiProtoApp
 */
angular.module('globiProtoApp')
  .controller('MapCtrl', function ($scope, $state, taxonInteractionDetails) {

    // TODO in a real app, this should go in MarkerService
    var buildMarkers = function(data) {
      var uniqueHolder = {};
      data.forEach(function(item) {
        if (item.study && item.latitude && item.longitude) {
          // str.replace(/\s|\-/g,'')
          var uniqueKey = item.study.replace(/\s|\-/g, '') + item.latitude.toString().replace('-','#') + '_' + item.longitude.toString().replace('-','#');
          if (uniqueHolder[uniqueKey]) {
            uniqueHolder[uniqueKey].itemCount += 1;
            uniqueHolder[uniqueKey].message = item.study + ', ' + uniqueHolder[uniqueKey].itemCount + ' Observations';
          } else {
            uniqueHolder[uniqueKey] = {
              lat: item.latitude,
              lng: item.longitude,
              message: item.study + ', 1 Observations',
              focus: true,
              draggable: false,
              itemCount: 1
            };
          }
        }
      });
      return uniqueHolder;
    };

    var calculateCenter = function(markers) {
      var totalLat = 0;
      var totalLng = 0;
      Object.keys(markers).forEach(function(key) {
        totalLat += markers[key].lat;
        totalLng += markers[key].lng;
      });
      return {
        lat: totalLat / Object.keys(markers).length,
        lng: totalLng / Object.keys(markers).length,
        zoom: 2
      };
    };

    $scope.center = {};
    taxonInteractionDetails.query({
      interactionType: $state.params.interactionType,
      sourceTaxon: $state.params.sourceTaxon,
      targetTaxon: $state.params.targetTaxon
    }, function(response) {
      $scope.markers = buildMarkers(response);
      $scope.center = calculateCenter($scope.markers);
      console.dir($scope.center);
      // TODO If no markers found - display error and/or redirect back to Learn
    }, function(err) {
      console.dir(err);
    });

    // $scope.center = {
    //     lat: 45,
    //     lng: 18,
    //     zoom: 2
    // };

  });
{'fraction_non_alphanumeric': 0.13707637835103692, 'fraction_numerical': 0.005311077389984826, 'mean_word_length': 2.097102584181676, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3687781', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1188, 'n_words': 261}