(function(context,$) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.RsvpCancelDialog = function(app, sessionId, rsvpRequestId) {
    var EVENTS = context.JK.EVENTS;
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var $dialog = null;
    var dialogId = 'rsvp-cancel-dialog';
    var $btnCancel = $("#btnCancelRsvp");

    function beforeShow(data) {
    }

    function afterShow(data) {

      rest.getSessionHistory(sessionId)
        .done(function(response) {
          if (response) {
            $('.session-name', $dialog).html(response.name);
            $('.scheduled-start', $dialog).html(response.pretty_scheduled_start_with_timezone);

            if (response.recurring_mode !== null && response.recurring_mode !== 'once') {
              $('.schedule-recurrence', $dialog).html("Recurs " + response.recurring_mode + " on this day at this time");
            }
          }
        })
        .fail(function(xhr) {

        });
    }

    function afterHide() {
    }

    function showDialog() {
      return app.layout.showDialog(dialogId);
    }

    function events() {
      $btnCancel.unbind('click');
      $btnCancel.click(function(e) {
        e.preventDefault();

        var error = false;
        var cancelOption = $('input[name="cancel"]:checked', $dialog).val();
        rest.cancelRsvpRequest(sessionId, rsvpRequestId, cancelOption)
          .done(function(response) {
            var comment = $.trim($('#txtComment', $dialog).val());
            if (comment.length > 0) {
              rest.addSessionInfoComment(sessionId, comment)
                .done(function(response) {

                })
                .fail(function(xhr) {
                  error = true;
                  $('.error', $dialog).html("Unexpected error occurred while saving message (" + xhr.status + ")");
                  $('.error', $dialog).show();
                });
            }

            if (!error) {
              $dialog.triggerHandler(EVENTS.RSVP_CANCELED);
              app.layout.closeDialog(dialogId);
            }
          })
          .fail(function(xhr) {
            if (xhr.status == 400) {
              var errorText = "";
              $.each(xhr.responseJSON, function(key, value) {
                errorText += value + ", ";
              });
              app.notify(
                {
                  title: "Unable to Cancel RSVP",
                  text: errorText
                }
              );
            }
            else {
              $('.error', $dialog).html("Unexpected error occurred while cancelling RSVP request (" + xhr.status + ")");
              $('.error', $dialog).show();
            }
          });
        return false;
      });
    }

    function initialize() {

      var dialogBindings = {
        'beforeShow' : beforeShow,
        'afterShow' : afterShow,
        'afterHide': afterHide
      };

      app.bindDialog(dialogId, dialogBindings);

      $dialog = $('[layout-id="' + dialogId + '"]');

      $dialog.iCheck({
        checkboxClass: 'icheckbox_minimal',
        radioClass: 'iradio_minimal',
        inheritClass: true
      });

      events();
    }

    this.initialize = initialize;
    this.showDialog = showDialog;
  }

  return this;
})(window,jQuery);