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

import com.google.common.collect.ImmutableSet;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrException;
import org.springframework.beans.factory.annotation.Qualifier;
import uk.ac.ebi.atlas.solr.SolrUtil;
import uk.ac.ebi.atlas.web.GeneQuery;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;

@Named
public class AnalyticsSearchDao {
    public static final String ABOVE_CUTOFF = "(" +
                                               "(experimentType:(rnaseq_mrna_baseline OR proteomics_baseline) AND expressionLevel:[0.5 TO *]) OR " +
                                               "(experimentType:(rnaseq_mrna_differential OR microarray_1colour_mrna_differential OR microarray_2colour_mrna_differential OR microarray_1colour_microrna_differential) AND foldChange:[1.0 TO *])" +
                                              ")";

    private static final Logger LOGGER = Logger.getLogger(AnalyticsSearchDao.class);

    private SolrClient analyticsSolrClient;

    @Inject
    public AnalyticsSearchDao(@Qualifier("analyticsSolrClient") SolrClient analyticsSolrClient) {
        this.analyticsSolrClient = analyticsSolrClient;
    }


    public ImmutableSet<String> fetchExperimentTypes(GeneQuery geneQuery) {

        QueryResponse queryResponse = query(buildQuery(geneQuery));

        return SolrUtil.extractFirstFacetValues(queryResponse);
    }

    public QueryResponse query(SolrQuery solrQuery) {
        try {
            return analyticsSolrClient.query(solrQuery);
        } catch (SolrServerException | IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw new SolrException(SolrException.ErrorCode.UNKNOWN, e);
        }
    }

    private SolrQuery buildQuery(GeneQuery geneQuery) {
        String identifierSearch = geneQuery.asString(); //TODO: support multiple gene query terms
        SolrQuery solrQuery = new SolrQuery("identifierSearch:" + identifierSearch);
        solrQuery.setRows(0);
        solrQuery.setFilterQueries(ABOVE_CUTOFF);
        solrQuery.setFacet(true);
        solrQuery.addFacetField("experimentType");
        solrQuery.setFacetMinCount(1);
        return solrQuery;
    }

}

@@ -1073,16 +1073,17 @@
 dChange:
+(
 %5B1.0 TO 
@@ -1084,16 +1084,32 @@
 .0 TO *%5D
+ OR %5B* TO -1.0%5D)
 )%22 +%0A   
@@ -2043,32 +2043,33 @@
 ry geneQuery) %7B%0A
+%0A
         String i
@@ -2070,91 +2070,369 @@
 ring
- identifierSearch = geneQuery.asString(); //TODO: support multiple gene q
+Builder sb = new StringBuilder(%22identifierSearch:(%22);%0A        if (geneQuery.terms().size() %3E 0) %7B%0A            for (int i = 0 ; i %3C geneQuery.terms().size() - 1 ; i++) %7B%0A                sb.append(geneQuery.terms().get(i)).append(%22 OR %22);%0A            %7D%0A            sb.append(geneQuery.terms().get(geneQ
 uery
- 
+.
 terms
+().size() - 1));%0A        %7D%0A        sb.append(%22)%22);%0A
 %0A   
@@ -2476,49 +2476,25 @@
 ery(
-%22identifierSearch:%22 + identifierSearch
+sb.toString()
 );%0A
+%0A
     
@@ -2719,17 +2719,17 @@
 rQuery;%0A
+%0A
     %7D%0A
-%0A
 %7D%0A

Add support for multiple genes in new search ; include results with foldChange -1.0 and lower