import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class resultReader {
    public Map<String, Integer> readResult(String resultString){

        HashMap<String, Integer> results = new HashMap<>();

        int sampleCount;
        double packetsPerSecond;
        int avgLatency;
        int minLatency;
        int maxLatency;

        //  404 in 00:00:05 =   79.9/s Avg:    11 Min:     8 Max:   648 Err:     0 (0.00%)
        System.out.println(resultString);
        String[] parts = resultString.split("summary =");
        String summaryLine = parts[1];

        List<String> allMatches = new ArrayList<>();
        Matcher m = Pattern.compile("[0-9]*\\.?[0-9]+").matcher(summaryLine);
        while (m.find()) {
            allMatches.add(m.group());
        }

        sampleCount = Integer.parseInt(allMatches.get(0));
        packetsPerSecond = Double.parseDouble(allMatches.get(4));
        avgLatency = Integer.parseInt(allMatches.get(5));
        minLatency = Integer.parseInt(allMatches.get(6));
        maxLatency = Integer.parseInt(allMatches.get(7));

        results.put("samples", sampleCount);
        results.put("packets per second", (int) packetsPerSecond);
        results.put("average latency", avgLatency);
        results.put("minimum latency", minLatency);
        results.put("maximum latency", maxLatency);

        return results;
    }
}
