import Ember from 'ember';
import moment from 'moment';
import { fetchDepartures } from 'bus-detective/utils/api';
var run = Ember.run;

const DEFAULT_POLL_INTERVAL = 15 * 1000;
const DEFAULT_CHECK_INTERVAL = 0.5 * 1000;
const DEFAULT_DURATION_IN_HOURS = 1;

export default Ember.Object.extend(Ember.Evented, {
  stopId: null,
  intervalId: null,
  hasFetched: false,

  init() {
    this._super();
    this.setProperties({
      pollInterval: this.get('pollInterval') || DEFAULT_POLL_INTERVAL,
      checkInterval: this.get('checkInterval') || DEFAULT_CHECK_INTERVAL,
      duration: this.get('duration') || DEFAULT_DURATION_IN_HOURS
    });
  },

  fetch() {
    this.set('nextFetchTime', moment().add(this.get('pollInterval'), 'milliseconds'));
    return fetchDepartures({ stopId: this.get('stopId'), duration: this.get('duration') }).then(run.bind(this, 'handleFetchSuccess'))
  },

  handleFetchSuccess(response) {
    this.set('hasFetched', true);
    this.trigger('didFetch', response.get('departures'));
  },

  fetchIfNessessary() {
    let now = moment();
    if (now.isAfter(this.get('nextFetchTime'))) {
      this.fetch();
    }
  },

  startFetching() {
    this.set('intervalId', setInterval(run.bind(this, 'fetchIfNessessary'), this.get('checkInterval')));
    return this.fetch();
  },

  stopFetching() {
    clearInterval(this.get('intervalId'));
  },

  increaseDuration() {
    this.set('duration', this.get('duration') + 3);
    this.fetch();
  }
});

@@ -365,111 +365,23 @@
 se,%0A
-%0A  init() %7B%0A    this._super();%0A    this.setProperties(%7B%0A      pollInterval: this.get('pollInterval') %7C%7C
+  pollInterval:
  DEF
@@ -400,20 +400,16 @@
 TERVAL,%0A
-    
   checkI
@@ -420,37 +420,8 @@
 val:
- this.get('checkInterval') %7C%7C
  DEF
@@ -441,20 +441,16 @@
 TERVAL,%0A
-    
   durati
@@ -456,32 +456,8 @@
 ion:
- this.get('duration') %7C%7C
  DEF
@@ -482,20 +482,8 @@
 OURS
-%0A    %7D);%0A  %7D
 ,%0A%0A 

Remove unnecessary init function