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

var textUtil = require("../utils/text_util");

var _constants = require("../utils/constants");

var ContextCachedBy = _constants.ContextCachedBy;

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.Text
 * 
 * @docauthor 大漠穷秋 <damoqiongqiu@126.com>
 */
var Text =
/*#__PURE__*/
function (_Element) {
  _inherits(Text, _Element);

  /**
   * @method constructor Text
   * @param {Object} opts 
   */
  function Text(opts) {
    var _this;

    _classCallCheck(this, Text);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Text).call(this, opts));
    /**
     * @property {String} type
     */

    _this.type = 'text';
    return _this;
  }
  /**
   * @method render
   */


  _createClass(Text, [{
    key: "render",
    value: function render() {
      var ctx = this.ctx;
      var prevEl = this.prevEl;
      var style = this.style; // Optimize, avoid normalize every time.

      this.__dirty && textUtil.normalizeTextStyle(style, true); // Use props with prefix 'text'.

      style.fill = null;
      style.stroke = null;
      style.shadowBlur = null;
      style.shadowColor = null;
      style.shadowOffsetX = null;
      style.shadowOffsetY = null;
      var text = style.text; // Convert to string

      text != null && (text += ''); // Do not apply style.bind in Text node. Because the real bind job
      // is in textUtil.renderText, and performance of text render should
      // be considered.
      // style.bind(ctx, this, prevEl);

      if (!textUtil.needDrawText(text, style)) {
        // The current el.style is not applied
        // and should not be used as cache.
        ctx.__attrCachedBy = ContextCachedBy.NONE;
        return;
      }

      this.applyTransform(ctx);
      textUtil.renderText(this, ctx, text, style, null, prevEl);
      this.restoreTransform(ctx);
    }
    /**
     * @method getBoundingRect
     * Get bounding rect of this element, NOTE: 
     * this method will return the bounding rect without transforming(translate/scale/rotate/skew). 
     * However, direct modifications to the shape property will be reflected in the bouding-rect.
     * For example,  if we modify this.shape.width directly, then the new width property will be calculated.
     * 
     * 
     * 获取当前元素的边界矩形,注意:
     * 此方法返回的是没有经过 transform(translate/scale/rotate/skew) 处理的边界矩形,但是对 shape 属性直接进行的修改会反映在获取的边界矩形上。
     * 例如,用代码直接对 this.shape.width 进行赋值,那么在计算边界矩形时就会用新的 width 属性进行计算。
     */

  }, {
    key: "getBoundingRect",
    value: function getBoundingRect() {
      var style = this.style; // Optimize, avoid normalize every time.

      this.__dirty && textUtil.normalizeTextStyle(style, true);

      if (!this.__boundingRect) {
        var text = style.text;
        text != null ? text += '' : text = '';
        var rect = textUtil.getBoundingRect(style.text + '', style.font, style.textAlign, style.textVerticalAlign, style.textPadding, style.textLineHeight, style.rich);
        rect.x1 += style.x || 0;
        rect.y1 += style.y || 0;

        if (textUtil.getStroke(style.textStroke, style.textStrokeWidth)) {
          var w = style.textStrokeWidth;
          rect.x1 -= w / 2;
          rect.y1 -= w / 2;
          rect.width += w;
          rect.height += w;
          rect.x2 = rect.x1 + rect.width;
          rect.y2 = rect.y1 + rect.height;
        }

        this.__boundingRect = rect;
      }

      return this.__boundingRect;
    }
  }]);

  return Text;
}(Element);

module.exports = Text;