var eventUtil = require("../core/utils/event_util");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

/**
 * @class qrenderer.event.GestureMgr
 * 
 * Implement necessary gestures for mobile platform.
 * 
 * @docauthor 大漠穷秋 <damoqiongqiu@126.com>
 */
function dist(pointPair) {
  var dx = pointPair[1][0] - pointPair[0][0];
  var dy = pointPair[1][1] - pointPair[0][1];
  return Math.sqrt(dx * dx + dy * dy);
}

function center(pointPair) {
  return [(pointPair[0][0] + pointPair[1][0]) / 2, (pointPair[0][1] + pointPair[1][1]) / 2];
}

var recognizers = {
  pinch: function pinch(track, event) {
    var trackLen = track.length;

    if (!trackLen) {
      return;
    }

    var pinchEnd = (track[trackLen - 1] || {}).points;
    var pinchPre = (track[trackLen - 2] || {}).points || pinchEnd;

    if (pinchPre && pinchPre.length > 1 && pinchEnd && pinchEnd.length > 1) {
      var pinchScale = dist(pinchEnd) / dist(pinchPre);
      !isFinite(pinchScale) && (pinchScale = 1);
      event.pinchScale = pinchScale;
      var pinchCenter = center(pinchEnd);
      event.pinchX = pinchCenter[0];
      event.pinchY = pinchCenter[1];
      return {
        type: 'pinch',
        target: track[0].target,
        event: event
      };
    }
  } // Only pinch currently.

};

var GestureMgr =
/*#__PURE__*/
function () {
  function GestureMgr() {
    _classCallCheck(this, GestureMgr);

    /**
     * @private
     * @property {Array<Object>}
     */
    this._track = [];
  }

  _createClass(GestureMgr, [{
    key: "recognize",
    value: function recognize(event, target, root) {
      this._doTrack(event, target, root);

      return this._recognize(event);
    }
  }, {
    key: "clear",
    value: function clear() {
      this._track.length = 0;
      return this;
    }
  }, {
    key: "_doTrack",
    value: function _doTrack(event, target, root) {
      var touches = event.touches;

      if (!touches) {
        return;
      }

      var trackItem = {
        points: [],
        touches: [],
        target: target,
        event: event
      };

      for (var i = 0, len = touches.length; i < len; i++) {
        var touch = touches[i];
        var pos = eventUtil.clientToLocal(root, touch, {});
        trackItem.points.push([pos.qrX, pos.qrY]);
        trackItem.touches.push(touch);
      }

      this._track.push(trackItem);
    }
  }, {
    key: "_recognize",
    value: function _recognize(event) {
      for (var eventName in recognizers) {
        if (recognizers.hasOwnProperty(eventName)) {
          var gestureInfo = recognizers[eventName](this._track, event);

          if (gestureInfo) {
            return gestureInfo;
          }
        }
      }
    }
  }]);

  return GestureMgr;
}();

var _default = GestureMgr;
module.exports = _default;