import Ember from 'ember';
import moment from 'npm:moment';
import $ from 'jquery';

export default Ember.Controller.extend({
  spendingsMeter: 0.00,
  sumByCategory: null,
  pollingInterval: 10000,
  currencyRates: null,

  init () {
    this.set('currentMonthDisplaying', {
      month: parseInt(moment().format('MM')),
      year: parseInt(moment().format('YYYY')),
      isInPast: false
    });
    Ember.run.once(() => {
      this.onPoll();
    });
    this.startPolling();
  },

  displayCurrencyRates: function () {
    return this.get('currencyRates');
  }.property('currencyRates'),

  schedulePollEvent (event, interval) {
    var eventInterval = interval || this.get('pollingInterval');
    return Ember.run.later(() => {
      event.apply(this);
      this.set('timer', this.schedulePollEvent(event));
    }, eventInterval);
  },

  startPolling (interval) {
    this.set('timer', this.schedulePollEvent(this.get('onPoll'), interval));
  },

  stopPolling () {
    Ember.run.cancel(this.get('timer'));
  },

  onPoll () {
    const _this = this;
    return $.get('http://api.fixer.io/latest?base=USD')
      .then(response => _this.set('currencyRates', response))
      .catch(err => console.log(err));
  },

  dataToDisplay: function () {
    let month = this.get('currentMonthDisplaying').month;
    let year = this.get('currentMonthDisplaying').year;
    let timePeriodsList = [];

    // Filter expenses for current month and generate timePeriodsList while you at it.
    let filteredData = this.get('model').filter(item => {
      // Save values to be reused
      let timePeriod = [item.get('year'), item.get('month')];

      // Determine if timeperiod is alredy on the list
      let notOnList = timePeriodsList.every(arr => {
        if (arr[0] !== timePeriod[0]) {
          return true;
        } else {
          // if arr[0] === timePeriod[0]
          return arr[1] !== timePeriod[1];
        }
      });

      // Save if not on list
      if (notOnList) {
        timePeriodsList.push(timePeriod);
      }

      // Returns false if expense is not for current month
      return timePeriod[0] === year && timePeriod[1] === month;
    });

    this.set('timePeriodsList', timePeriodsList);
    return filteredData;
  }.property('model.@each'),

  updateSpendingsMeter: function (data) {
    let sumCounted = 0;
    let sumByCategory = [];
    this.get('dataToDisplay').forEach(item => {
      let sum = item.get('sum');
      let category = item.get('category');

      if (sumByCategory.findBy('category', category)) {
        sumByCategory.findBy('category', category).sum += parseFloat(sum);
      } else {
        sumByCategory.pushObject({
          category,
          sum: parseFloat(sum)
        });
      }
      sumCounted += parseFloat(sum);
    });
    this.set('spendingsMeter', sumCounted);
    this.set('sumByCategory', sumByCategory);
    return;
  }.observes('model.@each.sum'),

  formatedChartData: function () {
    const data = [
      ['Category', 'Spendings']
    ];

    this.get('sumByCategory').forEach(category => {
      data.push(
        [category.category, category.sum]
      );
    });
    return data;
  }.property('sumByCategory'),

  actions: {

    timePeriodHasChanged (timePeriod) {
      this.set('currentMonthDisplaying', timePeriod);
      this.set('model', this.store.findAll('expense'));
    },

    saveRecord (record) {
      let setTimePeriodRecord = record;

      // If timeperiod is in past it will set correct month and year for expense on save
      // otherwise default value is set in model
      if (this.get('currentMonthDisplaying').isInPast) {
        setTimePeriodRecord.month = this.get('currentMonthDisplaying').month;
        setTimePeriodRecord.year = this.get('currentMonthDisplaying').year;
      }

      let expense = this.store.createRecord('expense', setTimePeriodRecord);
      expense.save();
    }
  }
});

@@ -1065,19 +1065,20 @@
 eturn $.
-get
+ajax
 ('http:/
@@ -1111,21 +1111,54 @@
 USD'
-)
+, %7B
 %0A      
-.then
+dataType: 'jsonp'%0A    %7D)%0A      .done
 (res
@@ -1165,16 +1165,26 @@
 ponse =%3E
+ %7B%0A       
  _this.s
@@ -1212,16 +1212,25 @@
 esponse)
+;%0A      %7D
 )%0A      
@@ -1234,13 +1234,12 @@
    .
-catch
+fail
 (err

Use ajax instead of get for polling.