'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _index = require('../index.js');

var _index2 = _interopRequireDefault(_index);

var _rotationconstraint = require('../rotationconstraint.js');

var _rotationconstraint2 = _interopRequireDefault(_rotationconstraint);

var _viewhint = require('../viewhint.js');

var _viewhint2 = _interopRequireDefault(_viewhint);

var _condition = require('../events/condition.js');

var _condition2 = _interopRequireDefault(_condition);

var _interaction = require('../interaction/interaction.js');

var _interaction2 = _interopRequireDefault(_interaction);

var _pointer = require('../interaction/pointer.js');

var _pointer2 = _interopRequireDefault(_pointer);

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

/**
 * @classdesc
 * Allows the user to zoom and rotate the map by clicking and dragging
 * on the map.  By default, this interaction is limited to when the shift
 * key is held down.
 *
 * This interaction is only supported for mouse devices.
 *
 * And this interaction is not included in the default interactions.
 *
 * @constructor
 * @extends {ol.interaction.Pointer}
 * @param {olx.interaction.DragRotateAndZoomOptions=} opt_options Options.
 * @api
 */
var _ol_interaction_DragRotateAndZoom_ = function (opt_options) {

  var options = opt_options ? opt_options : {};

  _pointer2.default.call(this, {
    handleDownEvent: _ol_interaction_DragRotateAndZoom_.handleDownEvent_,
    handleDragEvent: _ol_interaction_DragRotateAndZoom_.handleDragEvent_,
    handleUpEvent: _ol_interaction_DragRotateAndZoom_.handleUpEvent_
  });

  /**
   * @private
   * @type {ol.EventsConditionType}
   */
  this.condition_ = options.condition ? options.condition : _condition2.default.shiftKeyOnly;

  /**
   * @private
   * @type {number|undefined}
   */
  this.lastAngle_ = undefined;

  /**
   * @private
   * @type {number|undefined}
   */
  this.lastMagnitude_ = undefined;

  /**
   * @private
   * @type {number}
   */
  this.lastScaleDelta_ = 0;

  /**
   * @private
   * @type {number}
   */
  this.duration_ = options.duration !== undefined ? options.duration : 400;
};

_index2.default.inherits(_ol_interaction_DragRotateAndZoom_, _pointer2.default);

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @this {ol.interaction.DragRotateAndZoom}
 * @private
 */
_ol_interaction_DragRotateAndZoom_.handleDragEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return;
  }

  var map = mapBrowserEvent.map;
  var size = map.getSize();
  var offset = mapBrowserEvent.pixel;
  var deltaX = offset[0] - size[0] / 2;
  var deltaY = size[1] / 2 - offset[1];
  var theta = Math.atan2(deltaY, deltaX);
  var magnitude = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
  var view = map.getView();
  if (view.getConstraints().rotation !== _rotationconstraint2.default.disable && this.lastAngle_ !== undefined) {
    var angleDelta = theta - this.lastAngle_;
    _interaction2.default.rotateWithoutConstraints(view, view.getRotation() - angleDelta);
  }
  this.lastAngle_ = theta;
  if (this.lastMagnitude_ !== undefined) {
    var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude);
    _interaction2.default.zoomWithoutConstraints(view, resolution);
  }
  if (this.lastMagnitude_ !== undefined) {
    this.lastScaleDelta_ = this.lastMagnitude_ / magnitude;
  }
  this.lastMagnitude_ = magnitude;
};

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @return {boolean} Stop drag sequence?
 * @this {ol.interaction.DragRotateAndZoom}
 * @private
 */
_ol_interaction_DragRotateAndZoom_.handleUpEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return true;
  }

  var map = mapBrowserEvent.map;
  var view = map.getView();
  view.setHint(_viewhint2.default.INTERACTING, -1);
  var direction = this.lastScaleDelta_ - 1;
  _interaction2.default.rotate(view, view.getRotation());
  _interaction2.default.zoom(view, view.getResolution(), undefined, this.duration_, direction);
  this.lastScaleDelta_ = 0;
  return false;
};

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @return {boolean} Start drag sequence?
 * @this {ol.interaction.DragRotateAndZoom}
 * @private
 */
_ol_interaction_DragRotateAndZoom_.handleDownEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return false;
  }

  if (this.condition_(mapBrowserEvent)) {
    mapBrowserEvent.map.getView().setHint(_viewhint2.default.INTERACTING, 1);
    this.lastAngle_ = undefined;
    this.lastMagnitude_ = undefined;
    return true;
  } else {
    return false;
  }
};
exports.default = _ol_interaction_DragRotateAndZoom_;
module.exports = exports['default'];