// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: apiconnect-cli-apps
// US Government Users Restricted Rights - Use, duplication or disclosure
// restricted by GSA ADP Schedule Contract with IBM Corp.

/********************************************************* {COPYRIGHT-TOP} ***
 * IBM Confidential
 * OCO Source Materials
 * 5725-C11, 5725-C12, 7198-8FX, 5725-F70, 5725-F71, 5725-F72, 5725-L30
 *
 * (C) Copyright IBM Corporation 2015  All Rights Reserved.
 *
 * The source code for this program is not published or otherwise
 * divested of its trade secrets, irrespective of what has been
 * deposited with the U.S. Copyright Office.
 ********************************************************* {COPYRIGHT-END} **/
/*eslint brace-style: 0, max-len: 0*/

var fs = require('fs');
var path = require('path');

function helpInfo(helpFile) {
  return fs.readFileSync(path.join(__dirname, '..', 'intl', 'en', helpFile),
    'utf-8');
}

module.exports = {
  name: 'apps',
  version: require('../package.json').version,
  path: __filename,
  isBuiltin: true,
  topics: getTopics,
};

function getTopics() {
  return [ {
    name: 'apps',
    description: 'List the apps in organization',
    commands: [
      {
        command: 'list',
        loginRequired: 'org',
        aliases: [ 'apps' ],
        helpInfo: function() {
          return helpInfo('apic_apps_list_help.txt');
        },
        action: function(opts) {
          return require('./plugin-impl').appsListCmd(opts);
        },
        options: [
          { name: 'all-organizations', rune: '-O', type: 'boolean' },
          { name: 'organization', rune: 'o', default: '$APIC_CONFIG$.org.data.orgs' },
          { name: 'server', rune: 's', default: '$APIC_CONFIG$.org.hostname' },
        ],
      },
      {
        command: 'build',
        aliases: [ 'build' ],
        helpInfo: helpInfo('apic_apps_build_help.txt'),
        options: [
          { name: 'format', rune: 'f' },
          { name: 'no-install', rune: 'n', type: 'boolean' },
          { name: 'scripts', rune: 's', type: 'boolean' },
        ],
        action: function(opts) {
          return require('./plugin-impl').buildCmd(opts);
        },
      },
      {
        command: 'publish',
        loginRequired: 'app',
        action: function(options) {
          return require('./deploy').deployProject(options);
        },
        options: [
          { name: 'organization', rune: 'o', type: 'name', default: '$APIC_CONFIG$.app.data.orgs', required: true },
          { name: 'server', rune: 's', type: 'hostname', default: '$APIC_CONFIG$.app.hostname', required: true },
          { name: 'app', rune: 'a', type: 'name', default: '$APIC_CONFIG$.app.data.apps', required: true },
          { name: 'project-dir', type: 'dir', mustExist: true },
        ],
        helpInfo: helpInfo('apic_apps_publish_help.txt'),
      },
      {
        command: 'set',
        loginRequired: 'app',
        action: function(options) {
          return require('./plugin-impl').appsSet(options);
        },
        options: [
          { name: 'organization', rune: 'o', type: 'name', default: '$APIC_CONFIG$.app.data.orgs', required: true },
          { name: 'server', rune: 's', type: 'hostname', default: '$APIC_CONFIG$.app.hostname', required: true },
          { name: 'app', rune: 'a', type: 'name', default: '$APIC_CONFIG$.app.data.apps', required: true },
          { name: 'status', type: 'status', required: true, validator: function(s) {
            s = s.toLowerCase();
            if (s !== 'online' && s !== 'offline') {
              throw new Error('The value of --status is invalid; it must be online or offline.');
            }
          } },
        ],
        helpInfo: helpInfo('apic_apps_set_help.txt'),
      },
      {
        command: 'get',
        loginRequired: 'app',
        action: function(options) {
          return require('./plugin-impl').appsGet(options);
        },
        options: [
          { name: 'organization', rune: 'o', type: 'name', default: '$APIC_CONFIG$.app.data.orgs', required: true },
          { name: 'server', rune: 's', type: 'hostname', default: '$APIC_CONFIG$.app.hostname', required: true },
          { name: 'app', rune: 'a', type: 'name', default: '$APIC_CONFIG$.app.data.apps', required: true },
        ],
        helpInfo: helpInfo('apic_apps_get_help.txt'),
      },
    ],
  } ];
}
