(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;

    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() {
      return "https://jamkazam.desk.com/customer/portal/articles/1599967-first-time-setup---step-5---turn-off-direct-monitoring";
    }

    function handleNext() {

    }

    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');
      }
    }

    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 initialize(_$step) {
      $step = _$step;

      $directMonitoringBtn = $step.find('.test-direct-monitoring');
      $directMonitoringBtn.on('click', togglePlay);
      $adjustSettingsDirectMonitor = $step.find('.adjust-settings-direct-monitor');
      $adjustSettingsDirectMonitor.on('click', onAdjustGearRequested)
    }

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

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