'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, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _partial = require('lodash/partial');

var _partial2 = _interopRequireDefault(_partial);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

var _IconArrowOpenDownLine = require('instructure-icons/lib/Line/IconArrowOpenDownLine');

var _IconArrowOpenDownLine2 = _interopRequireDefault(_IconArrowOpenDownLine);

var _IconArrowOpenUpLine = require('instructure-icons/lib/Line/IconArrowOpenUpLine');

var _IconArrowOpenUpLine2 = _interopRequireDefault(_IconArrowOpenUpLine);

var _Button = require('@instructure/ui-buttons/lib/components/Button');

var _Button2 = _interopRequireDefault(_Button);

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

var _lib2 = _interopRequireDefault(_lib);

var _FormattedDuration = require('../../../shared/FormattedDuration');

var _FormattedDuration2 = _interopRequireDefault(_FormattedDuration);

var _formatTimespan = require('../../../../util/formatTimespan');

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

var _theme2 = _interopRequireDefault(_theme);

var _formatMessage = require('../../../../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.Timer__styles__timer {\n  line-height: 1rem;\n  background-color: ' + theme.timerBackgroundColor + ';\n  color: ' + theme.timerColor + ';\n\n  border: solid ' + theme.timerBorderWidth + ' ' + theme.timerBorderColor + ';\n  border-radius: ' + theme.timerBorderRadius + ';\n\n  font-size: ' + theme.timerFontSize + ';\n  padding: 0 ' + theme.timerPadding + ';\n  margin-bottom: ' + theme.timerMargin + ';\n\n  display: flex;\n  flex-direction: row;\n  align-items: center;\n  justify-content: center;\n}\n\n.Timer__styles__timerLeft {\n  display: flex;\n  flex-direction: column-reverse;\n  align-items: center;\n}\n\n.Timer__styles__timerRight {\n  padding: ' + theme.timerRightPadding + ';\n}\n\n.Timer__styles__timeRemainingLabel {\n  font-size: ' + theme.timeRemainingLabelFontSize + ';\n  width: 100%;\n  text-align: center;\n}\n\n[dir="ltr"] .Timer__styles__timeRemainingLabel {\n  text-align: center;\n}\n\n[dir="rtl"] .Timer__styles__timeRemainingLabel {\n  text-align: center;\n}\n';
  },
  'timer': 'Timer__styles__timer',
  'timerLeft': 'Timer__styles__timerLeft',
  'timerRight': 'Timer__styles__timerRight',
  'timeRemainingLabel': 'Timer__styles__timeRemainingLabel'
};
var Timer = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(Timer, _Component);

  function Timer() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Timer);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Timer.__proto__ || Object.getPrototypeOf(Timer)).call.apply(_ref, [this].concat(args))), _this), _this.handleKeyDown = function (event) {
      // Check for Alt+Shift+T and nothing else
      if (event.keyCode === 84 && event.altKey && event.shiftKey && !event.ctrlKey) {
        var message = void 0;
        event.preventDefault();

        if (_this.props.timeLeftInSeconds >= 1) {
          var timeString = (0, _formatTimespan.formatTime)(_this.props.timeLeftInSeconds);
          message = 'Il tempo residuo \xE8: ' + timeString;
        } else {
          message = 'Nessun tempo residuo rimasto';
        }

        _this.props.screenreaderNotification(message);
      }
    }, _this.showTimerRef = function (node) {
      _this.showTimerBtn = node;
    }, _this.hideTimerRef = function (node) {
      _this.hideTimerBtn = node;
    }, _this.focusOnShow = function () {
      _this.showTimerBtn && _this.showTimerBtn.focus();
    }, _this.focusOnHide = function () {
      _this.hideTimerBtn && _this.hideTimerBtn.focus();
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Timer, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps, prevState) {
      var _props = this.props,
          timerMinimized = _props.timerMinimized,
          screenreaderNotification = _props.screenreaderNotification,
          timerRunning = _props.timerRunning;


      if (timerMinimized !== prevProps.timerMinimized) {
        var message = timerMinimized ? 'timer ridotto a icona' : 'timer visualizzato';
        screenreaderNotification(message);
        var focusFn = timerMinimized ? this.focusOnShow : this.focusOnHide;
        focusFn();
      }

      if (timerRunning && !prevProps.timerRunning) {
        document.body.addEventListener('keydown', this.handleKeyDown);
      } else if (!timerRunning && prevProps.timerRunning) {
        document.body.removeEventListener('keydown', this.handleKeyDown);
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      document.body.removeEventListener('keydown', this.handleKeyDown);
    }
  }, {
    key: 'renderHelpScreenreaderText',
    value: function renderHelpScreenreaderText() {
      return _react2.default.createElement(
        _ScreenReaderContent2.default,
        null,
        'Durante il quiz, premi ALT+MAIUSC+T per consentire la lettura del tempo residuo'
      );
    }
  }, {
    key: 'renderMinimizedTimer',
    value: function renderMinimizedTimer() {
      return _react2.default.createElement(
        'div',
        { className: styles.timer },
        this.renderHelpScreenreaderText(),
        _react2.default.createElement(
          _Button2.default,
          {
            ref: this.showTimerRef,
            variant: 'icon',
            onClick: (0, _partial2.default)(this.props.updateTimerMinimized, false)
          },
          _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            'Abilita visibilit\xE0 timer'
          ),
          _react2.default.createElement(_IconArrowOpenDownLine2.default, { height: '1rem', width: '1rem' })
        )
      );
    }
  }, {
    key: 'renderMaximizedTimer',
    value: function renderMaximizedTimer() {
      return _react2.default.createElement(
        'div',
        { className: styles.timer },
        this.renderHelpScreenreaderText(),
        _react2.default.createElement(
          'div',
          { className: styles.timerLeft },
          _react2.default.createElement(
            'div',
            { className: styles.timeRemainingLabel },
            'Tempo rimanente'
          ),
          _react2.default.createElement(_FormattedDuration2.default, { timeLimitInSeconds: this.props.timeLeftInSeconds })
        ),
        _react2.default.createElement(
          'div',
          { className: styles.timerRight },
          _react2.default.createElement(
            _Button2.default,
            {
              ref: this.hideTimerRef,
              variant: 'icon',
              onClick: (0, _partial2.default)(this.props.updateTimerMinimized, true)
            },
            _react2.default.createElement(
              _ScreenReaderContent2.default,
              null,
              'Abilita visibilit\xE0 timer'
            ),
            _react2.default.createElement(_IconArrowOpenUpLine2.default, { height: '1rem', width: '1rem' })
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      if (!this.props.timerRunning) {
        return null;
      } else if (this.props.timerMinimized) {
        return this.renderMinimizedTimer();
      } else {
        return this.renderMaximizedTimer();
      }
    }
  }]);
  Timer.displayName = 'Timer'
  ;
  return Timer;
}(_react.Component), _class2.propTypes = {
  screenreaderNotification: _propTypes2.default.func.isRequired,
  timeLeftInSeconds: _propTypes2.default.number,
  timerMinimized: _propTypes2.default.bool,
  timerRunning: _propTypes2.default.bool,
  updateTimerMinimized: _propTypes2.default.func.isRequired
}, _class2.defaultProps = {
  timerRunning: false,
  timeLeftInSeconds: 0,
  timerMinimized: false
}, _temp2)) || _class);
exports.default = Timer;