(function (context, $) {

  "use strict";

  context.JK = context.JK || {};
  context.JK.StepNetworkTest = function (app, dialog) {


    var logger = context.JK.logger;
    var networkTest = new context.JK.NetworkTest(app);
    var $step = null;
    // if not null and with in say 5 seconds, then the user is 'NEXT'ing too quickly. slow them down
    var clickFastTime = null;

    function getLastNetworkFailAnalytics() {
      return networkTest.getLastNetworkFailure();
    }

    function networkTestDone() {
      updateButtons();
    }

    function networkTestFail() {
      updateButtons();
    }

    function networkTestCancel() {
      updateButtons();
    }

    function networkTestStart() {
      updateButtons();
    }

    function updateButtons() {
      initializeNextButtonState();
      initializeBackButtonState();
    }
    function initializeNextButtonState() {
      dialog.setNextState(!networkTest.isScoring());
    }

    function initializeBackButtonState() {
      dialog.setBackState(!networkTest.isScoring());
    }

    function handleNext() {
      // if we don't have a valid score, and if it's been less than 5 seconds since we've shown this step, slow the user down
      if (context.jamClient.GetNetworkTestScore() < 1 && userIsFastNexting()) {
        context.JK.Banner.showYesNo({
          html: "By clicking NEXT and skipping the test, you won't be able to play online in real-time sessions with others.  Is this OK?",
          yes: function() {
            dialog.moveToNext();
          }});

          return false;
      }
      else {
        return true;
      }
    }

    function handleHelp() {
      return "https://www.youtube.com/watch?v=rhAdCVuwhBc"
      //return "https://jamkazam.desk.com/customer/portal/articles/1599969-first-time-setup---step-6---test-your-network";
    }

    function newSession() {
      networkTest.reset();
      updateButtons();
    }

    function beforeShow() {
      networkTest.haltScoring();
      networkTest.cancel();
      updateButtons();
      watchForFastNexting();
    }

    // fast nexting is a someone hitting next very quickly
    function watchForFastNexting() {
      clickFastTime = new Date();
    }

    function userIsFastNexting() {
      return new Date().getTime() - clickFastTime.getTime() < 5000
    }

    function beforeHide() {
      networkTest.resumeScoring();
      networkTest.cancel();
    }

    function initialize(_$step) {
      $step = _$step;
      networkTest.initialize($step, true);
      $(networkTest)
        .on(networkTest.NETWORK_TEST_DONE, networkTestDone)
        .on(networkTest.NETWORK_TEST_FAIL, networkTestFail)
        .on(networkTest.NETWORK_TEST_CANCEL, networkTestCancel)
        .on(networkTest.NETWORK_TEST_START, networkTestStart)
    }

    this.handleHelp = handleHelp;
    this.newSession = newSession;
    this.beforeHide = beforeHide;
    this.handleNext = handleNext;
    this.beforeShow = beforeShow;
    this.initialize = initialize;
    this.getLastNetworkFailAnalytics = getLastNetworkFailAnalytics;

    return this;
  }
})(window, jQuery);