const RestClient = require('../../api/RestClient');
const { subcommand, println } = require('../util');


module.exports = {
  command: 'status [newStatus]',
  description: 'Get or set the status of the local node. ' + 'If `newStatus` is not given, returns the current status. ' + '`newStatus` must be one of: online, offline, public\n',
  handler: subcommand(opts => {
    const { client, newStatus } = opts;

    if (!newStatus) {
      return client.getStatus().then(println);
    }

    let status;
    switch (newStatus) {
      case 'online':
      case 'offline':
      case 'public':
        status = newStatus;
        break;
      default:
        return Promise.reject(new Error(`Cannot set status to ${ newStatus }. Must be one of: online, offline, public`));
    }
    return client.setStatus(status).then(returnedStatus => {
      println(`status set to ${ returnedStatus }`);
    });
  })
};