(function (context, $) {

  "use strict";

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

    var ASSIGNMENT = context.JK.ASSIGNMENT;
    var VOICE_CHAT = context.JK.VOICE_CHAT;
    var gearUtils = context.JK.GearUtils;
    var logger = context.JK.logger;

    var $step = null;
    var $reuseAudioInputRadio = null;
    var $useChatInputRadio = null;
    var $chatInputs = null;
    var $templateChatInput = null;
    var $selectedChatInput = null;// should only be used if isChatEnabled = true

    var voiceChatHelper = new context.JK.VoiceChatHelper(app);
    var firstTimeShown = false;
    var $watchVideoBtn = null;
    var firstTime = true;
    var helpTimeout = null;
    var operatingSystem = null;

    function handleHelp() {
      if (operatingSystem == "Win32") {
        return "https://jamkazam.freshdesk.com/support/solutions/articles/66000123633-setup-wizard-windows-step-4-setting-up-a-chat-mic"
      }
      else {
        return "https://jamkazam.freshdesk.com/support/solutions/articles/66000123594-setup-wizard-mac-step-4-setting-up-a-chat-mic"
      }
    }

    function newSession() {
      firstTimeShown = true;
    }

    function beforeShow() {
      var forceDisabledChat = firstTimeShown;

      voiceChatHelper.reset(forceDisabledChat);
      voiceChatHelper.beforeShow();
      firstTimeShown = false;

      prodInstructions()
    }

    function beforeHide() {
      voiceChatHelper.beforeHide();
    }

    function handleNext() {

      if (helpTimeout) {
        clearTimeout(helpTimeout);
      }
      return true;
    }


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

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

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

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

      operatingSystem = _operatingSystem;
      voiceChatHelper.initialize($step, 'configure_voice_gear_wizard', true, {vuType: "vertical", lightCount: 8, lightWidth: 3, lightHeight: 10}, 101);

      $watchVideoBtn = $step.find('.watch-video')
      $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);