"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var TimerService =
/*#__PURE__*/
function () {
  function TimerService(getTimeRemaining, updateTimeRemaining, getTimerData, submitQuiz, alert) {
    var _this = this;

    (0, _classCallCheck2.default)(this, TimerService);

    this.__performApiCall = function () {
      _this._getTimeRemaining(function () {
        var data = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};

        if (_this._stopped) {
          return;
        }

        _this.__resetClockInterval(data.timerRunning && data.timeLeftInSeconds >= 0);

        _this.__onTimerChange(data);
      });
    };

    this._getTimeRemaining = getTimeRemaining;
    this._updateTimeRemaining = updateTimeRemaining;
    this._getTimerData = getTimerData;
    this._submitQuiz = submitQuiz;
    this._alert = alert;
    this._stopped = false;
    this._apiInterval = null;
    this._clockInterval = null;
    this._apiTimeIncrement = 10000;
    this._clockTimeIncrement = 1000;
  }

  (0, _createClass2.default)(TimerService, [{
    key: "start",
    value: function start() {
      this.__performApiCall();

      this.__startApiInterval();

      return this;
    }
  }, {
    key: "stop",
    value: function stop() {
      this._stopped = true;

      this.__stopApiInterval();

      this.__stopClockInterval();

      return this;
    }
  }, {
    key: "__ensureNotStopped",
    value: function __ensureNotStopped() {
      if (this._stopped) {
        throw new Error('An attempt was made to restart a stopped timer. This is disallowed ' + 'to prevent tricky race conditions. Create a new TimerService object instead.');
      }
    }
  }, {
    key: "__startApiInterval",
    value: function __startApiInterval() {
      this.__ensureNotStopped();

      this._apiInterval = setInterval(this.__performApiCall, this._apiTimeIncrement);
    }
  }, {
    key: "__stopApiInterval",
    value: function __stopApiInterval() {
      clearInterval(this._apiInterval);
      this._apiInterval = null;
    }
  }, {
    key: "__startClockInterval",
    value: function __startClockInterval() {
      var _this2 = this;

      this.__ensureNotStopped();

      var action = function action() {
        var _this2$_getTimerData$ = _this2._getTimerData().toJS(),
            timerRunning = _this2$_getTimerData$.timerRunning,
            timeLeftInSeconds = _this2$_getTimerData$.timeLeftInSeconds;

        if (!timerRunning || timeLeftInSeconds !== 0 && !timeLeftInSeconds) {
          return;
        }

        var delta = Math.round(_this2._clockTimeIncrement / 1000);
        var newTimeLeftInSeconds = timeLeftInSeconds - delta;

        _this2.__onTimerChange({
          timerRunning: timerRunning,
          timeLeftInSeconds: newTimeLeftInSeconds
        });
      };

      this._clockInterval = setInterval(action, this._clockTimeIncrement);
    }
  }, {
    key: "__stopClockInterval",
    value: function __stopClockInterval() {
      clearInterval(this._clockInterval);
      this._clockInterval = null;
    }
  }, {
    key: "__resetClockInterval",
    value: function __resetClockInterval() {
      var startClock = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true;

      this.__stopClockInterval();

      if (startClock) {
        this.__startClockInterval();
      }
    }
  }, {
    key: "__onTimerChange",
    value: function __onTimerChange(_ref) {
      var timerRunning = _ref.timerRunning,
          timeLeftInSeconds = _ref.timeLeftInSeconds;

      var prevTimerData = this._getTimerData();

      this._updateTimeRemaining({
        timerRunning: timerRunning,
        timeLeftInSeconds: timeLeftInSeconds
      });

      if (prevTimerData.get('timeLeftInSeconds') > 30 && timeLeftInSeconds <= 30) {
        this._alert("Du har mindre \xE4n 30 sekunder kvar.");
      }

      if (timeLeftInSeconds !== null && timeLeftInSeconds <= 0) {
        this.stop();

        this._submitQuiz();
      }
    }
  }]);
  return TimerService;
}();

exports.default = TimerService;