package uk.ac.ebi.atlas.search.analyticsindex.differential;

import com.google.gson.JsonObject;
import uk.ac.ebi.atlas.search.SemanticQuery;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class DifferentialAnalyticsSearchService {

    private final DifferentialFacetsReader differentialFacetsReader;
    private final DifferentialResultsReader differentialResultsReader;
    private final DifferentialAnalyticsSearchDao differentialAnalyticsSearchDao;

    @Inject
    public DifferentialAnalyticsSearchService(DifferentialAnalyticsSearchDao differentialAnalyticsSearchDao,
                                              DifferentialFacetsReader differentialFacetsReader,
                                              DifferentialResultsReader differentialResultsReader) {
        this.differentialAnalyticsSearchDao = differentialAnalyticsSearchDao;
        this.differentialFacetsReader = differentialFacetsReader;
        this.differentialResultsReader = differentialResultsReader;
    }

    public JsonObject fetchDifferentialFacetsForSearch(SemanticQuery query) {
        String jsonResponse = differentialAnalyticsSearchDao.fetchFacetsAboveDefaultFoldChangeForSearch(query);
        return differentialFacetsReader.generateFacetsTreeJson(jsonResponse);
    }

    public JsonObject fetchDifferentialResultsForSearch(SemanticQuery query) {
        String differentialResults = differentialAnalyticsSearchDao.fetchDifferentialResultsAboveDefaultFoldChangeForSearch(query);
        return differentialResultsReader.extractResultsAsJson(differentialResults);
    }

    public JsonObject fetchDifferentialFacetsForQuery(SemanticQuery geneQuery) {
        return fetchDifferentialFacetsForQuery(geneQuery, SemanticQuery.create(), "");
    }

    public JsonObject fetchDifferentialFacetsForQuery(SemanticQuery geneQuery, SemanticQuery conditionQuery, String species) {
        String jsonResponse = differentialAnalyticsSearchDao.fetchFacetsAboveDefaultFoldChangeForQuery(geneQuery, conditionQuery, species);
        return differentialFacetsReader.generateFacetsTreeJson(jsonResponse);
    }

    public JsonObject fetchDifferentialResultsForQuery(SemanticQuery geneQuery) {
        return fetchDifferentialResultsForQuery(geneQuery, SemanticQuery.create(), "");
    }

    public JsonObject fetchDifferentialResultsForQuery(SemanticQuery geneQuery, SemanticQuery conditionQuery, String species) {
        String differentialResults = differentialAnalyticsSearchDao.fetchDifferentialResultsAboveDefaultFoldChangeForQuery(geneQuery, conditionQuery, species);
        return differentialResultsReader.extractResultsAsJson(differentialResults);
    }

}

@@ -133,16 +133,56 @@
 icQuery;
+%0Aimport uk.ac.ebi.atlas.species.Species;
 %0A%0Aimport
@@ -1080,55 +1080,85 @@
 etch
-DifferentialFacetsForSearch(SemanticQuery query
+Facets(SemanticQuery geneQuery, SemanticQuery conditionQuery, Species species
 ) %7B%0A
@@ -1178,32 +1178,48 @@
 g jsonResponse =
+%0A               
  differentialAna
@@ -1263,39 +1263,103 @@
 ltFoldChange
-ForSearch(query
+(%0A                        geneQuery, conditionQuery, species.getReferenceName()
 );%0A        r
@@ -1465,37 +1465,40 @@
 etch
-DifferentialResultsForSearch(
+Facets(SemanticQuery geneQuery, 
 Sema
@@ -1507,17 +1507,26 @@
 icQuery 
-q
+conditionQ
 uery) %7B%0A
@@ -1540,37 +1540,46 @@
  String 
-differentialResults =
+jsonResponse =%0A               
  differe
@@ -1607,33 +1607,20 @@
 ao.fetch
-DifferentialResul
+Face
 tsAboveD
@@ -1639,197 +1639,36 @@
 ange
-ForSearch(query);%0A        return differentialResultsReader.extractResultsAsJson(differentialResults);%0A    %7D%0A%0A    public JsonObject fetchDifferentialFacetsForQuery(SemanticQuery gene
+(geneQuery, condition
 Query)
- %7B
+;
 %0A   
@@ -1671,38 +1671,33 @@
 %0A        return 
-fetchD
+d
 ifferentialFacet
@@ -1701,54 +1701,50 @@
 cets
-ForQuery(geneQuery, SemanticQuery.create(), %22%22
+Reader.generateFacetsTreeJson(jsonResponse
 );%0A 
@@ -1780,34 +1780,15 @@
 etch
-DifferentialFacetsForQuery
+Results
 (Sem
@@ -1832,37 +1832,38 @@
 onditionQuery, S
-tring
+pecies
  species) %7B%0A    
@@ -1873,30 +1873,53 @@
  String 
-jsonResponse =
+differentialResults =%0A               
  differe
@@ -1939,36 +1939,37 @@
 sSearchDao.fetch
-Face
+Resul
 tsAboveDefaultFo
@@ -1972,33 +1972,50 @@
 ltFoldChange
-ForQuery(
+(%0A                        
 geneQuery, c
@@ -2028,32 +2028,51 @@
 onQuery, species
+.getReferenceName()
 );%0A        retur
@@ -2077,36 +2077,37 @@
 urn differential
-Face
+Resul
 tsReader.generat
@@ -2103,220 +2103,48 @@
 der.
-generateFacetsTreeJson(jsonResponse);%0A    %7D%0A%0A    public JsonObject fetchDifferentialResultsForQuery(SemanticQuery geneQuery) %7B%0A        return fetchDifferentialResultsForQuery(geneQuery, SemanticQuery.create(), %22%22
+extractResultsAsJson(differentialResults
 );%0A 
@@ -2180,35 +2180,15 @@
 etch
-DifferentialResultsForQuery
+Results
 (Sem
@@ -2241,24 +2241,8 @@
 uery
-, String species
 ) %7B%0A
@@ -2277,16 +2277,32 @@
 esults =
+%0A               
  differe
@@ -2330,28 +2330,16 @@
 ao.fetch
-Differential
 ResultsA
@@ -2363,16 +2363,8 @@
 ange
-ForQuery
 (gen
@@ -2385,25 +2385,16 @@
 ionQuery
-, species
 );%0A     

Use new interface of DifferentialAnalyticsSearchDao