<commit_before>angular.module('pvta.factories')

.factory('Trips', function () {
  var trips = [];
  var loadedTrip = null;
  var lastPoppedIndex = 0;
  var steps = [];

  var getAll =  function (callback) {
    localforage.getItem('savedTrips', function (err, value) {
      if (err) {
        console.log('Error loading trips.;');
        callback([]);
        return;
      }
      if (value === null) {
        console.log('No trips loaded');
        callback([]);
        return;
      }
      trips = JSON.parse(value);
      for (var i = 0; i < trips.length; i = i + 1) {
        trips[i].time.datetime = new Date(trips[i].time.datetime);
      }
      callback(trips);
    });
  };

  var push = function (index) {
    lastPoppedIndex = index;
    loadedTrip = trips[index];
  };

  var pop = function () {
    var toReturn = loadedTrip;
    loadedTrip = null;
    return toReturn;
  };

  var set = function (trip) {//Sets a new trip object at the index of the last trip popped
    trips[lastPoppedIndex] = trip;
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };

  var add = function (trip) {
    trips.push(trip);
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };

  var remove = function (index) {
    trips.splice(index, 1);
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };
  
  function route(params, directionsDisplay, callback) {//params.origin.id and params.destination.id required
                          // directionDisplay is google.maps.DirectionsRenderer
                          //optional: params.datetime.time
                          //          params.datetime.type 'departure' or 'arrival'
    directionsService = new google.maps.DirectionsService;
    var route = {};
    transitOptions = {
      modes: [google.maps.TransitMode.BUS]
    };
    if (params.time.datetime !== undefined && params.time.type !== undefined && params.time.asap !== true) {
      if (params.time.type === 'departure') {
        transitOptions['departureTime'] = params.time.datetime;
      }
      else {
        transitOptions['arrivalTime'] = params.time.datetime;
      }
    }
    directionsService.route({
      origin: {'placeId': params.origin.id},
      destination: {'placeId': params.destination.id},
      travelMode: google.maps.TravelMode.TRANSIT,
      transitOptions: transitOptions
    }, function (response, status) {
      route.status = status;
      if (status === google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        leg = response.routes[0].legs[0];
        steps = leg.steps;
        route.arrivalTime = leg['arrival_time']['text'];
        route.departureTime = leg['departure_time']['text'];
        route.origin = leg['start_address'];
        route.destination = leg['end_address'];
      } else console.log(status);
      callback(route);
    });<commit_msg>for planning a walking trip<commit_after>angular.module('pvta.factories')

.factory('Trips', function () {
  var trips = [];
  var loadedTrip = null;
  var lastPoppedIndex = 0;
  var steps = [];

  var getAll =  function (callback) {
    localforage.getItem('savedTrips', function (err, value) {
      if (err) {
        console.log('Error loading trips.;');
        callback([]);
        return;
      }
      if (value === null) {
        console.log('No trips loaded');
        callback([]);
        return;
      }
      trips = JSON.parse(value);
      for (var i = 0; i < trips.length; i = i + 1) {
        trips[i].time.datetime = new Date(trips[i].time.datetime);
      }
      callback(trips);
    });
  };

  var push = function (index) {
    lastPoppedIndex = index;
    loadedTrip = trips[index];
  };

  var pop = function () {
    var toReturn = loadedTrip;
    loadedTrip = null;
    return toReturn;
  };

  var set = function (trip) {//Sets a new trip object at the index of the last trip popped
    trips[lastPoppedIndex] = trip;
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };

  var add = function (trip) {
    trips.push(trip);
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };

  var remove = function (index) {
    trips.splice(index, 1);
    localforage.setItem('savedTrips', JSON.stringify(trips), function (err) {
      if (err !== null) {
        console.log('Error saving trips.');
      }
    });
  };
  
  function route(params, directionsDisplay, callback) {//params.origin.id and params.destination.id required
                          // directionDisplay is google.maps.DirectionsRenderer
                          //optional: params.datetime.time
                          //          params.datetime.type 'departure' or 'arrival'
    directionsService = new google.maps.DirectionsService;
    var route = {};
    transitOptions = {
      modes: [google.maps.TransitMode.BUS]
    };
    if (params.time.datetime !== undefined && params.time.type !== undefined && params.time.asap !== true) {
      if (params.time.type === 'departure') {
        transitOptions['departureTime'] = params.time.datetime;
      }
      else {
        transitOptions['arrivalTime'] = params.time.datetime;
      }
    }
    directionsService.route({
      origin: {'placeId': params.origin.id},
      destination: {'placeId': params.destination.id},
      travelMode: google.maps.TravelMode.TRANSIT,
      transitOptions: transitOptions
    }, function (response, status) {
      route.status = status;
      if (status === google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        leg = response.routes[0].legs[0];
        steps = leg.steps;
        if (leg['arrival_time']) {
          route.arrivalTime = leg['arrival_time']['text'];
        }
        if (leg['departure_time']) {
          route.departureTime = leg['departure_time']['text'];
        }
        route.origin = leg['start_address'];
        route.destination = leg['end_address'];
      } else console.log(status);
      callback(route);
    });
{'fraction_non_alphanumeric': 0.14496768236380425, 'fraction_numerical': 0.0018467220683287165, 'mean_word_length': 2.4044002095337875, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930762', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1945, 'n_words': 416}