'use strict';

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

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global $ */


var AudioBox = function (_Component) {
  _inherits(AudioBox, _Component);

  function AudioBox(props, context) {
    _classCallCheck(this, AudioBox);

    var _this = _possibleConstructorReturn(this, (AudioBox.__proto__ || Object.getPrototypeOf(AudioBox)).call(this, props, context));

    _this.state = {
      playing: false,
      currentTime: 0,
      duration: 0
    };
    _this.togglePlay = _this.togglePlay.bind(_this);
    _this.handleChange = _this.handleChange.bind(_this);
    return _this;
  }

  _createClass(AudioBox, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      var audio = this.audioEl;

      audio.addEventListener('error', function (e) {
        _this2.props.onError && _this2.props.onError(e);
      });

      // When audio play starts
      audio.addEventListener('play', function (e) {
        _this2.props.onPlay && _this2.props.onPlay(e);
      });

      // When unloading the audio player (switching to another src)
      audio.addEventListener('abort', function (e) {
        _this2.props.onAbort && _this2.props.onAbort(e);
      });

      // When the file has finished playing to the end
      audio.addEventListener('ended', function (e) {
        _this2.props.onEnded && _this2.props.onEnded(e);
      });

      // When the user pauses playback
      audio.addEventListener('pause', function (e) {
        _this2.props.onPause && _this2.props.onPause(e);
      });

      audio.addEventListener('loadedmetadata', function () {
        _this2.setState({
          duration: audio.duration
        });
      });

      this.setState({
        duration: audio.duration
      });

      // When the audio time update
      audio.addEventListener('timeupdate', function () {
        var pos = audio.currentTime / audio.duration * 100;

        $(_this2.audioProgress).width(pos + '%');

        _this2.setState({
          currentTime: audio.currentTime
        });
      });
    }
  }, {
    key: 'togglePlay',
    value: function togglePlay() {
      if (this.state.playing) {
        this.audioEl.pause();
        $(this.audioBtn).removeClass('play');
        $(this.audioBtn).addClass('pause');
        this.setState({
          playing: false
        });
      } else {
        this.audioEl.play();
        $(this.audioBtn).removeClass('pause');
        $(this.audioBtn).addClass('play');
        this.setState({
          playing: true
        });
      }
    }
  }, {
    key: 'handleChange',
    value: function handleChange(e) {
      var audio = this.audioEl;
      audio.currentTime = e.target.value;
      this.setState({
        currentTime: e.target.value
      });
    }
  }, {
    key: 'toMMSS',
    value: function toMMSS(seconds) {
      seconds = parseInt(seconds, 10);
      var minutes = parseInt(seconds / 60) % 60;
      seconds = seconds % 60;

      return (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      var _props = this.props;
      var id = _props.id;
      var title = _props.title;
      var url = _props.url;
      var cover = _props.cover;

      var coverStyle = {};

      if (cover) {
        coverStyle = { background: 'url(' + cover + ') no-repeat center center', backgroundSize: 'contain' };
      }

      return _react2.default.createElement(
        'div',
        { className: 'audio-box' },
        _react2.default.createElement(
          'div',
          { className: 'audio-container ' },
          _react2.default.createElement(
            'div',
            { className: 'audio-time' },
            _react2.default.createElement(
              'span',
              { className: 'left' },
              this.toMMSS(this.state.currentTime)
            ),
            ' / ',
            this.toMMSS(this.state.duration)
          ),
          _react2.default.createElement(
            'div',
            { className: 'audio-progress', ref: function ref(_ref2) {
                _this3.seekbar = _ref2;
              } },
            _react2.default.createElement('input', { type: 'range', value: this.state.currentTime, min: '0', max: this.state.duration, onChange: this.handleChange, step: '1', style: { width: '100%' } }),
            _react2.default.createElement(
              'div',
              { className: 'bar', ref: function ref(_ref) {
                  _this3.audioProgress = _ref;
                } },
              _react2.default.createElement('div', { className: 'circle' })
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 'audio-cover', style: coverStyle },
            _react2.default.createElement('div', { className: 'audio-btn pause', ref: function ref(_ref3) {
                _this3.audioBtn = _ref3;
              }, onClick: function onClick() {
                return _this3.togglePlay(id);
              } })
          ),
          _react2.default.createElement(
            'div',
            { className: 'audio-title' },
            _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: title } })
          ),
          _react2.default.createElement('div', { className: 'audio-desc' }),
          _react2.default.createElement('audio', { ref: function ref(_ref4) {
              _this3.audioEl = _ref4;
            }, src: url })
        )
      );
    }
  }]);

  return AudioBox;
}(_react.Component);

exports.default = AudioBox;
exports.AudioBox = AudioBox;