(function (context, $) {

  "use strict";

  context.JK = context.JK || {};

  context.JK.ClientPreferencesDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var dialogId = '#client-preferences-dialog';
    var $dialog = null;
    var $autoStartField = null;
    var $autoStartInput = null;
    var $useStaticPortField = null;
    var $useStaticPortInput = null;
    var $staticPortField = null;
    var $staticPortInput = null;
    var $btnSave = null;

    var beforeValues = {}

    function validate() {

      var staticPort = $staticPortInput.val();

      staticPort = new Number(staticPort);

      console.log("staticPort", staticPort)
      if(context._.isNaN(staticPort)) {
        app.layout.notify({title: 'No Settings Have Been Saved!', text: 'Please enter a number from 1026-49150.'})
        return false;
      }

      /**
      if (staticPort % 2 == 1) {
        app.layout.notify({title: 'No Settings Have Been Saved!', text: 'Please pick an even port number.'})
        return false;
      }
       */

      if (staticPort < 1026 || staticPort >= 65525) {
        app.layout.notify({title: 'No Settings Have Been Saved!', text: 'Please pick a port from 1026 to 65524.'})
        return false;
      }

      return true;
    }

    function events() {
      context.JK.checkbox($autoStartInput);
      context.JK.checkbox($useStaticPortInput);

      $btnSave.click(function() {

        if (!validate()) {
          return false;
        }

        var autostart = $autoStartField.find('.icheckbox_minimal').is('.checked');
        context.jamClient.SetAutoStart(autostart);

        var useStaticPort = $useStaticPortField.find('.icheckbox_minimal').is('.checked');
        context.jamClient.SetUseStaticPort(useStaticPort);

        var staticPort = new Number($staticPortInput.val());
        context.jamClient.SetStaticPort(staticPort);

        app.layout.closeDialog('client-preferences-dialog')
        context.jamClient.SaveSettings();

        logger.debug("New Client Settings", {autostart: autostart, useStaticPort: useStaticPort, staticPort: staticPort})
        if ((beforeValues.useStaticPort != useStaticPort) || (beforeValues.staticPort != staticPort)) {
          context.JK.Banner.showYesNo({
            title: "Please Restart",
            html: "The changes you made won't take effect until you restart JamKazam. Restart now?",
            yes: function() {
              context.jamClient.RestartApplication();
            },
            no : function() {
              context.JK.Banner.hide();
            }
          })
        }
        return false;
      })
    }

    function beforeShow() {
      var autostart = context.jamClient.GetAutoStart();
      autostart ? $autoStartInput.iCheck('check') : $autoStartInput.iCheck('uncheck');

      var useStaticPort = context.jamClient.GetUseStaticPort();
      useStaticPort ? $useStaticPortInput.iCheck('check') : $useStaticPortInput.iCheck('uncheck');

      var staticPort = context.jamClient.GetStaticPort();

      $staticPortInput.val(staticPort);

      beforeValues = {autostart: autostart, useStaticPort: useStaticPort, staticPort: staticPort}
      logger.debug("Client Settings:", beforeValues)
    }

    function afterHide() {

    }

    function initialize() {

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

      app.bindDialog('client-preferences-dialog', dialogBindings);

      $dialog = $(dialogId);
      $autoStartField = $dialog.find('.field[data-purpose="autostart"]')
      $autoStartInput = $dialog.find('input[name="autostart"]')
      $useStaticPortField = $dialog.find('.field[data-purpose="use-static-port"]')
      $useStaticPortInput = $dialog.find('input[name="use-static-port"]')
      $staticPortField = $dialog.find('.field[data-purpose="static-port"]')
      $staticPortInput = $dialog.find('input[name="static-port"]')
      $btnSave = $dialog.find('.btnSave')

      events();
    }

    this.initialize = initialize;

  }
})(window, jQuery);
