import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.internal.LinkedTreeMap;
import conf.reader.getPropertyValues;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class main {
    public static void main(String[] args) throws Exception {
        getPropertyValues properties = new getPropertyValues();
        Properties props = properties.getPropValues();

        resultReader resultReader = new resultReader();

        attachmentGenerator attachmentGenerator = new attachmentGenerator();

        Gson gson = new Gson();
        JsonElement json = gson.fromJson(new FileReader("src/main/resources/tests.json"), JsonElement.class);
        String result = gson.toJson(json);

        LinkedTreeMap testNames = gson.fromJson(result , LinkedTreeMap.class);

        for (Object i : testNames.keySet()) {
            LinkedTreeMap tests = gson.fromJson(testNames.get(i.toString()).toString(), LinkedTreeMap.class);

            for (Object o : tests.keySet()) {
                LinkedTreeMap testDetails = gson.fromJson(tests.get(o.toString()).toString(), LinkedTreeMap.class);
                Integer threads = (int) Double.parseDouble(testDetails.get("threads").toString());
                Integer duration = (int) Double.parseDouble(testDetails.get("duration").toString());
                Integer size;
                try {
                    size = (int) Double.parseDouble(testDetails.get("size").toString());
                } catch (Exception nosize) {
                    size = 0;
                }

                String testName = i.toString();


                if (testName.toUpperCase().contains("SEND")){
                    attachmentGenerator.cleanUpAttachment();
                    attachmentGenerator.generateAttachment(size);
                }

                Runtime rt = Runtime.getRuntime();
                Process pr = rt.exec(jmeterRunner.generateJmeterCommand("TestJMX\\" + testName + ".jmx", threads, duration, size, props));

                BufferedReader stdInput = new BufferedReader(new
                        InputStreamReader(pr.getInputStream()));

                BufferedReader stdError = new BufferedReader(new
                        InputStreamReader(pr.getErrorStream()));


                String s = null;

                StringBuilder sb = new StringBuilder();

                while ((s = stdInput.readLine()) != null) {

                    sb.append(s);
                }


                attachmentGenerator.cleanUpAttachment();

                resultReader.readResult(sb.toString());

                dataHandler.writeResult(testName, Double.parseDouble(String.valueOf(resultReader.readResult(sb.toString()).get("average latency"))), "Latency", threads, duration, size);
                dataHandler.writeResult(testName, Double.parseDouble(String.valueOf(resultReader.readResult(sb.toString()).get("packets per second")*60)), "Sample", threads, duration, size);

                // Read any errors from the attempted command
                System.out.println("error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                stdInput.close();
                stdError.close();

            }
        }


    }
}
