angular.module('app.services', [

])
.factory('queryService',function($http, chartService){

  return {
    getDataFromServer: getDataFromServer
  };

  function getDataFromServer(endpoint, callback){
    function success(response){
      callback(response.data);
    }

    function error(err){
      console.log("error geting from database ==>", error);
    }

    $http.get(endpoint)
      .then(success, error)
  }



})
.factory('navService', function($http) {
  //var country = keys.collections.geo.united_states;
  // country.states = Object.keys(keys.collections.geo.united_states);
  // country.states.unshift('Select State:');

  return {
    getHubsFromServer: getHubsFromServer,
    getViewsFromServer: getViewsFromServer,
    selectedHub: "san_francisco",
  };

  function getHubsFromServer(callback) {

    function success(response){
      callback(response.data);
    }

    function error(err){
      console.log("error fetching hubs ==>", err)
    }

     $http.get('/analyzed-data/hubs')
      .then(success, error);
  }

  function getViewsFromServer(callback){
    function success(response){
      callback(response.data);
    }
    function error(err){
      console.log("error getting view list from database ==>", err);
    }

    $http.get("/analyzed-data/views")
      .then(success, error);
  }

})
.factory('chartService',function(){

  function createCategories(hubDataPoints){
    //each databpoint is a list of techs and numbers
    let keys = Object.keys(hubDataPoints[0]);
    let result = [];

    keys.forEach( (category) => {
      let categoryObject = {
        name: category,
        data: []
      };
      hubDataPoints.forEach( (dataPoint) => {
        categoryObject.data.push(dataPoint[category]);
      });
      result.push(categoryObject);
    });
    return result;
  }

  function extractDates(dateList){
    return dateList.map((data) => {
      return moment(data.date, "x").format('MMMM DD'); //change format later
    });
  }

  function extractDataPoints(dataList){
    return dataList.map((data) => {
      return data.data;
    });
  }

  function highChartsFormat(data){
    let result = {};

    result.dates = extractDates(data);
    //result for particular view

    result.data = createCategories(extractDataPoints(data));

    return result;
  }
  //aray of objects with name and data
  //iterate over the views
  function formatViews(data){
    var obj = {};
    for(let key in data) { //each individual view
      //skip unfilled views
        if(data[key].length > 0){
          obj[key] = highChartsFormat(data[key]);
        }
      }
    return obj;

  }

  return {
    formatResponseData: formatViews,
    highChartsFormat: highChartsFormat,
    // extractDataPoints: extractDataPoints,
    // createCatagories: createCatagories,
    // extractDates: extractDates,
  };

})

@@ -2197,31 +2197,284 @@
 ion 
-highChartsFormat(data)%7B
+sortData(dataList)%7B%0D%0A    return dataList.sort((a, b) =%3E %7B%0D%0A      if(a.date %3E b.date)%7B%0D%0A        return 1;%0D%0A      %7D%0D%0A      if( a.date %3C b.date)%7B%0D%0A        return -1%0D%0A      %7D%0D%0A      return 0%0D%0A    %7D);%0D%0A  %7D%0D%0A%0D%0A  function highChartsFormat(data)%7B%0D%0A    let sortedData = sortData(data);
 %0D%0A  
@@ -2527,17 +2527,23 @@
 ctDates(
-d
+sortedD
 ata);%0D%0A 
@@ -2630,17 +2630,23 @@
 aPoints(
-d
+sortedD
 ata));%0D%0A

sorts data by date