'use strict';

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

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;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

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

var _lib2 = _interopRequireDefault(_lib);

var _Card = require('../../../../common/components/shared/Card');

var _Card2 = _interopRequireDefault(_Card);

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

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

var _Page = require('../../../../common/components/layout/Page');

var _Page2 = _interopRequireDefault(_Page);

var _Header = require('../../../../common/components/resources/quizSessionResult/Header');

var _Header2 = _interopRequireDefault(_Header);

var _Info = require('../../../../common/components/resources/quizSessionResult/Info');

var _Info2 = _interopRequireDefault(_Info);

var _SessionItemResultsList = require('../../../../common/components/resources/sessionItemResult/SessionItemResultsList');

var _SessionItemResultsList2 = _interopRequireDefault(_SessionItemResultsList);

var _Spinner = require('../../../../common/components/shared/spinner/Spinner');

var _Spinner2 = _interopRequireDefault(_Spinner);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _AttemptHistory = require('../../AttemptHistory');

var _AttemptHistory2 = _interopRequireDefault(_AttemptHistory);

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

var _theme2 = _interopRequireDefault(_theme);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.Results__styles__quizResults {\n  margin-top: ' + theme.quizResultsMargin + ';\n  width: 100%;\n  height: 100%;\n}\n';
  },
  'quizResults': 'Results__styles__quizResults'
};


// ================================
// this is the results page for a student
// displayed after the student submits the quiz
// ================================

var Results = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(Results, _Component);

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

  (0, _createClass3.default)(Results, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.getResults();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      if (this.props.quizSessionId !== prevProps.quizSessionId) {
        this.getResults();
      }
    }
  }, {
    key: 'getResults',
    value: function getResults() {
      var _props = this.props,
          getResultInfo = _props.getResultInfo,
          quizSessionId = _props.quizSessionId;

      getResultInfo(quizSessionId, void 0, true);
    }
  }, {
    key: 'regradeFilter',
    value: function regradeFilter(sir) {
      return sir.regradeInfo && sir.regradeInfo.get('regraded');
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          appContainer = _props2.appContainer,
          attemptHistory = _props2.attemptHistory,
          authoritativeResult = _props2.authoritativeResult,
          needToLoad = _props2.needToLoad,
          onAttemptClick = _props2.onAttemptClick,
          quizSession = _props2.quizSession,
          quizSessionId = _props2.quizSessionId,
          scoreToKeep = _props2.scoreToKeep,
          sessionItemResults = _props2.sessionItemResults,
          sessionItems = _props2.sessionItems,
          studentHeaderDescription = _props2.studentHeaderDescription,
          isAutograding = _props2.isAutograding;


      if (needToLoad && !isAutograding) {
        return _react2.default.createElement(
          _Card2.default,
          { wrapped: true },
          _react2.default.createElement(_Spinner2.default, null)
        );
      }

      if (isAutograding) {
        return _react2.default.createElement(
          _Card2.default,
          { wrapped: true },
          _react2.default.createElement(_Spinner2.default, { mainText: '\u81EA\u52A8\u8BC4\u5206...' })
        );
      }

      return _react2.default.createElement(
        _Page2.default,
        null,
        _react2.default.createElement(
          _Card2.default,
          { wrapped: true },
          _react2.default.createElement(_Header2.default, {
            quizSession: quizSession,
            studentDescription: studentHeaderDescription,
            regradedSessionItemResults: sessionItemResults.filter(this.regradeFilter)
          }),
          _react2.default.createElement(_Info2.default, {
            quizSession: quizSession,
            quizSessionResult: authoritativeResult,
            sessionItems: sessionItems,
            sessionItemResults: sessionItemResults
          }),
          this.props.children,
          attemptHistory && _react2.default.createElement(_AttemptHistory2.default, {
            attemptHistory: attemptHistory,
            onAttemptClick: onAttemptClick,
            quizId: quizSession.quiz,
            scoreToKeep: scoreToKeep,
            selectedSessionId: quizSessionId
          }),
          sessionItems.size > 0 && _react2.default.createElement(_SessionItemResultsList2.default, {
            appContainer: appContainer,
            sessionItems: sessionItems
          })
        )
      );
    }
  }]);
  Results.displayName = 'Results'
  ;
  return Results;
}(_react.Component), _class2.propTypes = {
  appContainer: _CustomPropTypes2.default.selectors.isRequired,
  attemptHistory: _CustomPropTypes2.default.attemptHistory,
  authoritativeResult: _reactImmutableProptypes2.default.record,
  children: _propTypes2.default.node,
  getResultInfo: _propTypes2.default.func.isRequired,
  isAutograding: _propTypes2.default.bool.isRequired,
  needToLoad: _propTypes2.default.bool,
  onAttemptClick: _propTypes2.default.func,
  quizSession: _reactImmutableProptypes2.default.record,
  quizSessionId: _propTypes2.default.string.isRequired,
  scoreToKeep: _propTypes2.default.string,
  sessionItemResults: _reactImmutableProptypes2.default.list,
  sessionItems: _reactImmutableProptypes2.default.list.isRequired,
  studentHeaderDescription: _propTypes2.default.node
}, _class2.defaultProps = {
  attemptHistory: null,
  authoritativeResult: null,
  children: null,
  needToLoad: false,
  onAttemptClick: function onAttemptClick() {},

  quizSession: null,
  scoreToKeep: 'highest',
  sessionItemResults: null,
  studentHeaderDescription: null
}, _temp)) || _class);
exports.default = Results;