"use strict";

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

var _openDataPainter = _interopRequireDefault(require("./openDataPainter"));

var _setSize = _interopRequireDefault(require("./setSize"));

var _utils = require("./utils");

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

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function drawAvatar(_x, _x2) {
  return _drawAvatar.apply(this, arguments);
}

function _drawAvatar() {
  _drawAvatar = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(shareData, avatar) {
    var canvas, ctx, dataUrl;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (window.WWOpenData && window.WWOpenData.initCanvas) {
              window.WWOpenData.initCanvas();
            }

            canvas = document.createElement("canvas");
            ctx = canvas.getContext("2d");
            (0, _setSize["default"])(canvas, ctx, 72, 72);
            ctx.beginPath();
            _context2.next = 7;
            return draw(ctx, {
              width: 72,
              height: 72,
              x: 0,
              y: 0,
              url: avatar,
              name: shareData.name,
              id: shareData.userId,
              color: shareData.color
            });

          case 7:
            dataUrl = canvas.toDataURL();
            return _context2.abrupt("return", dataUrl);

          case 9:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _drawAvatar.apply(this, arguments);
}

function draw(_x3, _x4) {
  return _draw.apply(this, arguments);
}

function _draw() {
  _draw = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(ctx, params) {
    return regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            ctx.save();
            ctx.beginPath();
            ctx.arc(params.width / 2 + params.x, params.height / 2 + params.y, params.width / 2, 0, Math.PI * 2, false);
            ctx.clip();
            ctx.closePath();

            if (!params.url) {
              _context3.next = 10;
              break;
            }

            _context3.next = 8;
            return drawImageAvatar(ctx, params);

          case 8:
            _context3.next = 12;
            break;

          case 10:
            _context3.next = 12;
            return drawTextAvatar(ctx, params);

          case 12:
            ctx.restore();
            return _context3.abrupt("return", params.y + params.height);

          case 14:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _draw.apply(this, arguments);
}

function drawImageAvatar(ctx, avatar) {
  return (0, _utils.drawImage)(ctx, avatar.url, avatar.x, avatar.y, avatar.width, avatar.height);
}

function drawTextAvatar(ctx, avatar) {
  console.log("text");
  var maxNameWidth = avatar.maxNameWidth || 28;
  return new Promise( /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(resolve) {
      var namePainter, width, height;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              ctx.fillStyle = avatar.color;
              ctx.fill();
              ctx.closePath();
              ctx.fillStyle = "white";
              ctx.font = "14px arial";
              ctx.textBaseline = "bottom";
              _context.next = 8;
              return (0, _openDataPainter["default"])(ctx, {
                id: avatar.id,
                name: avatar.name,
                type: "userName"
              }, 22, maxNameWidth);

            case 8:
              namePainter = _context.sent;
              width = namePainter.width, height = namePainter.height;
              namePainter.draw(ctx, avatar.x + avatar.width / 2 - width / 2, avatar.y + avatar.height / 2 - height / 2);
              ctx.closePath();
              resolve();

            case 13:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function (_x5) {
      return _ref.apply(this, arguments);
    };
  }());
}