'use strict';

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

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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

var _createClass3 = _interopRequireDefault(_createClass2);

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

/**
 * Created by godsong on 16/6/26.
 */
var P2PSession = require('./P2PSession');
var Emitter = require('events').EventEmitter;

var Device = function () {
    function Device(deviceInfo, websocket) {
        (0, _classCallCheck3.default)(this, Device);

        this.deviceId = deviceInfo.deviceId;

        this.inspectorSession = P2PSession.newSession(websocket);
        this.debuggerSession = P2PSession.newSession(websocket);

        this.deviceInfo = Object.assign(deviceInfo, {
            inspectorSessionId: this.inspectorSession.id,
            debuggerSessionId: this.debuggerSession.id
        });
        websocket._deviceId = this.deviceId;
    }

    (0, _createClass3.default)(Device, [{
        key: 'destroy',
        value: function destroy() {
            this.destroyed = true;
            this.inspectorSession.destroy();
            this.debuggerSession.destroy();
            this.inspectorSession = null;
            this.debuggerSession = null;
        }
    }, {
        key: 'reconnect',
        value: function reconnect(websocket) {
            this.inspectorSession.join(websocket);
            this.debuggerSession.join(websocket);
            websocket._deviceId = this.deviceId;
        }
    }]);
    return Device;
}();

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

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

        var _this = (0, _possibleConstructorReturn3.default)(this, Object.getPrototypeOf(DeviceManager).call(this));

        _this.deviceList = [];
        return _this;
    }

    (0, _createClass3.default)(DeviceManager, [{
        key: 'registerDevice',
        value: function registerDevice(deviceInfo, websocket) {
            var _this2 = this;

            var existDevice = this.deviceList.filter(function (dvc) {
                return dvc.deviceId == deviceInfo.deviceId;
            })[0];
            if (existDevice) {
                existDevice.reconnect(websocket);
                return existDevice;
            } else {
                (function () {
                    var device = new Device(deviceInfo, websocket);
                    var idx = _this2.deviceList.length;
                    _this2.deviceList.push(device);
                    _this2.emit('update', _this2.getDeviceList());
                    device.inspectorSession.on('timeout', function () {
                        console.log('timeout1', device.debuggerSession.isTimeout);

                        if (device.debuggerSession.isTimeout) {
                            if (!device.destroyed) {
                                device.destroy();
                                _this2.deviceList.splice(idx, 1);
                                _this2.emit('update', _this2.getDeviceList());
                            }
                        }
                    });
                    device.debuggerSession.on('timeout', function () {
                        console.log('timeout2', device.inspectorSession.isTimeout);
                        if (device.inspectorSession.isTimeout) {
                            if (!device.destroyed) {
                                device.destroy();
                                _this2.deviceList.splice(idx, 1);
                                _this2.emit('update', _this2.getDeviceList());
                            }
                        }
                    });
                })();
            }
        }
    }, {
        key: 'getDevice',
        value: function getDevice(websocket) {
            return this.deviceList.filter(function (dvc) {
                return dvc.deviceId === websocket._deviceId;
            })[0];
        }
    }, {
        key: 'getDeviceList',
        value: function getDeviceList() {
            return this.deviceList.map(function (device) {
                return device.deviceInfo;
            });
        }
    }]);
    return DeviceManager;
}(Emitter);

module.exports = new DeviceManager();