'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _pathExtra = require('path-extra');

var _worker = require('views/services/worker');

var _reactFontawesome = require('react-fontawesome');

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

require('./assets/avatar.css');

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

const { APPDATA_PATH, getStore } = window;

const avatarCachePath = (0, _pathExtra.join)(APPDATA_PATH, 'avatar', 'cache');

function _ref(mstId) {
  _worker.avatarWorker.postMessage(['Request', mstId, getStore(`const.$graphs.${mstId}.api_version`), getStore(`const.$graphs.${mstId}.api_filename`), getStore('info.server.ip')]);
}

class Avatar extends _react.PureComponent {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      available: false,
      failed: false
    }, this.onMessage = e => {
      if (e.data[1] === this.props.mstId) {
        if (e.data[0] === 'Ready') {
          this.setState({ available: true });
        } else if (e.data[0] === 'Failed') {
          this.setState({ failed: true });
        }
      }
    }, this.sendMessage = _ref, this.componentDidMount = () => {
      _worker.avatarWorker.addEventListener('message', this.onMessage);
      this.sendMessage(this.props.mstId);
    }, this.componentWillUnmount = () => {
      _worker.avatarWorker.removeEventListener('message', this.onMessage);
    }, this.componentWillReceiveProps = nextProps => {
      if (this.props.mstId !== nextProps.mstId) {
        this.setState({ available: false });
        this.sendMessage(nextProps.mstId);
      }
    }, _temp;
  }

  render() {
    return _react2.default.createElement(
      'div',
      { className: 'ship-avatar-container', style: {
          width: Math.round(1.85 * this.props.height),
          height: this.props.height
        } },
      this.state.failed ? _react2.default.createElement(
        'div',
        { className: 'ship-avatar-loading' },
        _react2.default.createElement(_reactFontawesome2.default, { name: 'times' })
      ) : this.state.available ? _react2.default.createElement('img', {
        className: 'ship-avatar',
        style: { height: this.props.height, marginLeft: -Math.round(this.props.marginMagic * this.props.height) },
        src: (0, _pathExtra.join)(avatarCachePath, `${this.props.mstId}_${this.props.isDamaged ? 'd' : 'n'}.png`) }) : _react2.default.createElement(
        'div',
        { className: 'ship-avatar-loading' },
        _react2.default.createElement(_reactFontawesome2.default, { name: 'spinner', pulse: true })
      )
    );
  }
}
exports.Avatar = Avatar;
Avatar.propTypes = {
  mstId: _propTypes2.default.number.isRequired,
  height: _propTypes2.default.number,
  marginMagic: _propTypes2.default.number,
  isDamaged: _propTypes2.default.bool
};
Avatar.defaultProps = {
  height: 121,
  marginMagic: 0.555,
  isDamaged: false
};