"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _config = _interopRequireDefault(require("./config"));
var _Canvas2 = _interopRequireDefault(require("./Canvas"));
var _getIconConfig = require("../_util/getIconConfig");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /* eslint-disable */
var _default = exports["default"] = /*#__PURE__*/function (_Canvas) {
  _inherits(_default, _Canvas);
  var _super = _createSuper(_default);
  function _default(options) {
    var _this;
    _classCallCheck(this, _default);
    _this = _super.call(this);
    var hideFooter = options.hideFooter;
    _this.options = options || {};
    _this.options.color = (0, _getIconConfig.getColor)(_this.options);
    _this.settings = {
      height: hideFooter ? 55 : 118,
      // 整体高度
      width: 55,
      // 整体高度
      iconOuterWrapSize: 55,
      // 外侧圆大小
      iconInnerWrapSize: 43,
      // 内侧圆大小
      pillar: {
        // 中间的柱子
        width: 5,
        // 宽度
        height: 58 // 高度
      },

      info: {
        // 信息框
        height: 55,
        // 高度
        gutter: 32 // 两侧间距
      },

      footerOuterCircle: {
        // 底部外侧圆
        width: 51,
        // 宽度
        height: 18 // 高度
      },

      footerInnerCircle: {
        // 底部外侧圆
        width: 26,
        // 宽度
        height: 10 // 高度
      }
    };

    // 初始化框高
    _this.setWidth(_this.settings.width);
    _this.setHeight(_this.settings.height);

    // 创建图标
    _this.createIcon();
    return _this;
  }

  // 创建图标
  _createClass(_default, [{
    key: "createIcon",
    value: function createIcon() {
      var _this$options = this.options,
        info = _this$options.info,
        icon = _this$options.icon,
        text = _this$options.text,
        canvasIcon = _this$options.canvasIcon,
        hideFooter = _this$options.hideFooter;
      if (!hideFooter) {
        // 绘制底座外侧椭圆
        this.drawFooterOuterCircle();

        // 绘制底座内侧椭圆
        this.drawFooterInnerCircle();

        // 绘制柱子
        this.drawPillar();
      }

      // 绘制图标外侧
      this.drawIconOuterWrap();

      // 绘制图标内侧侧
      this.drawIconInnerWrap();

      // 绘制信息框
      if (info) {
        this.drawInfo();
      }

      // 绘制图标
      if (icon) {
        this.drawIcon();
      }

      // 绘制图标
      if (canvasIcon) {
        this.drawCanvasIcon();
      }

      // 绘制文本
      if (text) {
        this.drawCanvasText();
      }
    }

    // 绘制文本
  }, {
    key: "drawCanvasText",
    value: function drawCanvasText() {
      var _this$options2 = this.options,
        text = _this$options2.text,
        color = _this$options2.color,
        _this$options2$fontSi = _this$options2.fontSize,
        fontSize = _this$options2$fontSi === void 0 ? 14 : _this$options2$fontSi;
      var ctx = this.ctx;
      var _ctx$measureText = ctx.measureText(text),
        width = _ctx$measureText.width,
        actualBoundingBoxAscent = _ctx$measureText.actualBoundingBoxAscent,
        actualBoundingBoxDescent = _ctx$measureText.actualBoundingBoxDescent;
      var actualHeight = actualBoundingBoxAscent + actualBoundingBoxDescent;
      var x = (this.settings.width - width * 1.5) / 2;
      var y = (this.settings.iconOuterWrapSize + actualHeight) / 2;
      ctx.font = fontSize + 'px Arial';
      ctx.fillStyle = color || '#FFF';
      ctx.fillText(text, x, y);
    }

    // 绘制图标外侧
  }, {
    key: "drawIconOuterWrap",
    value: function drawIconOuterWrap() {
      var color = this.options.color;
      var iconOuterWrapSize = this.settings.iconOuterWrapSize;

      // 根据对角线计算正方形边长, 计算公式：正方形边长 = 对角线 / 1.414
      var sideLength = iconOuterWrapSize / 1.414;

      // 保存状态，为下面偏移中心点、旋转做恢复
      this.ctx.save();
      this.ctx.beginPath();

      // 偏移中心点
      this.ctx.translate(sideLength / 2 - 11, sideLength / 2);

      // 旋转
      this.ctx.rotate(45 * Math.PI / 180);

      // 绘制背景
      this.roundRectInnerShadow(this.ctx, 0, -(iconOuterWrapSize / 2), sideLength, sideLength, 2, color);

      // 绘制边框
      this.roundRect(this.ctx, 0, -(iconOuterWrapSize / 2), sideLength, sideLength, 2, color);
      this.ctx.strokeStyle = color;
      this.ctx.stroke();
      this.ctx.restore();
    }

    // 绘制图标内侧侧
  }, {
    key: "drawIconInnerWrap",
    value: function drawIconInnerWrap() {
      var color = this.options.color;
      var iconOuterWrapSize = this.settings.iconOuterWrapSize;
      var iconInnerWrapSize = this.settings.iconInnerWrapSize;

      // 绘制图标轮廓
      var wrapOuterSize = iconOuterWrapSize / 1.414;

      // 保存状态，为下面偏移中心点、旋转做恢复
      this.ctx.save();

      // 偏移中心点
      this.ctx.translate(wrapOuterSize / 2 - 11, wrapOuterSize / 2);

      // 旋转
      this.ctx.rotate(45 * Math.PI / 180);

      // 根据对角线计算正方形边长, 计算公式：正方形边长 = 对角线 / 1.414
      var sideLength = iconInnerWrapSize / 1.414;

      // 绘制
      this.roundRect(this.ctx, 4, -(iconInnerWrapSize / 2) - 1.8, sideLength, sideLength, 2, color);

      // 填充背景色
      this.ctx.fillStyle = '#FFF';
      this.ctx.fill();
      this.ctx.restore();
    }

    // 绘制柱子
  }, {
    key: "drawPillar",
    value: function drawPillar() {
      var ctx = this.ctx;
      var color = this.options.color;
      var x = this.settings.iconOuterWrapSize / 2 - this.settings.pillar.width / 2;
      var y = this.settings.iconOuterWrapSize - 8;
      var w = this.settings.pillar.width;
      var h = this.settings.pillar.height;
      ctx.save();
      ctx.beginPath();

      // 裁剪区(只保留内部阴影部分)
      this.roundPillarArrow(ctx, x, y, w, h);
      ctx.clip();

      // 边框+阴影
      ctx.beginPath();
      var grd1 = ctx.createLinearGradient(x, y, x + w, y + h);
      grd1.addColorStop(0.1, 'rgba(0, 0, 0, 0)');
      grd1.addColorStop(0.15, this.transformRgb(color, 0.5));
      grd1.addColorStop(0.45, color);
      grd1.addColorStop(0.7, this.transformRgb(color, 0.5));
      grd1.addColorStop(1, 'rgba(0, 0, 0, 0)');
      ctx.fillStyle = grd1;

      // 因线是由坐标位置向两则画的，所以要移动起点坐标位置，和加大矩形。
      this.roundPillarArrow(ctx, x, y, w, h);
      ctx.fillRect(x, y, x + w, y + h);
      ctx.restore();
    }

    // 绘制底座外侧椭圆
  }, {
    key: "drawFooterOuterCircle",
    value: function drawFooterOuterCircle() {
      var x = this.settings.iconOuterWrapSize / 2;
      var y = this.settings.height - this.settings.footerOuterCircle.height / 2 - 2;
      var w = this.settings.footerOuterCircle.width;
      var h = this.settings.footerOuterCircle.height;
      var color = this.options.color;
      this.ellipseShadow(this.ctx, x, y, w, h, color);
    }

    // 绘制底座内侧椭圆
  }, {
    key: "drawFooterInnerCircle",
    value: function drawFooterInnerCircle() {
      var x = this.settings.iconOuterWrapSize / 2;
      var y = this.settings.height - 12;
      var w = this.settings.footerInnerCircle.width;
      var h = this.settings.footerInnerCircle.height;
      var color = this.options.color;
      this.ellipseShadow(this.ctx, x, y, w, h, color);
    }

    // 绘制信息框
  }, {
    key: "drawInfo",
    value: function drawInfo() {
      var _this$options3 = this.options,
        info = _this$options3.info,
        color = _this$options3.color;
      var x = this.settings.iconOuterWrapSize / 2 + 4;
      var y = 0;
      var width = this.getCanvasWidth(info) + this.settings.info.gutter * 2;
      var height = this.settings.info.height;
      var radius = 30;

      // 改变 canvas 大小
      this.resize(x + width);

      // 绘制阴影
      this.roundRectInnerArrowShadow(this.ctx, x, y, width, height, radius, color);

      // 绘制文本
      var textHeight = this.settings.info.height;
      var textX = this.settings.iconOuterWrapSize + 2;
      this.renderText(this.ctx, textHeight, info, textX);
    }
  }, {
    key: "drawIcon",
    value: function drawIcon() {
      var _this2 = this;
      var _this$options4 = this.options,
        icon = _this$options4.icon,
        color = _this$options4.color;
      var src = icon.src,
        _icon$width = icon.width,
        width = _icon$width === void 0 ? 22 : _icon$width,
        _icon$height = icon.height,
        height = _icon$height === void 0 ? 22 : _icon$height;
      var x = this.settings.iconOuterWrapSize / 2 - width / 2;
      var y = this.settings.iconOuterWrapSize / 2 - height / 2;
      this.drawImage(src, color, function (imgSrc) {
        var image = new Image();
        image.src = imgSrc;
        image.onload = function () {
          _this2.ctx.drawImage(image, x, y, width, height);
        };
      });
    }

    // 绘制canvas图标
  }, {
    key: "drawCanvasIcon",
    value: function drawCanvasIcon() {
      var _ref = (0, _getIconConfig.getIcon)(this.options) || {},
        size = _ref.size,
        draw = _ref.draw;
      var color = this.options.color;

      // 起始位置
      var x = this.settings.iconOuterWrapSize / 2 - size / 2;
      var y = this.settings.iconOuterWrapSize / 2 - size / 2;

      // 保存状态
      this.ctx.save();

      // 偏移中心点
      this.ctx.translate(x, y);

      // 绘制颜色
      this.ctx.fillStyle = color;

      // 绘制图标
      draw(this.ctx);

      // 恢复状态
      this.ctx.restore();
    }
  }]);
  return _default;
}(_Canvas2["default"]);
//# sourceMappingURL=CockpitIcon.js.map
