package fr.inria.spirals.repairnator.serializer;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import fr.inria.jtravis.entities.Build;
import fr.inria.spirals.repairnator.BuildToBeInspected;
import fr.inria.spirals.repairnator.Utils;
import fr.inria.spirals.repairnator.serializer.engines.SerializedData;
import fr.inria.spirals.repairnator.serializer.engines.SerializerEngine;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by fernanda on 13/03/17.
 */
public class ScannerDetailedDataSerializer extends ProcessSerializer {

    private List<BuildToBeInspected> buildsToBeInspected;

    public ScannerDetailedDataSerializer(List<SerializerEngine> engines, List<BuildToBeInspected> buildsToBeInspected) {
        super(engines, SerializerType.DETAILEDDATA);
        this.buildsToBeInspected = buildsToBeInspected;
    }

    private List<Object> serializeAsList(BuildToBeInspected buildToBeInspected) {
        Build build = buildToBeInspected.getBuggyBuild();

        Build previousBuild = buildToBeInspected.getBuggyBuild();
        int previousBuildId = (previousBuild != null) ? previousBuild.getId() : -1;
        String committerEmail = (build.getCommit().getCommitterEmail() != null) ? build.getCommit().getCommitterEmail() : "-";

        List<Object> dataCol = new ArrayList<Object>();

        Date date = new Date();
        dataCol.add(build.getId() + "");
        dataCol.add(previousBuildId + "");
        dataCol.add(buildToBeInspected.getStatus().toString());
        dataCol.add(build.getRepository().getSlug());
        dataCol.add(Utils.formatCompleteDate(date));
        dataCol.add(Utils.formatOnlyDay(date));
        dataCol.add(Utils.getHostname());
        dataCol.add(Utils.getTravisUrl(build.getId(), build.getRepository().getSlug()));
        dataCol.add(Utils.getTravisUrl(previousBuildId, build.getRepository().getSlug()));
        dataCol.add(committerEmail);
        dataCol.add(buildToBeInspected.getRunId());

        return dataCol;
    }

    private JsonElement serializeAsJson(BuildToBeInspected buildToBeInspected) {
        JsonObject result = new JsonObject();

        Build build = buildToBeInspected.getBuggyBuild();

        Build previousBuild = buildToBeInspected.getBuggyBuild();
        int previousBuildId = (previousBuild != null) ? previousBuild.getId() : -1;
        String committerEmail = (build.getCommit().getCommitterEmail() != null) ? build.getCommit().getCommitterEmail() : "-";
        Date date = new Date();

        result.addProperty("buildId", build.getId());
        result.addProperty("previousBuildId", previousBuildId);
        result.addProperty("scannedStatus", buildToBeInspected.getStatus().name());
        result.addProperty("repositoryName", build.getRepository().getSlug());
        result.addProperty("dateScannedStr", Utils.formatCompleteDate(date));
        this.addDate(result, "dateScanned", date);
        result.addProperty("dayScanned", Utils.formatOnlyDay(date));
        result.addProperty("hostname", Utils.getHostname());
        result.addProperty("travisBuildUrl", Utils.getTravisUrl(build.getId(), build.getRepository().getSlug()));
        result.addProperty("travisPreviousBuildUrl", Utils.getTravisUrl(previousBuildId, build.getRepository().getSlug()));
        result.addProperty("comitterEmail", committerEmail);
        result.addProperty("runId", buildToBeInspected.getRunId());

        return result;
    }

    public void serialize() {
        if (!this.buildsToBeInspected.isEmpty()) {
            List<SerializedData> allData = new ArrayList<>();

            for (BuildToBeInspected buildToBeInspected : this.buildsToBeInspected) {
                allData.add(new SerializedData(this.serializeAsList(buildToBeInspected), this.serializeAsJson(buildToBeInspected)));
            }

            for (SerializerEngine engine : this.getEngines()) {
                engine.serialize(allData, this.getType());
            }
        }
    }

}

@@ -959,32 +959,89 @@
 oBeInspected) %7B%0A
+        List%3CObject%3E dataCol = new ArrayList%3CObject%3E();%0A%0A
         Build bu
@@ -1060,37 +1060,39 @@
 oBeInspected.get
-Buggy
+Patched
 Build();%0A%0A      
@@ -1076,33 +1076,32 @@
 PatchedBuild();%0A
-%0A
         Build pr
@@ -1226,32 +1226,33 @@
 d.getId() : -1;%0A
+%0A
         String c
@@ -1367,65 +1367,8 @@
 %22;%0A%0A
-        List%3CObject%3E dataCol = new ArrayList%3CObject%3E();%0A%0A
     
@@ -1534,16 +1534,12 @@
 s().
-toString
+name
 ());
@@ -2201,37 +2201,39 @@
 oBeInspected.get
-Buggy
+Patched
 Build();%0A%0A      
@@ -2221,25 +2221,24 @@
 hedBuild();%0A
-%0A
         Buil
@@ -2375,16 +2375,17 @@
 ) : -1;%0A
+%0A
         
@@ -2503,16 +2503,17 @@
  : %22-%22;%0A
+%0A
         
@@ -2532,25 +2532,24 @@
 new Date();%0A
-%0A
         resu
@@ -3345,16 +3345,17 @@
 rty(%22com
+m
 itterEma

Fix ScannerDetailedDataSerializer. (#425)