package hu.advancedweb.scott.runtime;

import gherkin.formatter.model.Result;
import hu.advancedweb.scott.runtime.report.FailureRenderer;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

class ScottCucumberEnricher {

	private List<String> informationList;

    public void scenario() {
        informationList = new ArrayList<>();
    }

    public Result result(Result result) {
        String errorMessage = result.getErrorMessage();
        if (result.getStatus().equals(Result.FAILED)) {
            String errorString = FailureRenderer.render(null, null, result.getError());
            StringBuilder builder = new StringBuilder();
            for (String info : informationList) {
                builder.append(info);
            }
            builder.append(errorString);
            builder.append(getErrorMessage(result.getError()));
            errorMessage = builder.toString();
        } else {
            String errorString = FailureRenderer.render(null, null);
            informationList.add(errorString);
        }
        return new Result(result.getStatus(), result.getDuration(), errorMessage);
    }

    private String getErrorMessage(Throwable error) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        error.printStackTrace(printWriter);
        String msg = stringWriter.getBuffer().toString();
        msg = msg.substring(msg.indexOf("\n"));
        return msg;
    }
}

@@ -312,20 +312,17 @@
 nList;%0A%0A
-    
+%09
 public v
@@ -338,24 +338,18 @@
 rio() %7B%0A
-        
+%09%09
 informat
@@ -377,27 +377,21 @@
 st%3C%3E();%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 public R
@@ -416,32 +416,26 @@
 t result) %7B%0A
-        
+%09%09
 String error
@@ -470,24 +470,18 @@
 sage();%0A
-        
+%09%09
 if (resu
@@ -520,28 +520,19 @@
 LED)) %7B%0A
-            
+%09%09%09
 String e
@@ -599,28 +599,19 @@
 ror());%0A
-            
+%09%09%09
 StringBu
@@ -647,28 +647,19 @@
 lder();%0A
-            
+%09%09%09
 for (Str
@@ -692,24 +692,12 @@
 ) %7B%0A
-                
+%09%09%09%09
 buil
@@ -718,34 +718,16 @@
 o);%0A
-            %7D%0A            
+%09%09%09%7D%0A%09%09%09
 buil
@@ -751,28 +751,19 @@
 tring);%0A
-            
+%09%09%09
 builder.
@@ -806,28 +806,19 @@
 or()));%0A
-            
+%09%09%09
 errorMes
@@ -844,24 +844,18 @@
 ring();%0A
-        
+%09%09
 %7D else %7B
@@ -855,28 +855,19 @@
  else %7B%0A
-            
+%09%09%09
 String e
@@ -919,20 +919,11 @@
 l);%0A
-            
+%09%09%09
 info
@@ -956,26 +956,14 @@
 g);%0A
-        %7D%0A        
+%09%09%7D%0A%09%09
 retu
@@ -1037,19 +1037,13 @@
 e);%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 priv
@@ -1088,24 +1088,18 @@
 rror) %7B%0A
-        
+%09%09
 StringWr
@@ -1138,24 +1138,18 @@
 iter();%0A
-        
+%09%09
 PrintWri
@@ -1197,24 +1197,18 @@
 riter);%0A
-        
+%09%09
 error.pr
@@ -1235,24 +1235,18 @@
 riter);%0A
-        
+%09%09
 String m
@@ -1287,24 +1287,18 @@
 ring();%0A
-        
+%09%09
 msg = ms
@@ -1333,16 +1333,10 @@
 ));%0A
-        
+%09%09
 retu
@@ -1347,12 +1347,10 @@
 sg;%0A
-    %7D
+%09%7D%0A
 %0A%7D%0A

Fix formatting