(function (context, $) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.GettingStartedDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var invitationDialog = null;
    var $dialog = null;
    var $dontShowAgain = null;
    var $setupGearBtn = null;
    var $browserJamTrackBtn = null;
    var $jamTrackSection = null;
    var $jamTracksLimitedTime = null;

    function handleStartAudioQualification() {

      app.afterFtue = function() {
        app.layout.showDialog('join-test-session');
      };
      app.cancelFtue = function() { };
      app.layout.startNewFtue()
    }

    function registerEvents() {

      $setupGearBtn.click(function() {
        if (gon.isNativeClient) {

          app.layout.closeDialog('getting-started');

          // if no profiles, show FTUE in-line, if any, redirect to audio profile line
          var profiles = context.jamClient.FTUEGetAllAudioConfigurations();

          if(profiles && profile.length > 0) {
            window.location = '/client#/account/audio'
          }
          else
          {
            handleStartAudioQualification();
          }


        }
        else {
          context.JK.guardAgainstBrowser(app, {d1: 'gear'});
        }
        return false;
      })

      $browserJamTrackBtn.click(function() {
        app.layout.closeDialog('getting-started')
        window.location = '/client#/jamtrack'
        return false;
      })

      $('#getting-started-dialog a.facebook-invite').on('click', function (e) {
        invitationDialog.showFacebookDialog(e);
      });

      $('#getting-started-dialog a.google-invite').on('click', function (e) {
        invitationDialog.showGoogleDialog();
      });

      $('#getting-started-dialog a.email-invite').on('click', function (e) {
        invitationDialog.showEmailDialog();
      });
    }

    function beforeShow() {
      app.user().done(function(user) {
        var jamtrackRule = user.has_redeemable_jamtrack ? 'has-free-jamtrack' : 'no-free-jamtrack'
        $jamTrackSection.removeClass('has-free-jamtrack').removeClass('no-free-jamtrack').addClass(jamtrackRule)
        if(user.has_redeemable_jamtrack) {
          $jamTracksLimitedTime.removeClass('hidden')
        }
      })
    }

    function beforeHide() {
      var showWhatsNext = !$dontShowAgain.is(':checked')
      app.user().done(function(user) {
        app.updateUserModel({show_whats_next: showWhatsNext, show_whats_next_count: user.show_whats_next_count + 1})
      })

    }

    function initializeButtons() {

      context.JK.checkbox($dontShowAgain);
    }

    function initialize(invitationDialogInstance) {
      var dialogBindings = {
        'beforeShow': beforeShow,
        'beforeHide': beforeHide
      };

      app.bindDialog('getting-started', dialogBindings);

      $dialog = $('#getting-started-dialog');
      $dontShowAgain = $dialog.find('#show_getting_started');
      $setupGearBtn = $dialog.find('.setup-gear-btn')
      $browserJamTrackBtn = $dialog.find('.browse-jamtrack');
      $jamTrackSection = $dialog.find('.get-a-free-jamtrack-section')
      $jamTracksLimitedTime = $dialog.find('.jamtracks-limited-time')

      registerEvents();

      invitationDialog = invitationDialogInstance;

      initializeButtons();
    };


    this.initialize = initialize;
  }

  return this;
})(window, jQuery);