(function (context, $) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.JoinTestSessionDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var $dialog = null;
    var $joinTestSessionBtn = null;

    function joinSession(sessionId) {
      app.layout.closeDialog('join-test-session')
      $joinTestSessionBtn.removeClass('disabled');
      context.JK.GA.trackSessionCount(true, true, 0);

      // we redirect to the session screen, which handles the REST call to POST /participants.
      logger.debug("joining session screen: " + sessionId)
      context.location = '/client#/session/' + sessionId;
    };

    function createSession(sessionId) {
      var data = {};

      data.name = 'First Session';
      data.description = 'This is my first ever session on JamKazam, so please be gentle. :)';
      data.genres = ['other']
      data.musician_access = true;
      data.approval_required = false;
      data.fan_access = true;
      data.fan_chat = true;
      data.legal_policy = 'Standard'
      data.legal_terms = true;
      data.language = 'eng';
      data.start = new Date().toDateString() + ' ' + context.JK.formatUtcTime(new Date(), false);
      data.duration = "60";
      data.recurring_mode = 'once'
      data.music_notations = [];
      data.timezone = 'Central Time (US & Canada),America/Chicago'
      data.open_rsvps = true
      data.rsvp_slots = [];

      // auto pick an 'other' instrument
      var otherId = context.JK.server_to_client_instrument_map.Other.server_id; // get server ID
      var otherInstrumentInfo = context.JK.instrument_id_to_instrument[otherId]; // get display name
      var beginnerLevel  = 1; // default to beginner
      var instruments = [ {id: otherId, name: otherInstrumentInfo.display, level: beginnerLevel} ];
      $.each(instruments, function(index, instrument) {
        var slot = {};
        slot.instrument_id = instrument.id;
        slot.proficiency_level = instrument.level;
        slot.approve = true;
        data.rsvp_slots.push(slot);
      });

      data.isUnstructuredRsvp = true;

      rest.createScheduledSession(data)
        .done(function(response) {
          logger.debug("created test session on server");
          //$('#create-session-buttons .btn-next').off('click');
          var newSessionId = response.id;

          joinSession(newSessionId);
        })
        .fail(function(jqXHR){
          $joinTestSessionBtn.removeClass('disabled');
          logger.debug("unable to schedule a test session")
          app.notifyServerError(jqXHR, "Unable to schedule a test session");
        })
    }

    function registerEvents() {

      $joinTestSessionBtn.click(function() {

        if($joinTestSessionBtn.is('.disabled')) return false;

        $joinTestSessionBtn.addClass('disabled');

        createSession();

        return false;
      })
    }

    function beforeShow() {
      $joinTestSessionBtn.removeClass('disabled');
    }

    function beforeHide() {

    }

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

      app.bindDialog('join-test-session', dialogBindings);

      $dialog = $('#join-test-session-dialog');
      $joinTestSessionBtn = $dialog.find('.join-test-session')

      registerEvents();
    };


    this.initialize = initialize;
  }

  return this;
})(window, jQuery);