'use strict';

var Actions = require('../actions');

/**
 * Change the kerberos principal.
 *
 * @param {String} principal - The principal.
 */
function onKerberosPrincipalChanged(principal) {
  this.state.currentConnection.kerberos_principal = principal;
  this.trigger(this.state);
}

/**
 * Change the kerberos password.
 *
 * @param {String} password - The password.
 */
function onKerberosPasswordChanged(password) {
  this.state.currentConnection.kerberos_password = password;
  this.trigger(this.state);
}

/**
 * Change the kerberos service name.
 *
 * @param {String} name - The service name.
 */
function onKerberosServiceNameChanged(name) {
  this.state.currentConnection.kerberos_service_name = name;
  this.trigger(this.state);
}

function onCnameToggle() {
  var connection = this.state.currentConnection;
  connection.kerberos_canonicalize_hostname = !connection.kerberos_canonicalize_hostname;
  this.trigger(this.state);
}

/**
 * When extending the connect plugin, we bind the instance of the store
 * to our functions here, so when our actions are called the changes
 * are happening to the connection model in the connect plugin's store.
 *
 * @param {Store} store - The connect plugin store instance.
 */
function extendAuthentication(store) {
  var principal = onKerberosPrincipalChanged.bind(store);
  var password = onKerberosPasswordChanged.bind(store);
  var serviceName = onKerberosServiceNameChanged.bind(store);
  var cName = onCnameToggle.bind(store);

  Actions.onKerberosPrincipalChanged.listen(principal);
  Actions.onKerberosPasswordChanged.listen(password);
  Actions.onKerberosServiceNameChanged.listen(serviceName);
  Actions.onCnameToggle.listen(cName);
}

module.exports = extendAuthentication;