"use strict";

var _keys = require("babel-runtime/core-js/object/keys");

var _keys2 = _interopRequireDefault(_keys);

var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of");

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require("babel-runtime/helpers/createClass");

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require("babel-runtime/helpers/inherits");

var _inherits3 = _interopRequireDefault(_inherits2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Emitter = require("events").EventEmitter;

var DeviceManager = function (_Emitter) {
  (0, _inherits3.default)(DeviceManager, _Emitter);

  function DeviceManager() {
    (0, _classCallCheck3.default)(this, DeviceManager);

    var _this = (0, _possibleConstructorReturn3.default)(this, (DeviceManager.__proto__ || (0, _getPrototypeOf2.default)(DeviceManager)).call(this));

    _this.deviceMap = {};
    return _this;
  }

  (0, _createClass3.default)(DeviceManager, [{
    key: "registerDevice",
    value: function registerDevice(device, channelId) {
      var existed = this.deviceMap[channelId];
      if (existed) {
        for (var i = existed.length - 1; i >= 0; i--) {
          if (existed[i].deviceId === device.deviceId) {
            clearTimeout(existed.timer);
            // this.deviceMap[channelId].splice(i,1,device)
            return device;
          }
        }
        this.deviceMap[channelId].unshift(device);
        return device;
      } else {
        device.channelId = channelId;
        this.deviceMap[channelId] = [device];
        return device;
      }
    }
  }, {
    key: "removeDevice",
    value: function removeDevice(channelId, callback) {
      var _this2 = this;

      var device = this.deviceMap[channelId];
      if (device && device.length > 0) {
        clearTimeout(device.timer);
        device.timer = setTimeout(function () {
          _this2.deviceMap[channelId].pop();
          if (_this2.deviceMap[channelId] && _this2.deviceMap[channelId].length === 0) {
            callback();
          }
        }, 5000);
      }
      return device;
    }
  }, {
    key: "getDevice",
    value: function getDevice(channelId) {
      var device = this.deviceMap[channelId];
      return device && device[device.length - 1];
    }
  }, {
    key: "getDeviceList",
    value: function getDeviceList() {
      var _this3 = this;

      return (0, _keys2.default)(this.deviceMap).map(function (key) {
        return _this3.deviceMap[key];
      });
    }
  }]);
  return DeviceManager;
}(Emitter);

module.exports = new DeviceManager();