define([
    'freeipa/phases',
    'freeipa/host',
    'freeipa/stageuser',
    'freeipa/user'],
    function(phases, host_mod, stageuser_mod, user_mod) {

// helper function
function get_item(array, attr, value) {

    for (var i=0,l=array.length; i<l; i++) {
        if (array[i][attr] === value) return array[i];
    }
    return null;
}

function find_index(array, attr, value) {

  for (var i=0,l=array.length; i<l; i++) {
    if (array[i][attr] === value) return i;
  }
  return null;
}

var astrocustom_plugin = {};

// adds 'puppetVar' field into host details facet
astrocustom_plugin.add_puppetvar_pre_op = function() {

    var facet = get_item(host_mod.entity_spec.facets, '$type', 'details');
    var section = get_item(facet.sections, 'name', 'details');
    section.fields.push({
      name: 'owner',
      $type: 'entity_select',
      other_entity: 'user',
      other_field: 'uid',
      label: 'Primary User',
    },
    {
      name: 'puppetvar',
      $type: 'multivalued',
      label: 'Puppet Variables',
    });

    // take 'userclass' off the list
    var uc = section.fields.indexOf('userclass');
    section.fields.splice(uc, 1);

    return true;
};

phases.on('customization', astrocustom_plugin.add_puppetvar_pre_op);

// adds things we want on the adder page
astrocustom_plugin.add_host_pre_op = function() {

  var section = get_item(host_mod.entity_spec.adder_dialog.sections, 'name', 'other');
  section.fields.push({
    name: 'nshostlocation',
    label: 'Location',
  },
  {
    name: 'owner',
    $type: 'entity_select',
    other_entity: 'user',
    other_field: 'uid',
    label: 'Primary User',
  },
  {
    name: 'puppetvar',
    $type: 'multivalued',
    label: 'Puppet Variables',
  },
  {
    $type: 'multivalued',
    name: 'macaddress',
    flags: ['w_if_no_aci'],
  });

  // take 'userclass' off the list
  var uc = section.fields.indexOf('userclass');
  section.fields.splice(uc, 1);

  var f = find_index(section.fields, 'name', 'force');
  section.fields.splice(f, 1);

  // This works as a warning message - leaving it here as an example but
  // commented out
  // host_mod.entity_spec.adder_dialog.sections.push({
  //  name: 'hostgroupwarning',
  //  label: 'Remember to add to appropriate host/net groups',
  //  fields: [],
  // });

  return true;
};

phases.on('customization', astrocustom_plugin.add_host_pre_op);

astrocustom_plugin.add_stageuser_pre_op = function() {

  var section = stageuser_mod.stageuser_spec.adder_dialog.sections[0];
  section.fields.push({
    name: 'krbprincipalexpiration',
    label: 'Account expiration',
    required: true,
    $type: 'datetime',
  },
  {
    name: 'manager',
    label: 'Requested by',
    required: true,
    $type: 'entity_select',
    other_entity: 'user',
    other_field: 'uid',
  },
  {
    name: 'employeetype',
    label: 'User tier',
    required: true,
    $type: 'select',
    options: [
      { label: 'Primary ($200/mo)', value: 1 },
      { label: 'Secondary ($25/mo)', value: 2 },
      { label: 'Tertiary ($0/mo)', value: 3 },
    ],
  },
  {
    name: 'loginshell',
    $type: 'select',
    options: [
      { label: 'bash', value: '/bin/bash' },
      { label: 'tcsh', value: '/bin/tcsh' },
      { label: 'RESTRICTED', value: '/usr/peyton/bin/rssh' },
    ],
  });

  // rename Class to Chart string
  var uc = section.fields.indexOf('userclass');
  section.fields.splice(uc, 1, {
    name: 'userclass',
    label: 'Chart string',
    required: true,
  });

  return true;
};

phases.on('customization', astrocustom_plugin.add_stageuser_pre_op);

astrocustom_plugin.mod_stageuser_pre_op = function() {

  var facet = get_item(stageuser_mod.stageuser_spec.facets, '$type', 'details');
  var id_section = get_item(facet.sections, 'name', 'identity');
  var acct_section = get_item(facet.sections, 'name', 'account');
  var empl_section = get_item(facet.sections, 'name', 'employee');

  id_section.fields.splice(find_index(id_section.fields, 'name', 'userclass'), 1, {
    name: 'userclass',
    label: 'Chart string',
    required: true,
  });

  acct_section.fields.splice(acct_section.fields.indexOf('krbprincipalname'), 1);

  acct_section.fields.splice(find_index(acct_section.fields, 'name', 'krbprincipalexpiration'), 1, {
    name: 'krbprincipalexpiration',
    label: 'Account expiration',
    required: true,
    $type: 'datetime',
  });

  // This should remove 'User authentication types', 'RADIUS proxy config' and
  // 'RADIUS proxy username'
  acct_section.fields.splice(find_index(acct_section.fields, 'name', 'ipauserauthtype'), 3);

  empl_section.fields.splice(find_index(empl_section.fields, 'name', 'manager'), 1, {
    name: 'manager',
    label: 'Requested by',
    required: true,
    $type: 'entity_select',
    other_entity: 'user',
    other_field: 'uid',
  });

  empl_section.fields.splice(empl_section.fields.indexOf('employeetype'), 1, {
    name: 'employeetype',
    label: 'User tier',
    required: true,
    $type: 'select',
    options: [
      { label: 'Primary ($200/mo)', value: 1 },
      { label: 'Secondary ($25/mo)', value: 2 },
      { label: 'Tertiary ($0/mo)', value: 3 },
    ],
  });

  facet.sections.splice(find_index(facet.sections, 'name', 'misc'), 1);

  return true;
};

phases.on('customization', astrocustom_plugin.mod_stageuser_pre_op);

// Now for the regular user adder pages

astrocustom_plugin.add_user_pre_op = function() {

  var section = user_mod.entity_spec.adder_dialog.sections[0];
  section.fields.push({
    name: 'krbprincipalexpiration',
    label: 'Account expiration',
    required: true,
    $type: 'datetime',
  },
  {
    name: 'manager',
    label: 'Requested by',
    required: true,
    $type: 'entity_select',
    other_entity: 'user',
    other_field: 'uid',
  },
  {
    name: 'employeetype',
    label: 'User tier',
    required: true,
    $type: 'select',
    options: [
      { label: 'Primary ($200/mo)', value: 1 },
      { label: 'Secondary ($25/mo)', value: 2 },
      { label: 'Tertiary ($0/mo)', value: 3 },
    ],
  },
  {
    name: 'loginshell',
    $type: 'select',
    options: [
      { label: 'bash', value: '/bin/bash' },
      { label: 'tcsh', value: '/bin/tcsh' },
      { label: 'RESTRICTED', value: '/usr/peyton/bin/rssh' },
    ],
  });

  // rename Class to Chart string
  var uc = section.fields.indexOf('userclass');
  section.fields.splice(uc, 1, {
    name: 'userclass',
    label: 'Chart string',
    required: true,
  });

  return true;
};

phases.on('customization', astrocustom_plugin.add_user_pre_op);

astrocustom_plugin.mod_user_pre_op = function() {

  var facet = get_item(user_mod.entity_spec.facets, '$type', 'details');
  var id_section = get_item(facet.sections, 'name', 'identity');
  var acct_section = get_item(facet.sections, 'name', 'account');
  var empl_section = get_item(facet.sections, 'name', 'employee');

  id_section.fields.splice(find_index(id_section.fields, 'name', 'userclass'), 1, {
    name: 'userclass',
    label: 'Chart string',
    required: true,
  });

  acct_section.fields.splice(acct_section.fields.indexOf('krbprincipalname'), 1);

  acct_section.fields.splice(find_index(acct_section.fields, 'name', 'krbprincipalexpiration'), 1, {
    name: 'krbprincipalexpiration',
    label: 'Account expiration',
    required: true,
    $type: 'datetime',
  });

  // This should remove 'User authentication types', 'RADIUS proxy config' and
  // 'RADIUS proxy username'
  acct_section.fields.splice(find_index(acct_section.fields, 'name', 'ipauserauthtype'), 3);

  empl_section.fields.splice(find_index(empl_section.fields, 'name', 'manager'), 1, {
    name: 'manager',
    label: 'Requested by',
    required: true,
    $type: 'entity_select',
    other_entity: 'user',
    other_field: 'uid',
  });

  empl_section.fields.splice(empl_section.fields.indexOf('employeetype'), 1, {
    name: 'employeetype',
    label: 'User tier',
    required: true,
    $type: 'select',
    options: [
      { label: 'Primary ($200/mo)', value: 1 },
      { label: 'Secondary ($25/mo)', value: 2 },
      { label: 'Tertiary ($0/mo)', value: 3 },
    ],
  });

  facet.sections.splice(find_index(facet.sections, 'name', 'misc'), 1);

  return true;
};

phases.on('customization', astrocustom_plugin.mod_user_pre_op);

return astrocustom_plugin;

});