import Ember from 'ember';
import Evented from 'ember-evented';
import Service from 'ember-service';
import injectService from 'ember-service/inject';
import { A } from 'ember-array/utils';
import { isEmpty } from 'ember-utils';
import { throttle } from 'ember-runloop';

const { testing } = Ember;

export default Service.extend(Evented, {
  scrollActivity: injectService('ember-user-activity@scroll-activity'),

  EVENT_THROTTLE: 100,
  defaultEvents: ['keydown', 'mousedown', 'scroll', 'touchstart'],
  enabledEvents: null,
  _eventsListened: null,

  _throttledEventHandlers: null,

  _boundEventHandler: null,
  handleEvent(event) {
    throttle(this, this._throttledEventHandlers[event.type], event, this.get('EVENT_THROTTLE'));
  },

  _handleScroll() {
    this.handleEvent({ type: 'scroll' });
  },

  _setupListeners() {
    this.get('defaultEvents').forEach((eventName) => {
      this.enableEvent(eventName);
    });
  },

  _listen(eventName) {
    if (eventName === 'scroll') {
      this.get('scrollActivity').on('scroll', this, this._handleScroll);
    } else if (this.get('_eventsListened').indexOf(eventName) === -1) {
      this.get('_eventsListened').pushObject(eventName);
      window.addEventListener(eventName, this._boundEventHandler, true);
    }

  },

  init() {
    this._super(...arguments);

    if (testing) { // Do not throttle in testing mode
      this.set('EVENT_THROTTLE', 0);
    }
    this.setProperties({
      _boundEventHandler: this.handleEvent.bind(this),
      _eventsListened: A(),
      _throttledEventHandlers: {}
    });
    if (isEmpty(this.get('enabledEvents'))) {
      this.set('enabledEvents', A());
    }
    this._setupListeners();
  },

  on(eventName) {
    this.enableEvent(eventName);
    this._super(...arguments);
  },

  enableEvent(eventName) {
    if (!this.isEnabled(eventName)) {
      this.get('enabledEvents').pushObject(eventName);
      this._throttledEventHandlers[eventName] = function fireEnabledEvent(event) {
        if (this.isEnabled(event.type)) {
          this.fireEvent(event);
        }
      };
      this._listen(eventName);
    }
  },

  disableEvent(eventName) {
    this.get('enabledEvents').removeObject(eventName);
    this.set(`_throttledEventHandlers.${eventName}`, null);
    if (eventName === 'scroll') {
      this.get('scrollActivity').off('scroll', this, this._handleScroll);
    } else {
      window.removeEventListener(eventName, this._boundEventHandler, true);
    }
  },

  fireEvent(event) {
    // Only fire events that have subscribers
    if (this.has(event.type)) {
      this.trigger(event.type, event);
    }
    if (this.has('userActive')) {
      this.trigger('userActive', event);
    }
  },

  isEnabled(eventName) {
    return this.get('enabledEvents').indexOf(eventName) !== -1;
  },

  willDestroy() {
    this.get('_eventsListened').forEach((eventName) => {
      this.disableEvent(eventName);
    });

    this._super(...arguments);
  }
});

@@ -1074,29 +1074,24 @@
 se if (this.
-get('
 _eventsListe
@@ -1093,18 +1093,16 @@
 Listened
-')
 .indexOf
@@ -1126,37 +1126,32 @@
 1) %7B%0A      this.
-get('
 _eventsListened'
@@ -1149,29 +1149,21 @@
 Listened
-')
 .push
-Object
 (eventNa
@@ -1385,32 +1385,33 @@
 TLE', 0);%0A    %7D%0A
+%0A
     this.setProp
@@ -1407,30 +1407,8 @@
 his.
-setProperties(%7B%0A      
 _bou
@@ -1421,17 +1421,18 @@
 tHandler
-:
+ =
  this.ha
@@ -1451,24 +1451,27 @@
 nd(this)
-,
+;
 %0A    
-  
+this.
 _eventsL
@@ -1481,21 +1481,24 @@
 ened
-: A(),%0A      
+ = %5B%5D;%0A    this.
 _thr
@@ -1520,20 +1520,15 @@
 lers
-:
+ =
  %7B%7D
-%0A    %7D)
 ;
+%0A
 %0A   
@@ -2172,13 +2172,8 @@
 his.
-set(%60
 _thr
@@ -2195,11 +2195,9 @@
 lers
-.$%7B
+%5B
 even
@@ -2205,17 +2205,16 @@
 Name
-%7D%60,
+%5D =
  null
-)
 ;%0A  
@@ -2773,21 +2773,16 @@
    this.
-get('
 _eventsL
@@ -2788,18 +2788,16 @@
 Listened
-')
 .forEach
@@ -2857,16 +2857,53 @@
 %0A    %7D);
+%0A    this._eventsListened.length = 0;
 %0A%0A    th

Drop ember array observation stuff