'use strict';

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _cliTable = require('cli-table2');

var _cliTable2 = _interopRequireDefault(_cliTable);

var _prettyBytes = require('pretty-bytes');

var _prettyBytes2 = _interopRequireDefault(_prettyBytes);

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 HostingFilesCmd {
  constructor(context) {
    this.context = context;
    this.session = context.session;
  }

  static fillTable(files, table) {
    files.forEach(file => {
      table.push([file.path, { hAlign: 'right', content: (0, _prettyBytes2.default)(file.size) }, { hAlign: 'right', content: file.isSynced ? _chalk2.default.green('✓') : _chalk2.default.red('✗') }]);
    });

    return table;
  }

  static echoResponse(hostingName, files, filledTable, totalSize) {
    if (!files.length) {
      return (0, _printTools.warning)('There are no files in this hosting');
    }

    (0, _printTools.echo)(4)(`Hosting ${_chalk2.default.cyan(hostingName)} has ${_chalk2.default.cyan(files.length)} files:`);
    (0, _printTools.echo)();
    (0, _printTools.echo)(filledTable.toString());
    (0, _printTools.echo)();
    (0, _printTools.echo)(4)(`You have ${files.length} files, ${_chalk2.default.cyan((0, _prettyBytes2.default)(totalSize))} in total.`);
    (0, _printTools.echo)();
  }

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

    return _asyncToGenerator(function* () {
      let hosting = null;
      if (cmd.socket) {
        // TODO: implement Socket-based hosting
      } else {
        hosting = yield _hosting2.default.get(hostingName);
      }

      if (!hosting) {
        (0, _printTools.error)(`There are no hostings configured for the "${_this.attributes.name}" socket!`);
        process.exit();
      }

      const table = new _cliTable2.default({
        head: ['path', { hAlign: 'right', content: 'size' }, { hAlign: 'right', content: 'synced' }],
        colWidths: [40, 30, 10],
        style: { 'padding-left': 4, 'padding-right': 0 },
        chars: {
          top: '',
          'top-mid': '',
          'top-left': '',
          'top-right': '',
          bottom: '',
          'bottom-mid': '',
          'bottom-left': '',
          'bottom-right': '',
          left: '',
          'left-mid': '',
          mid: '',
          'mid-mid': '',
          right: '',
          'right-mid': '',
          middle: ' '
        }
      });

      const files = yield hosting.listFiles();
      const totalSize = _lodash2.default.sum(_lodash2.default.map(files, 'size'));
      const filledTable = HostingFilesCmd.fillTable(files, table);

      HostingFilesCmd.echoResponse(hostingName, files, filledTable, totalSize);
    })();
  }
}
exports.default = HostingFilesCmd;
module.exports = exports['default'];
//# sourceMappingURL=hosting-files.js.map