package org.apache.poi.benchmark.results;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Read all json-files and prepare a map of benchmarks with their
 * daily results.
 */
public class Results {
    private static final ObjectMapper mapper = new ObjectMapper();

    private final Map<String, Map<String, Double>> values = new TreeMap<>();

    /**
     * Read results from the given list of json-files.
     *
     * @param files An array of .json files with results
     * @return The highest found date
     * @throws IOException If reading a file fails
     */
    public String readFiles(File[] files) throws IOException {
        String maxDateStr = null;
        for(File file : files) {
            String date = file.getName().replace("-results.json", "");

            //noinspection unchecked
            Map<String,Object>[] userData = mapper.readValue(file, Map[].class);
            for(Map<String,Object> data : userData) {
                //noinspection unchecked
                Map<String, Object> primaryMetric = (Map<String, Object>) data.get("primaryMetric");

                String benchmark = data.get("benchmark").toString();
                double value = Double.parseDouble(primaryMetric.get("score").toString());
                //System.out.println("File " + file + ": Found: " + benchmark + ": " + value);

                Map<String,Double> benchmarkValues = values.get(benchmark);
                if(benchmarkValues == null) {
                    benchmarkValues = new HashMap<>();
                }
                benchmarkValues.put(date, value);
                values.put(benchmark, benchmarkValues);

                if(maxDateStr == null || maxDateStr.compareTo(date) <= 0) {
                    maxDateStr = date;
                }
            }
        }

        Preconditions.checkNotNull(maxDateStr, "Should have a max date now!");

        return maxDateStr;
    }

    public Map<String, Map<String, Double>> getValues() {
        return values;
    }
}

@@ -945,28 +945,29 @@
 , %22%22);%0A%0A
-            
+%09%09%09try %7B%0A%09%09%09%09
 //noinsp
@@ -983,28 +983,20 @@
 checked%0A
-            
+%09%09%09%09
 Map%3CStri
@@ -994,24 +994,25 @@
 %09Map%3CString,
+ 
 Object%3E%5B%5D us
@@ -1061,23 +1061,16 @@
 s);%0A
-            
+%09%09%09%09
 for
+ 
 (Map
@@ -1077,16 +1077,17 @@
 %3CString,
+ 
 Object%3E 
@@ -1105,32 +1105,21 @@
 Data) %7B%0A
-                
+%09%09%09%09%09
 //noinsp
@@ -1135,32 +1135,21 @@
 checked%0A
-                
+%09%09%09%09%09
 Map%3CStri
@@ -1226,32 +1226,21 @@
 ric%22);%0A%0A
-                
+%09%09%09%09%09
 String b
@@ -1284,32 +1284,21 @@
 ring();%0A
-                
+%09%09%09%09%09
 double v
@@ -1363,32 +1363,21 @@
 ing());%0A
-                
+%09%09%09%09%09
 //System
@@ -1448,32 +1448,21 @@
 alue);%0A%0A
-                
+%09%09%09%09%09
 Map%3CStri
@@ -1464,16 +1464,17 @@
 %3CString,
+ 
 Double%3E 
@@ -1514,34 +1514,24 @@
 hmark);%0A
-                
+%09%09%09%09%09
 if
+ 
 (benchma
@@ -1550,36 +1550,22 @@
 null) %7B%0A
-                    
+%09%09%09%09%09%09
 benchmar
@@ -1595,42 +1595,20 @@
 ();%0A
-                %7D%0A                
+%09%09%09%09%09%7D%0A%09%09%09%09%09
 benc
@@ -1637,32 +1637,21 @@
 value);%0A
-                
+%09%09%09%09%09
 values.p
@@ -1687,26 +1687,16 @@
 );%0A%0A
-                
+%09%09%09%09%09
 if
+ 
 (max
@@ -1753,77 +1753,140 @@
 ) %7B%0A
-                    maxDateStr = date;%0A                %7D%0A            
+%09%09%09%09%09%09maxDateStr = date;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09throw new IOException(%22While handling file: %22 + file, e);%0A%09%09%09
 %7D%0A  

Add information when parsing a result-file fails