import * as _ from 'lodash';

export const SIZE_BYTE = 1;
export const SIZE_SHORT = 2;
export const SIZE_INT = 4;

export class NiceBuffer {
  constructor(buffer) {
    this.pos = 0;
    this.buffer = buffer;
    this.lengthMap = [];
    this.lengthMap[SIZE_INT] = 'int';
    this.lengthMap[SIZE_SHORT] = 'short';
    this.lengthMap[SIZE_BYTE] = 'byte';
  }

  move(len) {
    let current = this.pos;
    this.pos += len;
    return current;
  }

  read(len) {
    if ( ! _.has(this.lengthMap, len)) {
      return this.slice(len);
    }

    let method = this.lengthMap[len];
    return this[method]();
  }

  // buff.readStruct([
  //   [ 'access_flags', SIZE_SHORT ],
  //   [ 'name_index', SIZE_SHORT ],
  //   [ 'descriptor_index', SIZE_SHORT ],
  //   [ 'attribute_count', SIZE_SHORT ],
  //   [ '@attribute_info|attribute_count', {
  //     struct: [
  //       [ 'attribute_name_index', SIZE_SHORT ],
  //       [ 'attribute_length', SIZE_SHORT ],
  //       [ '$info', 'attribute_length' ],
  //     ]
  //   }],
  // ]);

  readStruct(struct) {
    let instance = {};

    _.each(struct, (obj) => {
      let [name, value] = obj;

      if (name.startsWith('@')) {
        // need to for-loop it up
        let items = [];
        let [key, lengthAttr] = name.substring(1).split('|');
        let len = instance[lengthAttr];

        for (let idx = 0; idx < len; idx++) {
          items.push(this.readStruct(value.struct));
        }

        instance[key] = items;
      } else if (name.startsWith('$')) {
        // read variable amount of bytes where the length has already
        // been stored in `instance`.
        let key = name.substring(1);
        let len = instance[value];
        instance[key] = this.slice(len);
      } else {
        let length = value;
        instance[name] = this.read(length);
      }
    });

    return instance;
  }

  short() {
    return this.buffer.readUInt16BE(this.move(SIZE_SHORT));
  }

  byte() {
    return this.buffer.readUInt8(this.move(SIZE_BYTE));
  }

  int() {
    return this.buffer.readUInt32BE(this.move(SIZE_INT));
  }

  slice(len) {
    let end = this.pos + len;
    return this.buffer.slice(this.move(len), end);
  }
}

@@ -606,431 +606,8 @@
  %7D%0A%0A
-  // buff.readStruct(%5B%0A  //   %5B 'access_flags', SIZE_SHORT %5D,%0A  //   %5B 'name_index', SIZE_SHORT %5D,%0A  //   %5B 'descriptor_index', SIZE_SHORT %5D,%0A  //   %5B 'attribute_count', SIZE_SHORT %5D,%0A  //   %5B '@attribute_info%7Cattribute_count', %7B%0A  //     struct: %5B%0A  //       %5B 'attribute_name_index', SIZE_SHORT %5D,%0A  //       %5B 'attribute_length', SIZE_SHORT %5D,%0A  //       %5B '$info', 'attribute_length' %5D,%0A  //     %5D%0A  //   %7D%5D,%0A  // %5D);%0A%0A
   re
@@ -1661,16 +1661,334 @@
 );%0A  %7D%0A%0A
+  write(bytes) %7B%0A    bytes.forEach((byte) =%3E this.writeByte(byte));%0A  %7D%0A%0A  writeByte(val) %7B%0A    this.buffer.writeUInt8LE(val, this.move(SIZE_BYTE));%0A  %7D%0A%0A  writeShort(val) %7B%0A    this.buffer.writeUInt16LE(val, this.move(SIZE_SHORT));%0A  %7D%0A%0A  writeInt(val) %7B%0A    this.buffer.writeUInt32LE(val, this.move(SIZE_INT));%0A  %7D%0A%0A
   slice(

add write methods to NiceBuffer