'use strict';

const bus = require('./message-bus');

const ItemFormatter = require('./item-formatter');
const style = require('./style');

class DebugEventLogger {
  static subscribe() {
    bus.eventChannel.subscribe('#', (data, envelope) => {
      switch (envelope.topic) {
        case 'game.started':
        case 'game.help-requested':
        case 'game.stop-requested':
        case 'game.stopped':
          log(`${envelope.topic}`);
          break;

        case 'player.location.moved':
          log(`${envelope.topic} => ${data.direction} from ${formatNode(data.previousNode)} to ${formatNode(data.currentNode)}`);
          break;

        case 'player.location.teleported':
          log(`${envelope.topic} => from ${formatNode(data.previousNode)} to ${formatNode(data.currentNode)}`);
          break;

        case 'player.location.move-blocked':
          log(`${envelope.topic} => trying to move ${data.direction} from ${formatNode(data.currentNode)}`);
          break;

        case 'player.inventory.added':
          for (const listItem of data.deltas) {
            log(`${envelope.topic} => ${ItemFormatter.formatDebugItem(listItem.item, listItem.count)}`);
          }
          break;

        case 'player.inventory.item-equipped':
          log(`${envelope.topic} => ${data.item.name}(${data.item.id})`);
          break;

        case 'item.conjured':
          log(`${envelope.topic} => ${ItemFormatter.formatDebugItem(data.item, data.count)} to ${data.target}`);
          break;

        case 'error':
          log(`${envelope.topic} => ${data}`);
          break;

        case 'player.inventory.list-requested':
          log(`${envelope.topic} => ${data.items.length} stacks in inventory`);
          break;

        default:
          log(`${envelope.topic} => NOT SET UP FOR DEBUGGING`);
      }
    });
  }
}

function log(text) {
  console.log(style.debug(text));
}

function formatNode(node) {
  return `${node.name}(${node.id})[${node.location.x},${node.location.y},${node.location.z}]`;
}

module.exports = DebugEventLogger;

@@ -186,24 +186,284 @@
 scribe() %7B%0D%0A
+    bus.clientCommandChannel.subscribe('#', (data, envelope) =%3E %7B%0D%0A      log(%60$%7Benvelope.channel%7D:$%7Benvelope.topic%7D%60);%0D%0A    %7D);%0D%0A%0D%0A    bus.clientEventChannel.subscribe('#', (data, envelope) =%3E %7B%0D%0A      log(%60$%7Benvelope.channel%7D:$%7Benvelope.topic%7D%60);%0D%0A    %7D);%0D%0A%0D%0A
     bus.even
@@ -694,32 +694,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D%60);%0D%0A     
@@ -810,32 +810,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7Bdata
@@ -1025,32 +1025,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E from $
@@ -1224,32 +1224,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E trying
@@ -1465,32 +1465,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7BItem
@@ -1670,32 +1670,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7Bdata
@@ -1816,32 +1816,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7BItem
@@ -1993,32 +1993,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7Bdata
@@ -2130,32 +2130,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E $%7Bdata
@@ -2269,32 +2269,52 @@
 log(%60$%7Benvelope.
+channel%7D:$%7Benvelope.
 topic%7D =%3E NOT SE

Add client channels to debug logger, reformat msgs