'use strict';

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

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _dec,
    _class,
    _class2,
    _temp,
    _default_enGB_number = Intl.NumberFormat('en-GB', { style: 'decimal' }).format;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames3 = require('classnames');

var _classnames4 = _interopRequireDefault(_classnames3);

var _lib = require('@instructure/ui-themeable/lib');

var _lib2 = _interopRequireDefault(_lib);

var _Link = require('@instructure/ui-elements/lib/components/Link');

var _Link2 = _interopRequireDefault(_Link);

var _ScreenReaderContent = require('@instructure/ui-a11y/lib/components/ScreenReaderContent');

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

var _Table = require('@instructure/ui-elements/lib/components/Table');

var _Table2 = _interopRequireDefault(_Table);

var _ToggleDetails = require('@instructure/ui-toggle-details/lib/components/ToggleDetails');

var _ToggleDetails2 = _interopRequireDefault(_ToggleDetails);

var _Text = require('@instructure/ui-elements/lib/components/Text');

var _Text2 = _interopRequireDefault(_Text);

var _Heading = require('@instructure/ui-elements/lib/components/Heading');

var _Heading2 = _interopRequireDefault(_Heading);

var _Tooltip = require('@instructure/ui-overlays/lib/components/Tooltip');

var _Tooltip2 = _interopRequireDefault(_Tooltip);

var _IconOffLine = require('instructure-icons/lib/Line/IconOffLine');

var _IconOffLine2 = _interopRequireDefault(_IconOffLine);

var _CustomPropTypes = require('../../../common/util/CustomPropTypes');

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

var _formatMessage = require('../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.AttemptHistory__styles__container {}\n\n.AttemptHistory__styles__bold {\n  font-weight: ' + theme.boldFontWeight + ';\n}\n\n.AttemptHistory__styles__emptyText {\n  display: block;\n  margin: ' + theme.emptyTextMargin + ' 0 0 0;\n}\n\n.AttemptHistory__styles__page {\n  overflow-y: scroll;\n  overflow-x: hidden;\n  flex: 1;\n  background-color: ' + theme.pageBackgroundColor + ';\n}\n\n.AttemptHistory__styles__sectionWrapper {\n  box-sizing: border-box;\n  padding: ' + theme.sectionWrapperPadding + ' ' + theme.sectionWrapperPaddingRight + ' ' + theme.sectionWrapperPadding + ' ' + theme.sectionWrapperPadding + ';\n  min-height: 3.25rem;\n  margin-bottom: ' + theme.sectionWrapperMargin + ';\n}\n';
  },
  'container': 'AttemptHistory__styles__container',
  'bold': 'AttemptHistory__styles__bold',
  'emptyText': 'AttemptHistory__styles__emptyText',
  'page': 'AttemptHistory__styles__page',
  'sectionWrapper': 'AttemptHistory__styles__sectionWrapper'
};
var AttemptHistory = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(AttemptHistory, _Component);

  function AttemptHistory() {
    (0, _classCallCheck3.default)(this, AttemptHistory);
    return (0, _possibleConstructorReturn3.default)(this, (AttemptHistory.__proto__ || Object.getPrototypeOf(AttemptHistory)).apply(this, arguments));
  }

  (0, _createClass3.default)(AttemptHistory, [{
    key: 'componentDidMount',


    // =============
    //   LIFECYCLE
    // =============

    value: function componentDidMount() {
      if (this.props.attemptHistory) {
        this.fetchData();
      }
    }
  }, {
    key: 'fetchData',
    value: function fetchData() {
      var _props = this.props,
          getQuizSessions = _props.getQuizSessions,
          quizId = _props.quizId,
          attemptHistory = _props.attemptHistory;

      var quizSessionIds = attemptHistory.map(function (attempt) {
        return attempt.quizSessionId;
      }).filter(function (quizSessionId) {
        return quizSessionId !== (void 0 || null);
      });

      if (!quizSessionIds.length) return;

      getQuizSessions(quizId, { ids: quizSessionIds });
    }
  }, {
    key: 'onAttemptClick',


    // =============
    //   Callbacks
    // =============

    value: function onAttemptClick(quizSessionId) {
      var _this2 = this;

      return function () {
        _this2.props.onAttemptClick(quizSessionId);
      };
    }

    // =============
    //   RENDERING
    // =============

  }, {
    key: 'renderAttemptLink',
    value: function renderAttemptLink(attempt, i) {
      var displayName = attempt.displayName,
          quizSessionId = attempt.quizSessionId;

      var attemptName = displayName || 'Attempt ' + (i + 1);
      var classes = (0, _classnames4.default)((0, _defineProperty3.default)({}, styles.bold, this.props.selectedSessionId === quizSessionId));

      if (this.props.onAttemptClick && quizSessionId) {
        return _react2.default.createElement(
          _Link2.default,
          { onClick: this.onAttemptClick(quizSessionId) },
          _react2.default.createElement(
            'span',
            { className: classes },
            attemptName
          )
        );
      } else {
        return attemptName;
      }
    }
  }, {
    key: 'renderHiddenAttemptInfo',
    value: function renderHiddenAttemptInfo() {
      return _react2.default.createElement(
        _Tooltip2.default,
        {
          variant: 'inverse',
          tip: 'Not displayed per instructor settings.',
          placement: 'top',
          mountNode: function mountNode() {
            return document.getElementById('main');
          }
        },
        _react2.default.createElement(_IconOffLine2.default, { size: 'x-small' })
      );
    }
  }, {
    key: 'renderAttemptHistory',
    value: function renderAttemptHistory() {
      var _this3 = this;

      return this.props.attemptHistory.map(function (attempt, i) {
        var points = null;
        var score = null;

        if (attempt.score != null) {
          if (attempt.pointsPossible) {
            points = _default_enGB_number(attempt.score) + ' of ' + _default_enGB_number(attempt.pointsPossible);
            if (attempt.percentage == null) {
              score = _formatMessage2.default.number(attempt.score / attempt.pointsPossible, 'percent');
            }
          } else {
            points = _formatMessage2.default.number(attempt.score);
          }
        }

        if (attempt.percentage != null) {
          score = _formatMessage2.default.number(attempt.percentage, 'percent');
        }

        var classes = (0, _classnames4.default)((0, _defineProperty3.default)({}, styles.bold, _this3.props.selectedSessionId === attempt.quizSessionId));

        return _react2.default.createElement(
          'tr',
          { className: classes, key: i },
          _react2.default.createElement(
            'td',
            null,
            _this3.renderAttemptLink(attempt, i)
          ),
          _react2.default.createElement(
            'td',
            null,
            points || _this3.renderHiddenAttemptInfo()
          ),
          _react2.default.createElement(
            'td',
            null,
            score || _this3.renderHiddenAttemptInfo()
          ),
          _react2.default.createElement(
            'td',
            null,
            _this3.renderScoreBody(attempt)
          )
        );
      });
    }
  }, {
    key: 'renderScoreBody',
    value: function renderScoreBody(attempt) {
      if (typeof attempt.score != 'undefined' && attempt.authoritative && this.props.scoreToKeep !== 'average') {
        return '(' + this.scoreToKeepString + ' score)';
      }
    }
  }, {
    key: 'renderTable',
    value: function renderTable() {
      return _react2.default.createElement(
        'div',
        { className: styles.sectionWrapper },
        _react2.default.createElement(
          _ToggleDetails2.default,
          {
            defaultExpanded: true,
            summary: _react2.default.createElement(
              _Heading2.default,
              { level: 'h2', color: 'primary' },
              'Attempt history'
            )
          },
          _react2.default.createElement(
            _Table2.default,
            {
              caption: _react2.default.createElement(
                _ScreenReaderContent2.default,
                null,
                'Attempt history'
              )
            },
            _react2.default.createElement(
              'thead',
              null,
              _react2.default.createElement(
                'tr',
                null,
                _react2.default.createElement(
                  'th',
                  null,
                  'Results'
                ),
                _react2.default.createElement(
                  'th',
                  null,
                  'Points'
                ),
                _react2.default.createElement(
                  'th',
                  null,
                  'Score'
                ),
                _react2.default.createElement(
                  'th',
                  null,
                  '(' + this.scoreToKeepString + ' score is kept)'
                )
              )
            ),
            _react2.default.createElement(
              'tbody',
              null,
              this.renderAttemptHistory()
            )
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var attemptHistory = this.props.attemptHistory;

      if (!attemptHistory) return null;
      if (attemptHistory.length > 0) {
        return this.renderTable();
      } else {
        return _react2.default.createElement(
          'div',
          { className: styles.page },
          _react2.default.createElement(
            'span',
            { className: styles.emptyText },
            _react2.default.createElement(
              _Text2.default,
              null,
              'No attempt information to display yet.'
            )
          )
        );
      }
    }
  }, {
    key: 'scoreToKeepString',
    get: function get() {
      return {
        highest: 'Highest',
        latest: 'Latest',
        average: 'Average'
      }[this.props.scoreToKeep];
    }
  }]);
  AttemptHistory.displayName = 'AttemptHistory'
  ;
  return AttemptHistory;
}(_react.Component), _class2.propTypes = {
  attemptHistory: _CustomPropTypes2.default.attemptHistory,
  getQuizSessions: _propTypes2.default.func.isRequired,
  onAttemptClick: _propTypes2.default.func,
  quizId: _propTypes2.default.string.isRequired,
  scoreToKeep: _propTypes2.default.oneOf(['highest', 'latest', 'average']).isRequired,
  selectedSessionId: _propTypes2.default.string.isRequired
}, _class2.defaultProps = {
  attemptHistory: null,
  onAttemptClick: function onAttemptClick() {} }, _temp)) || _class);
exports.default = AttemptHistory;