'use babel';

import * as fs from 'fs';
import _ from 'lodash';
import { parseString } from 'xml2js';


function splitLine(line) {
  return line.replace(/\r\n/, "\n").split("\n");
}

export function parseTestcaseError(testcase) {
  const err = _.get(testcase, 'failure') || _.get(testcase, 'error');
  const testcaseAttr = testcase.$;
  let state = 'passed';
  let filePath = _.get(testcaseAttr, 'file');
  let lineNumber = _.get(testcaseAttr, 'line') - 1;

  if (!err) {
    return {
      filePath,
      lineNumber,
      state
    };
  }

  const messageError = {};
  const errAttr = _.get(err, '0.$');
  const errChar = _.get(err, '0._');

  const type = errAttr.type.toLowerCase();
  if (type.indexOf('skipped') !== -1) {
    state = 'skipped';
  } else if (type.indexOf('incomplete') !== -1) {
    state = 'incomplete';
  } else if (type.indexOf('failed') !== -1) {
    state = 'failed';
  } else {
    state = 'error';
  }

  const msgs = _(splitLine(errChar)).map((msg) => {
    return msg.trim();
  }).filter().value()

  messageError.name = errAttr.type;
  messageError.message = msgs.length === 2 ? msgs[0] : msgs[1];

  const files = _(splitLine(msgs[msgs.length - 1])).map((line) => {
    try {
      const matches = line.match(/(.*):(\d+)/);

      return {
        filePath: matches[1],
        lineNumber: matches[2],
      }
    } catch (e) {
    }
  }).value();

  const file = _.get(files, files.length - 1, {filePath, lineNumber});
  filePath = file.filePath;
  lineNumber = file.lineNumber - 1;
  state = state === 'skipped' ? state : 'failed';

  return {
    filePath,
    lineNumber,
    error: messageError,
    state
  }
}

export function parseTestcase(testcase) {
  const testcaseAttr = testcase.$;

  return Object.assign({
    duration: _.get(testcaseAttr, 'time'),
    title: _.get(testcaseAttr, 'name'),
  }, parseTestcaseError(testcase));
}

export default function parse(filename) {
  let testsuites;
  const messages = [];
  parseString(fs.readFileSync(filename), (error, json) => {
    testsuites = json.testsuites;
    _.forEach(testsuites.testsuite, (testsuite) => {
      // const testsuiteAttrs = testsuite.$;
      _.forEach(_.get(testsuite, 'testcase', []), (testcase) => {
        messages.push(parseTestcase(testcase));
      });
    });
  });

  return messages;
}

@@ -158,20 +158,20 @@
 n/, 
-%22%5Cn%22
+'%5Cn'
 ).split(
 %22%5Cn%22
@@ -170,12 +170,12 @@
 lit(
-%22%5Cn%22
+'%5Cn'
 );%0A%7D
@@ -523,16 +523,17 @@
    state
+,
 %0A    %7D;%0A
@@ -966,51 +966,43 @@
 ar))
-.map((msg) =%3E %7B%0A    return msg
+%0A    .map(line =%3E line
 .trim()
-;%0A  %7D)
+)%0A    
 .fil
@@ -1014,17 +1014,17 @@
 .value()
-%0A
+;
 %0A  messa
@@ -1328,16 +1328,17 @@
 %0A      %7D
+;
 %0A    %7D c
@@ -1356,13 +1356,64 @@
     
-%7D%0A  %7D
+  console.error(e);%0A    %7D%0A%0A    return null;%0A  %7D).filter(
 ).va
@@ -1467,16 +1467,21 @@
 h - 1, %7B
+%0A    
 filePath
@@ -1481,16 +1481,20 @@
 ilePath,
+%0A   
  lineNum
@@ -1496,16 +1496,20 @@
 neNumber
+,%0A  
 %7D);%0A  fi
@@ -1694,20 +1694,22 @@
    state
+,
 %0A  %7D
+;
 %0A%7D%0A%0Aexpo

fix bug