import Ember from 'ember';

export default Ember.Service.extend({
  isCharging: null,
  level: null,
  chargingTime: null,
  dischargingTime: null,
  levelPercentage: Ember.computed('level', function() {
    return this.get('level') * 100;
  }),

  init() {
    this._super(...arguments);
    if (navigator && navigator.getBattery) {
      this.set('isSupported', true);
      navigator.getBattery().then((battery) => {
        this._battery = battery;
        this.set('isCharging', battery.charging);
        this.set('level', battery.level);
        this.set('chargingTime', battery.chargingTime);
        this.set('dischargingTime', battery.dischargingTime);
        this.addListeners();
      });
    } else {
      this.set('isSupported', false);
    }
  },

  addListeners() {
    this.onChargingChange = () => {
      this.set('isCharging', this._battery.charging);
    };

    this.onLevelChange = () => {
      this.set('level', this._battery.level);
    };

    this.onChargingTimeChange = () => {
      this.set('chargingTime', this._battery.chargingTime);
    };

    this.onDischargingTimeChange = () => {
      this.set('dischargingTime', this._battery.dischargingTime);
    };

    this._battery.addEventListener('chargingchange', this.onChargingChange);
    this._battery.addEventListener('levelchange', this.onLevelChange);
    this._battery.addEventListener('chargingtimechange', this.onChargingTimeChange);
    this._battery.addEventListener('dischargingtimechange', this.onDischargingTimeChange);
  },

  onDischargingTimeChange() {
     this.set('dischargingTime', this._battery.dischargingTime);
  },

  willDestroy() {
    this._super(...arguments);
    this.removeListeners();
  },

  removeListeners() {
    let battery = this.get('_battery');
    if (battery) {
      battery.removeEventListener('chargingchange', this.onChargingChange);
      battery.removeEventListener('levelchange', this.onLevelChange);
      battery.removeEventListener('chargingtimechange', this.onChargingTimeChange);
      battery.removeEventListener('dischargingtimechange', this.onDischargingTimeChange);
    }
  }
});

@@ -1521,109 +1521,8 @@
 %7D,%0A%0A
-  onDischargingTimeChange() %7B%0A     this.set('dischargingTime', this._battery.dischargingTime);%0A  %7D,%0A%0A
   wi
@@ -1649,21 +1649,16 @@
 his.
-get('
 _battery
 ');%0A
@@ -1653,18 +1653,16 @@
 _battery
-')
 ;%0A    if

remove leftover onDischargingTimeChange