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

var classUtil = require("../utils/class_util");

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

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

var Linkable = require("./link/Linkable");

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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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';
    classUtil.inheritProperties(_assertThisInitialized(_this), Linkable, _this.options);
    classUtil.copyOwnProperties(_assertThisInitialized(_this), _this.options, ['style', 'shape']);
    return _this;
  }
  /**
   * @method render
   */


  _createClass(QImage, [{
    key: "render",
    value: function render() {
      var ctx = this.ctx;
      var prevEl = this.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 && !height) {
        width = image.width;
        height = image.height;
      }

      this.applyTransform(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);
      }

      Element.prototype.render.call(this, ctx, prevEl);
    }
    /**
     * @method getBoundingRect
     */

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

      if (!style.x) {
        style.x = 0;
      }

      if (!style.y) {
        style.y = 0;
      }

      if (!style.width) {
        style.width = 0;
      }

      if (!style.height) {
        style.height = 0;
      }

      if (!this.__boundingRect) {
        this.__boundingRect = new BoundingRect(style.x, style.y, style.width - style.x, style.height - style.y, style.width, style.height);
      }

      return this.__boundingRect;
    }
  }, {
    key: "toJSONObject",
    value: function toJSONObject() {
      var result = Element.prototype.toJSONObject.call(this);
      result.linkable = this.linkable;
      return result;
    }
  }]);

  return QImage;
}(Element);

classUtil.mixin(QImage, Linkable);
var _default = QImage;
module.exports = _default;