// Generated by CoffeeScript 1.8.0
var init_errors, series;

series = require('../src/series');

init_errors = require('./errors');

module.exports = function(tugboat, names) {
  return tugboat.init(function(errors) {
    if (errors != null) {
      return init_errors(errors);
    }
    return tugboat.ps(function(err, groups) {
      var created, group, name, output, postfix, r, running, service, servicename, status, total, _, _i, _len, _ref, _ref1, _results;
      if (err != null) {
        console.error();
        console.error('  docker is down'.red);
        console.error();
        process.exit(1);
      }
      if (Object.keys(groups).length === 0) {
        console.log();
        console.log('  There are no groups defined in this directory or running containers that match'.magenta);
        console.log();
        return;
      }
      names = names.map(function(g) {
        return g.replace('.yml', '');
      });
      if (names.length === 0) {
        console.log();
        for (_ in groups) {
          group = groups[_];
          name = group.name.blue;
          while (name.length < 36) {
            name += ' ';
          }
          postfix = '';
          if (!group.isknown) {
            postfix += ' (unknown)'.magenta;
          }
          total = 0;
          created = 0;
          running = 0;
          _ref = group.services;
          for (_ in _ref) {
            service = _ref[_];
            total++;
            if (service.containers.length !== 0) {
              created++;
              r = service.containers.filter(function(d) {
                return d.inspect.State.Running;
              }).length;
              if (r === service.containers.length) {
                running++;
              }
            }
          }
          if (running === total) {
            console.log("  " + name + " " + ("" + total + " up").green + postfix);
            continue;
          }
          if (created === 0) {
            console.log("  " + name + " " + ("" + total + " uncreated").magenta + postfix);
            continue;
          }
          if (created === total && running === 0) {
            console.log("  " + name + " " + ("" + total + " stopped").red + postfix);
            continue;
          }
          output = "  " + name;
          if (running > 0) {
            output += " " + (running.toString().green);
            output += ' running';
          }
          if (created - running > 0) {
            output += (" " + (created - running)).red;
            output += ' stopped';
          }
          if (total - created > 0) {
            output += (" " + (total - created)).magenta;
            output += ' uncreated';
          }
          output += postfix;
          console.log(output);
        }
        console.log();
        return;
      }
      console.log();
      _results = [];
      for (_i = 0, _len = names.length; _i < _len; _i++) {
        name = names[_i];
        if (groups[name] == null) {
          console.error(("  The group '" + name + "' is not available in this directory").red);
          console.error("  and has no created containers".red);
          console.error();
          continue;
        }
        group = groups[name];
        if (group.isknown) {
          console.log("  " + group.name.blue + " services:");
        } else {
          console.log("  " + group.name.blue + " services: " + '(unknown)'.magenta);
        }
        console.log();
        _ref1 = group.services;
        for (_ in _ref1) {
          service = _ref1[_];
          servicename = service.name;
          if (service.pname != null) {
            servicename = service.pname;
          }
          while (servicename.length < 32) {
            servicename += ' ';
          }
          servicename = servicename.cyan;
          status = '-'.magenta;
          if (service.containers.length > 0) {
            r = service.containers.filter(function(d) {
              return d.inspect.State.Running;
            }).length;
            if (r !== service.containers.length) {
              status = 'stopped'.red;
            } else {
              status = service.containers.map(function(c) {
                return c.inspect.NetworkSettings.IPAddress.toString().blue;
              }).join(', ');
            }
          }
          if (!service.isknown) {
            status += ' (unknown)'.magenta;
          }
          console.log("  " + servicename + " " + status);
          continue;
        }
        _results.push(console.log());
      }
      return _results;
    });
  });
};
