(function (context, $) {

  "use strict";

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

    var EVENTS  = context.JK.EVENTS;
    var logger = context.JK.logger;
    var sessionUtils = context.JK.SessionUtils;

    var $dialog = null;
    var wizard = null;
    var $wizardSteps = null;
    var $self = $(this);

    var step1   = new context.JK.StepLoopbackIntro(app, this);
    var step2   = new context.JK.StepLoopbackTest(app, this);
    var step3   = new context.JK.StepLoopbackResult(app, this);

    var STEPS = {
      0: step1,
      1: step2,
      2: step3
    }

    function beforeShow(args) {
      sessionUtils.FTUEPageEnter();
      wizard.onBeforeShow(args);
    }

    function afterHide() {
      wizard.onAfterHide();
      sessionUtils.FTUEPageLeave();
    }

    function closeDialog() {
      wizard.onCloseDialog();
      app.layout.closeDialog('loopback-wizard');
    }

    function setNextState(enabled) {
      wizard.setNextState(enabled);
    }

    function setBackState(enabled) {
      wizard.setBackState(enabled);
    }


    function onStepChanged(e, data) {
      var step = wizard.getCurrentStep();
      var $currentWizardStep = wizard.getCurrentWizardStep();
    }


    function onCanceled() {
      closeDialog();
      return false;
    }

    function onClosed() {
      closeDialog();
      return false;
    }

    function getGearTest() {
      return step2.getGearTest();
    }

    function events() {
      $(wizard).on('step_changed', onStepChanged);
      $(wizard).on('wizard_cancel', onCanceled);
      $(wizard).on('wizard_close', onClosed);
    }

    function getDialog() {
      return $dialog;
    }

    function initialize() {

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

      app.bindDialog('loopback-wizard', dialogBindings);
      $dialog = $('#loopback-wizard-dialog');
      $wizardSteps = $dialog.find('.wizard-step');

      step1.initialize($wizardSteps.filter($('[layout-wizard-step=0]')));
      step2.initialize($wizardSteps.filter($('[layout-wizard-step=1]')));

      wizard = new context.JK.Wizard(app);
      wizard.initialize($dialog, $wizardSteps, STEPS);

      events();
    }

    this.setNextState = setNextState;
    this.setBackState = setBackState;
    this.initialize = initialize;
    this.getGearTest = getGearTest;
    this.getDialog = getDialog;

    return this;

  }
})(window, jQuery);
