'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _printTools = require('../utils/print-tools');

var _hosting = require('../utils/hosting');

var _hosting2 = _interopRequireDefault(_hosting);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

class HostingListCmd {
  constructor(context) {
    this.context = context;
    this.session = context.session;
    this.Socket = context.Socket;
  }

  run([cmd]) {
    var _this = this;

    return _asyncToGenerator(function* () {
      _this.cmd = cmd;

      if (cmd.socket) {
        // TODO: implement Socket-based hosting
      } else {
        return _hosting2.default.list().then(function (hostings) {
          HostingListCmd.printHostings(hostings);
        });
      }
    })();
  }

  static printNoHostingsInfo() {
    (0, _printTools.echo)();
    (0, _printTools.echo)(4)('You don\'t have any hostings!');
    (0, _printTools.echo)(4)(`Type ${_chalk2.default.cyan('syncano-cli hosting add')} to add hosting for your app!`);
    (0, _printTools.echo)();
  }

  static printHostings(hostings = []) {
    if (!hostings.length) {
      HostingListCmd.printNoHostingsInfo();
      process.exit(0);
    }
    (0, _printTools.echo)();
    (0, _printTools.echo)(4)('Your hostings:');
    (0, _printTools.echo)();
    hostings.forEach(HostingListCmd.printHosting.bind(this));
  }

  static printHosting(hosting) {
    const cname = typeof hosting.getCnameURL === 'function' && hosting.getCnameURL();
    (0, _printTools.echo)(4)(`${_chalk2.default.dim('name')}: ${_chalk2.default.cyan(hosting.name)}`);

    if (hosting.existRemotely) {
      (0, _printTools.echo)(4)(`${_chalk2.default.dim('url')}: ${_chalk2.default.cyan(hosting.getURL())}`);
    }

    if (hosting.getCnameURL()) {
      (0, _printTools.echo)(4)(`${_chalk2.default.dim('cname')}: ${_chalk2.default.cyan(cname)}`);
    }

    if (!hosting.existRemotely) {
      (0, _printTools.echo)(4)(`${_chalk2.default.dim('status')}: ${_chalk2.default.red('x')}`);
    } else {
      (0, _printTools.echo)(4)(`${_chalk2.default.dim('sync status')}: ${hosting.isUpToDate ? `${_chalk2.default.green('✓')}` : `${_chalk2.default.red('x')}`}`);
    }

    if (hosting.error) {
      const errorResponses = {
        404: `Type ${_chalk2.default.green(`syncano-cli hosting sync ${hosting.name}`)} to sync your hosting with server.`
      };

      (0, _printTools.echo)();
      (0, _printTools.echo)(4)(errorResponses[hosting.error]);
    }
    (0, _printTools.echo)();
  }
}

exports.default = HostingListCmd;
module.exports = exports['default'];
//# sourceMappingURL=hosting-list.js.map