'use strict';

const LOG_REGEXES = {
  zoneChange: /name=(.*) id=(\d+).*to (FRIENDLY|OPPOSING) (.*)$/,
  actionStart: /ACTION_START Entity=\[(.*)\] SubType=(.*) Index=-?\d+ Target=\[(.*)\]/,
  entity: /(?:name=([\w\s]+))? (?:id=(\d+))? zone=(\w+) zonePos=(\d+) cardId=(\w+) player=(\d+)/,
  start: /id=(\d) ChoiceType=MULLIGAN Cancelable=False CountMin=0 CountMax=\d/
};

class LogParser {
  parse(log) {
    const checkers = ['_zoneChange', '_attack', '_start'];
    for (let checker of checkers) {
      let data = this[checker](log);
      if (data !== null) {
        return data;
      }
    }
    return null;
  }

  _zoneChange(log) {
    var parts = LOG_REGEXES.zoneChange.exec(log);
    if (parts !== null) {
      var data = {
        cardName: parts[1],
        cardId: parseInt(parts[2]),
        team: parts[3],
        zone: parts[4]
      };
      return data;
    }
    return null;
  }

  _attack(log) {
    var parts = LOG_REGEXES.actionStart.exec(log);
    if (parts !== null && parts[2] === 'ATTACK') {
      var entity = this._parseEntity(parts[1]);
      var target = this._parseEntity(parts[3]);
      return {
        type: parts[2],
        entity: entity,
        target: target
      };
    }
    return null;
  }

  _start(log) {
    var parts = LOG_REGEXES.start.exec(log);
    if (parts !== null) {
      return {
        id: parts[1]
      };
    }
    return null;
  }

  _parseEntity(entity) {
    var parts = LOG_REGEXES.entity.exec(entity);
    if (parts !== null) {
      return {
        name: parts[1],
        id: parts[2],
        zone: parts[3],
        zonePos: parts[4],
        cardId: parts[5],
        player: parts[6]
      };
    }
    return null;
  }
}

module.exports = LogParser;

@@ -360,16 +360,77 @@
 tMax=%5Cd/
+,%0A  finish: /Entity=(.*) tag=PLAYSTATE value=(LOST%7CWON%7CTIED)/
 %0A%7D;%0A%0Acla
@@ -515,16 +515,27 @@
 '_start'
+, '_finish'
 %5D;%0A    f
@@ -1467,16 +1467,206 @@
 l;%0A  %7D%0A%0A
+  _finish(log) %7B%0A    var parts = LOG_REGEXES.finish.exec(log);%0A    if (parts !== null) %7B%0A      return %7B%0A        name: parts%5B1%5D,%0A        status: parts%5B2%5D%0A      %7D;%0A    %7D%0A    return null;%0A  %7D%0A%0A
   _parse

Add finish parser