"use strict";

var _stringify = require("babel-runtime/core-js/json/stringify");

var _stringify2 = _interopRequireDefault(_stringify);

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 EventEmitter = require("events").EventEmitter;
var uuid = require("uuid");
var Promise = require("ipromise");

var _require = require("../../util"),
    logger = _require.logger;

var SyncTerminal = function (_EventEmitter) {
  (0, _inherits3.default)(SyncTerminal, _EventEmitter);

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

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

    _this.id = uuid();
    _this.promise = new Promise();
    return _this;
  }

  (0, _createClass3.default)(SyncTerminal, [{
    key: "send",
    value: function send(data) {
      this.emit("message", data);
      this.syncId = data.params.syncId;
      return this.promise;
    }
  }, {
    key: "read",
    value: function read(message) {
      if (Array.isArray(message) && !message[0]) {
        message = [{}];
      }
      if (message.id && this.syncId === message.id) {
        this.promise.resolve(message);
      } else if (!message.id) {
        // while android sdk has not support, should return promise while getting message
        this.promise.resolve(message);
      }
      // never destory
      // cause the terminal will be destory before the socket send message
      // this.emit('destroy')
    }
  }]);
  return SyncTerminal;
}(EventEmitter);

var WebsocketTerminal = function (_EventEmitter2) {
  (0, _inherits3.default)(WebsocketTerminal, _EventEmitter2);

  function WebsocketTerminal(websocket, channelId) {
    (0, _classCallCheck3.default)(this, WebsocketTerminal);

    var _this2 = (0, _possibleConstructorReturn3.default)(this, (WebsocketTerminal.__proto__ || (0, _getPrototypeOf2.default)(WebsocketTerminal)).call(this));

    _this2.channelId = channelId;
    _this2.id = uuid();
    _this2.websocket = websocket;
    websocket.on("connect", function () {});
    websocket.on("message", function (message) {
      _this2.emit("message", JSON.parse(message));
    });
    websocket.on("close", function () {
      _this2.emit("destroy");
    });
    websocket.on("error", function (err) {
      logger.error(err);
    });
    return _this2;
  }

  (0, _createClass3.default)(WebsocketTerminal, [{
    key: "read",
    value: function read(message) {
      if (this.websocket.readyState === 1) {
        this.emit("read", message);
        this.websocket.send((0, _stringify2.default)(message));
      }
    }
  }]);
  return WebsocketTerminal;
}(EventEmitter);

module.exports = { WebsocketTerminal: WebsocketTerminal, SyncTerminal: SyncTerminal };