var Element = require("./Element");

var BoundingRect = require("./transform/BoundingRect");

var dataUtil = require("../core/utils/data_structure_util");

var imageHelper = require("./utils/image");

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

/**
 * @class qrenderer.graphic.QImage 
 * @docauthor 大漠穷秋 <damoqiongqiu@126.com>
 */
var QImage =
/*#__PURE__*/
function (_Element) {
  _inherits(QImage, _Element);

  /**
   * @method constructor QImage
   * @param {Object} options
   */
  function QImage(options) {
    var _this;

    _classCallCheck(this, QImage);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(QImage).call(this, options));
    /**
     * @property {String}
     */

    _this.type = 'image';
    return _this;
  }
  /**
   * @method brush
   * @param {Object} ctx 
   * @param {Element} prevEl 
   */


  _createClass(QImage, [{
    key: "brush",
    value: function brush(ctx, prevEl) {
      var style = this.style;
      var src = style.image; // Must bind each time

      style.bind(ctx, this, prevEl);
      var image = this._image = imageHelper.createOrUpdateImage(src, this._image, this, this.onload);

      if (!image || !imageHelper.isImageReady(image)) {
        return;
      }

      var x = style.x || 0;
      var y = style.y || 0;
      var width = style.width;
      var height = style.height;
      var aspect = image.width / image.height;

      if (width == null && height != null) {
        // Keep image/height ratio
        width = height * aspect;
      } else if (height == null && width != null) {
        height = width / aspect;
      } else if (width == null && height == null) {
        width = image.width;
        height = image.height;
      } // 设置transform


      this.setTransform(ctx);

      if (style.sWidth && style.sHeight) {
        var sx = style.sx || 0;
        var sy = style.sy || 0;
        ctx.drawImage(image, sx, sy, style.sWidth, style.sHeight, x, y, width, height);
      } else if (style.sx && style.sy) {
        var _sx = style.sx;
        var _sy = style.sy;
        var sWidth = width - _sx;
        var sHeight = height - _sy;
        ctx.drawImage(image, _sx, _sy, sWidth, sHeight, x, y, width, height);
      } else {
        ctx.drawImage(image, x, y, width, height);
      } // Draw rect text


      if (style.text != null) {
        // Only restore transform when needs draw text.
        this.restoreTransform(ctx);
        this.drawRectText(ctx, this.getBoundingRect());
      }
    }
    /**
     * @method getBoundingRect
     */

  }, {
    key: "getBoundingRect",
    value: function getBoundingRect() {
      var style = this.style;

      if (!this._rect) {
        this._rect = new BoundingRect(style.x || 0, style.y || 0, style.width || 0, style.height || 0);
      }

      return this._rect;
    }
  }]);

  return QImage;
}(Element);

module.exports = QImage;