import _objectSpread from "@babel/runtime/helpers/objectSpread";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import ReactPlayer from 'react-player';
import Card from '../CardWindow';
import CardFloatingBar from '../CardFloatingBar';
var defaultPlayerState = {
  // https://github.com/CookPete/react-player#props
  width: 'initial',
  style: {
    maxWidth: 500
  },
  soundcloudConfig: {
    clientId: 'iFAPeKCVCOTKeA0nNJCHuVHif2gEBKbl',
    showArtwork: true
  }
};

var MediaCard =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(MediaCard, _PureComponent);

  function MediaCard() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, MediaCard);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(MediaCard)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      playerState: {}
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleMedia", function (event) {
      var value = event.data.value;

      if (value) {
        // feeles.openMedia()
        _this.setState({
          playerState: value
        });

        _this.props.setCardVisibility('MediaCard', true);
      } else {
        // feeles.closeMedia()
        _this.props.setCardVisibility('MediaCard', false);
      }
    });

    return _this;
  }

  _createClass(MediaCard, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var globalEvent = this.props.globalEvent;
      globalEvent.on('message.media', this.handleMedia);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var visible = prevProps.cardPropsBag.visible;

      if (visible && !this.props.cardPropsBag.visible) {
        // カードが close されたとき動画を止める
        this.setState({
          playerState: _objectSpread({}, this.state.playerState, {
            playing: false
          })
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var playerState = _objectSpread({}, defaultPlayerState, this.state.playerState);

      return React.createElement(Card, this.props.cardPropsBag, React.createElement(CardFloatingBar, null, this.props.localization.mediaCard.title), this.state.playerState.url ? React.createElement(ReactPlayer, playerState) : React.createElement("div", null, "URL not given"));
    }
  }]);

  return MediaCard;
}(PureComponent);

_defineProperty(MediaCard, "propTypes", {
  cardPropsBag: PropTypes.object.isRequired,
  setCardVisibility: PropTypes.func.isRequired,
  localization: PropTypes.object.isRequired,
  globalEvent: PropTypes.object.isRequired
});

export { MediaCard as default };