"use strict";

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 }; }

var _uuid = 0;
var _messageBuffer = [];
var tools = require("../tools");

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

var Message = function () {
  function Message(payload, hubId, terminalId, channelId) {
    (0, _classCallCheck3.default)(this, Message);

    this._from = {
      hubId: hubId,
      terminalId: terminalId
    };
    this.channelId = channelId;
    this._to = [];
    this.destination = [];
    this.id = _uuid++;
    _messageBuffer.push(this);
    this._payload = [payload];
    this._createTime = new Date();
  }

  (0, _createClass3.default)(Message, [{
    key: "reply",
    value: function reply() {
      this.to(this._from.hubId, this._from.terminalId);
    }
  }, {
    key: "match",
    value: function match(fromString) {
      return !fromString || tools.matchHubId(fromString, this._from.hubId + "." + this._from.terminalId);
    }
  }, {
    key: "to",
    value: function to(hubId, terminalId) {
      this._to.push({
        hubId: hubId,
        terminalId: terminalId
      });
    }
  }, {
    key: "discard",
    value: function discard() {
      this._discard = true;
      this.destroy();
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var _this = this;

      if (this._discard) {
        logger.verbose(this.id + "#[" + this._from.hubId + "@" + this._from.terminalId.split("-")[0] + "-" + (this.channelId ? this.channelId.split("-")[0] : "*") + "] discard", this._payload.length > 1 ? this._payload : this.payload);
      }
      _messageBuffer = _messageBuffer.filter(function (m) {
        return m.id !== _this.id;
      });
    }
  }, {
    key: "isAlive",
    value: function isAlive() {
      return !this._discard;
    }
  }, {
    key: "route",
    value: function route(resolver) {
      var _this2 = this;

      this.routed = true;
      if (this._to.length === 0 && this.channelId) {
        // todo 如果没有明确的to但是有channelId 该当如何
        this._to.push({});
      }
      this.destination = [];
      this._to.forEach(function (to) {
        if (!to.terminalId && resolver) {
          _this2.destination.push.apply(_this2.destination, resolver(_this2._from, to, _this2.channelId));
        } else {
          _this2.destination.push(to);
        }
      });
    }
  }, {
    key: "selectOne",
    value: function selectOne(to) {
      var found = false;
      for (var i = 0; i < this._to.length; i++) {
        if (this._to[i].hubId === to.hubId && this._to[i].terminalId === to.terminalId) {
          this._to.splice(i, 1);
          found = true;
          break;
        }
      }
      if (found) {
        var selected = new Message(this.payload, this._from.hubId, this._from.terminalId, this.channelId);
        selected._to = [to];
        return selected;
      } else {
        throw new Error("message select not found");
      }
    }
  }, {
    key: "payload",
    get: function get() {
      return this._payload[0];
    },
    set: function set(value) {
      this._payload.unshift(value);
    }
  }]);
  return Message;
}();

module.exports = Message;