'use strict';

/**
 * @ngdoc function
 * @name atacamaApp.controller:ChartCtrl
 * @description
 * # ChartCtrl
 * Controller of the atacamaApp
 */
angular.module('atacamaApp')
  .controller('ChartCtrl', function ($scope, $stomp, $resource, tickService, Restangular) {
    console.log('ChartCtrl has been created');

    var url = 'http://localhost:48002';

    var sod = moment(0, "HH").format("x");

    // {"date": 1437583864374, "open": 100.0, "high": 100.24021489109903, "low": 98.2724267098159, "close": 99.51909089116204, "volume": 107.79215866544341, "symbol": "ABC.L", "exchange": "FTSE100", "timestamp": "2015-07-22T17:52:04.377+01:00" }

    var exchange = 'FTSE100';
    var symbol = 'ABC';


    $stomp
      .connect(url + '/ticks', [])

      // frame = CONNECTED headers
      .then(function (frame) {
        console.log('connected to tick websocket');
        var subscription = $stomp.subscribe('/topic/ticks.' + exchange + '.' + symbol, function (payload, headers, res) {
          // alert(payload.close);
          // var point = {x: payload.date, y: payload.close};
          var open = _.map([payload], _.curry(convertTicks)('open'));
          var high = _.map([payload], _.curry(convertTicks)('high'));
          var low = _.map([payload], _.curry(convertTicks)('low'));
          var close = _.map([payload], _.curry(convertTicks)('close'));

          // TODO support multiple ticks...
          $scope.data[0].values.push(open[0]);
          $scope.data[1].values.push(high[0]);
          $scope.data[2].values.push(low[0]);
          $scope.data[3].values.push(close[0]);
          $scope.$apply();
        }, {
          "headers": "are awesome"
        });

      });
//dummy
    // $scope.data = sinAndCos();

    var open = [],
        high = [],
        low = [],
        close = [];

    $scope.data = [
          {
              values: open,
              key: "open",
              color: "#bdc42d"
          },
          {
              values: high,
              key: "high",
              color: "#2ca02c"
          },
          {
              values: low,
              key: "low",
              color: "#9f442c"
          },
          {
              values: close,
              key: "close",
              color: "#2c649f"
          }
        ];


    function convertTicks(property, ticks) {
     return _(ticks).pick([property, 'date']).mapKeys(renameProperties).value();
    }

    function renameProperties(value, key) {
      switch (key) {
        case 'open':
        case 'close':
        case 'high':
        case 'low':
          return 'y';
        case 'date':
          return 'x'
        default:
          return 'unknown';
     }
     return key;
   }

    var ticks = Restangular.one('tick').one(symbol).one(sod);

    ticks.get().then(function (response) {
     // $scope.data[0].values = response.ticks;

     // console.log("original ticks:" + JSON.stringify(response.ticks));

     $scope.data[0].values = _.sortBy(_.map(response.ticks, _.curry(convertTicks)('open')), 'x');
     $scope.data[1].values = _.sortBy(_.map(response.ticks, _.curry(convertTicks)('high')), 'x');
     $scope.data[2].values = _.sortBy(_.map(response.ticks, _.curry(convertTicks)('low')), 'x');
     $scope.data[3].values = _.sortBy(_.map(response.ticks, _.curry(convertTicks)('close')), 'x');

    });


    $scope.options = {
      chart: {
        type: 'lineChart',
        height: 450,
        margin: {
          top: 20,
          right: 20,
          bottom: 50,
          left: 75
        },
        // x: function (d) {
        //   return d['date'];
        // },
        // y: function (d) {
        //  return d['close'];
        // },
        showValues: true,
        transitionDuration: 500,
        xAxis: {
          axisLabel: 'Dates',
          tickFormat: function (d) {
            return d3.time.format('%X')(new Date(d));
          },
        },
        yAxis: {
          axisLabel: 'Stock Price',
          tickFormat: function (d, i) {
            return '$' + d3.format(',.1f')(d);
          }
        }
      }
    };

  });

@@ -979,24 +979,25 @@
 ers, res) %7B%0A
+%0A
           //
@@ -1001,91 +1001,84 @@
  // 
-alert(payload.close);%0A          // var point = %7Bx: payload.date, y: payload.close%7D;
+TODO remove the square brackets if sent an array instead of single object...
 %0A   
@@ -1377,33 +1377,53 @@
 ODO 
-support multiple ticks...
+is there a way of merging arrays in lodash..?
 %0A   
@@ -1421,32 +1421,59 @@
 sh..?%0A          
+Array.prototype.push.apply(
 $scope.data%5B0%5D.v
@@ -1481,21 +1481,14 @@
 lues
-.push(
+, 
 open
-%5B0%5D
 );%0A 
@@ -1488,32 +1488,59 @@
 pen);%0A          
+Array.prototype.push.apply(
 $scope.data%5B1%5D.v
@@ -1548,21 +1548,14 @@
 lues
-.push(
+, 
 high
-%5B0%5D
 );%0A 
@@ -1555,32 +1555,59 @@
 igh);%0A          
+Array.prototype.push.apply(
 $scope.data%5B2%5D.v
@@ -1615,20 +1615,13 @@
 lues
-.push(low%5B0%5D
+, low
 );%0A 
@@ -1625,24 +1625,51 @@
 ;%0A          
+Array.prototype.push.apply(
 $scope.data%5B
@@ -1681,22 +1681,15 @@
 lues
-.push(
+, 
 close
-%5B0%5D
 );%0A 

support multiple ticks from websocket