package uk.ac.ebi.atlas.experimentpage;

import uk.ac.ebi.atlas.model.experiment.Experiment;
import uk.ac.ebi.atlas.resource.AtlasResourceHub;
import uk.ac.ebi.atlas.search.SemanticQuery;
import uk.ac.ebi.atlas.utils.HeatmapDataToJsonService;
import uk.ac.ebi.atlas.web.ApplicationProperties;
import uk.ac.ebi.atlas.web.ExperimentPageRequestPreferences;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class ExperimentPageService {

    private final AtlasResourceHub atlasResourceHub;
    protected final HeatmapDataToJsonService heatmapDataToJsonService;
    protected final ApplicationProperties applicationProperties;
    protected final Gson gson = new Gson();

    public ExperimentPageService(AtlasResourceHub atlasResourceHub,HeatmapDataToJsonService heatmapDataToJsonService, ApplicationProperties applicationProperties){
        this.atlasResourceHub = atlasResourceHub;
        this.heatmapDataToJsonService = heatmapDataToJsonService;
        this.applicationProperties = applicationProperties;
    }

    protected Map<String, ?> headerAttributes(Experiment experiment) {
        Map<String, Object> result = new HashMap<>();
        result.put("hasExtraInfo", atlasResourceHub.hasExtraInfo(experiment));
        return result;
    }
    protected Map<String, JsonElement> payloadAttributes(Experiment experiment,
                                               ExperimentPageRequestPreferences requestPreferences){
        Map<String, JsonElement> result = new HashMap<>();

        result.put("jsonExperiment", prepareExperimentDescription(experiment, requestPreferences));
        return result;
    }

    protected String downloadURL(SemanticQuery geneQuery, HttpServletRequest request){
        return applicationProperties.buildDownloadURL(geneQuery, request);
    }

    private JsonElement prepareExperimentDescription(Experiment experiment, ExperimentPageRequestPreferences
            requestPreferences){
        return prepareExperimentDescription(experiment, requestPreferences.getGeneQuery(), requestPreferences
                .getSerializedFilterFactors());
    }

    //used when external parties include our widget and also to pass header summary to heatmap tooltips
    private JsonElement prepareExperimentDescription(Experiment experiment, SemanticQuery geneQuery, String
            serializedFilterFactors) {
        String additionalQueryOptionsString =
                "?geneQuery="+geneQuery.toUrlEncodedJson()+
                        "&serializedFilterFactors="+serializedFilterFactors;

        JsonObject experimentDescription = new JsonObject();
        experimentDescription.addProperty("URL", "/experiments/"+experiment.getAccession()+additionalQueryOptionsString);
        experimentDescription.addProperty("relUrl", "experiments/"+experiment.getAccession()+additionalQueryOptionsString);
        experimentDescription.addProperty("description", experiment.getDescription());
        experimentDescription.addProperty("species", experiment.getSpecies().getName());
        return experimentDescription;
    }
}

@@ -882,16 +882,50 @@
 urceHub,
+%0A                                 
 HeatmapD
@@ -966,16 +966,49 @@
 Service,
+%0A                                
  Applica
@@ -1044,16 +1044,17 @@
 perties)
+ 
 %7B%0A      
@@ -1463,16 +1463,17 @@
 ;%0A    %7D%0A
+%0A
     prot
@@ -1635,32 +1635,33 @@
 uestPreferences)
+ 
 %7B%0A        Map%3CSt
@@ -1730,13 +1730,9 @@
 ut(%22
-jsonE
+e
 xper
@@ -1917,16 +1917,17 @@
 request)
+ 
 %7B%0A      
@@ -2077,16 +2077,69 @@
 eriment,
+%0A                                                    
  Experim
@@ -2163,28 +2163,16 @@
 ferences
-%0A           
  request
@@ -2183,16 +2183,17 @@
 erences)
+ 
 %7B%0A      
@@ -2222,32 +2222,49 @@
 mentDescription(
+%0A                
 experiment, requ
@@ -2312,33 +2312,16 @@
 ferences
-%0A                
 .getSeri
@@ -2555,23 +2555,16 @@
 neQuery,
- String
 %0A       
@@ -2567,16 +2567,64 @@
         
+                                          String
  seriali
@@ -2718,17 +2718,19 @@
 eQuery=%22
-+
+ + 
 geneQuer
@@ -2753,33 +2753,10 @@
 on()
-+%0A                      
  
++
  %22&s
@@ -2779,17 +2779,19 @@
 actors=%22
-+
+ + 
 serializ
@@ -2916,30 +2916,20 @@
 ty(%22
-URL%22, %22/experiments/%22+
+accession%22, 
 expe
@@ -2953,37 +2953,98 @@
 on()
-+additionalQueryOptionsString
+);%0A        experimentDescription.addProperty(%22type%22, experiment.getType().getDescription()
 );%0A 
@@ -3084,16 +3084,33 @@
 roperty(
+%0A                
 %22relUrl%22
@@ -3125,17 +3125,19 @@
 iments/%22
-+
+ + 
 experime

Replace URL with relUrl (no initial slash) to build the experiments URL with a relative subpath