(function (context, $) {

  "use strict";

  context.JK = context.JK || {};
  context.JK.StepDirectMonitoring = function (app) {

    var EVENTS  = context.JK.EVENTS;
    var logger = context.JK.logger;
    var $step = null;
    var $directMonitoringBtn = null;
    var $adjustSettingsDirectMonitor = null;
    var isPlaying = false;
    var playCheckInterval = null;
    var trackDurationMs = null;
    var $watchVideoBtn = null;
    var firstTime = true;
    var helpTimeout = null;
    var operatingSystem = null;
    function checkIfPlaying() {
      var currentPositionMs = context.jamClient.SessionCurrrentPlayPosMs();
      var atEnd = currentPositionMs == 0 || trackDurationMs == currentPositionMs;

      if(atEnd) {
        context.jamClient.SessionStopPlay();
        startPlay();
      }
    }

    function startPlay() {
      context.jamClient.SessionTrackSeekMs(0);
      context.jamClient.SessionStartPlay(1);
      $directMonitoringBtn.removeClass('playing paused').addClass('playing');

      trackDurationMs = context.jamClient.SessionGetTracksPlayDurationMs();
      if(!playCheckInterval) {
        playCheckInterval = setInterval(checkIfPlaying, 333);
      }
      isPlaying = true;
    }

    function stopPlay() {

      if(playCheckInterval) {
        clearTimeout(playCheckInterval);
        playCheckInterval = null;
      }

      context.jamClient.SessionStopPlay();
      $directMonitoringBtn.removeClass('playing paused').addClass('paused');
      isPlaying = false;

    }

    function togglePlay() {
      if(isPlaying) {
        stopPlay();
      }
      else {
        startPlay();
      }
    }

    function handleHelp() {
      if (operatingSystem == "Win32") {
        return "https://jamkazam.freshdesk.com/support/solutions/articles/66000123634-setup-wizard-windows-step-5-turning-off-direct-monitoring"
      }
      else {
        return "https://jamkazam.freshdesk.com/support/solutions/articles/66000123595-setup-wizard-mac-step-5-turning-off-direct-monitoring"
      }
    }

    function handleNext() {
      if (helpTimeout) {
        clearTimeout(helpTimeout);
      }
    }

    function newSession() {

    }

    function beforeShow() {
      context.jamClient.SessionRemoveAllPlayTracks();
      logger.debug("adding test sound");
      if(!context.jamClient.SessionAddPlayTrack("skin:jktest-audio.wav")) {
        context.JK.alertSupportedNeeded('Unable to open test sound');
      }
      prodInstructions()
    }

    function beforeHide() {
      if(isPlaying) {
        stopPlay();
      }

      logger.debug("removing test sound")
      context.jamClient.SessionRemoveAllPlayTracks();

      if(playCheckInterval) {
        clearTimeout(playCheckInterval);
        playCheckInterval = null;
      }
    }

    function onAdjustGearRequested() {
      app.layout.showDialog('adjust-gear-speed-dialog').one(EVENTS.DIALOG_CLOSED, function(e, data) {

        var adjustGearTest = data.result;

        if(!data.canceled) {
          if(adjustGearTest.isGoodFtue()) {

          }
        }
        else {
          logger.debug("adjust-gear-speed was cancelled; ignoring")
        }
      })
    }

    function prodInstructions() {
      if (firstTime && !helpTimeout) {

        helpTimeout = setTimeout(function() {
          helpTimeout = null
          firstTime = false;

          context.JK.HelpBubbleHelper.ftueStep4Instructions($watchVideoBtn)
        }, 2000)
      }
    }

    function initialize(_$step, _wizard, _operatingSystem) {
      $step = _$step;

      $directMonitoringBtn = $step.find('.test-direct-monitoring');
      $directMonitoringBtn.on('click', togglePlay);
      $adjustSettingsDirectMonitor = $step.find('.adjust-settings-direct-monitor');
      $adjustSettingsDirectMonitor.on('click', onAdjustGearRequested)
      $watchVideoBtn = $step.find('.watch-video')
      operatingSystem = _operatingSystem
      $watchVideoBtn.attr('href', handleHelp())
    }

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

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