package org.opencb.opencga.storage.core.search.solr;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.result.FacetedQueryResult;
import org.opencb.opencga.storage.core.config.SearchConfiguration;
import org.opencb.opencga.storage.core.config.StorageConfiguration;
import org.opencb.opencga.storage.core.search.VariantSearchManager;

/**
 * Created by jtarraga on 09/03/17.
 */
public class SolrFacetedQueryParserTest {

    //public String host = "http://localhost:8983/solr/";
    public String host = "http://bioinfo.hpc.cam.ac.uk/solr/"; //hgvav1_hgvauser_reference_grch37/select?facet=on&fq=chromosome:22&indent=on&q=*:*&rows=0&wt=json&facet.field=studies&facet.field=type

    //public String collection = "test1";
    public String collection = "hgvav1_hgvauser_reference_grch37";

    public String study = collection;

    public void executeFacetedQuery(Query query, QueryOptions queryOptions) {
        String user = "";
        String password = "";
        boolean active = true;
        int timeout = 300 * 1000; // SearchConfiguration.DEFAULT_TIMEOUT;
        int rows = 10;
        StorageConfiguration config = new StorageConfiguration();
        config.setSearch(new SearchConfiguration(host, collection, user, password, active, timeout, rows));
        VariantSearchManager searchManager = new VariantSearchManager(null, config);
        try {
            FacetedQueryResult result = searchManager.facetedQuery(collection, query, queryOptions);

            if (result.getResult() != null) {
                System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(result.getResult()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void facetField() {
        QueryOptions queryOptions = new QueryOptions();
        // two facets: 1) by type, and 2) by studies
        queryOptions.put(QueryOptions.FACET, "type;studies:2:2");

        Query query = new Query();
        // query for chromosome 2
        query.put("region", "22");


        // execute
        executeFacetedQuery(query, queryOptions);
    }

    public void facetFieldInclude() {
        QueryOptions queryOptions = new QueryOptions();
        // two facets: 1) by type, and 2) by studies
        //queryOptions.put(QueryOptions.FACET, "studies[EXAC,1kG_phase3]");
        queryOptions.put(QueryOptions.FACET, "type[INDEL,SNV]");

        Query query = new Query();
        // query for chromosome 2
        query.put("region", "22");


        // execute
        executeFacetedQuery(query, queryOptions);
    }


    public void facetNestedFields() {
        QueryOptions queryOptions = new QueryOptions();
        // two facets: 1) by nested fields: studies and soAcc, and 2) by type
        //queryOptions.put(QueryOptions.FACET, "studies[EXAC,GONL]:2:2>>type");
        //queryOptions.put(QueryOptions.FACET, "studies>>type[INDEL,SNV]");
        queryOptions.put(QueryOptions.FACET, "studies[EXAC,GONL]>>type[INDEL,SNV]");
        //queryOptions.put(QueryOptions.FACET, "studies>>genes[BRCA2]");

        Query query = new Query();
        // query for chromosome 2
        query.put("region", "22");


        // execute
        executeFacetedQuery(query, queryOptions);
    }


    public void facetRanges() {
        QueryOptions queryOptions = new QueryOptions();
        // two ranges: 1) phylop from 0 to 1 step 0.3, and 2) caddScaled from 0 to 30 step 5
        queryOptions.put(QueryOptions.FACET_RANGE, "start:1:50000000:1000000;phylop:0:1:0.3;caddScaled:0:30:5");

        Query query = new Query();
        // query for chromosome 2
        query.put("region", "22");

        // execute
        executeFacetedQuery(query, queryOptions);
    }

    public void facetFieldAndRange() {
        QueryOptions queryOptions = new QueryOptions();
        // two facets: 1) by nested fields: studies and type, and 2) by soAcc
        queryOptions.put(QueryOptions.FACET, "studies>>type;soAcc");
        // two ranges: 1) phylop from 0 to 1 step 0.3, and 2) caddScaled from 0 to 30 step 2
        queryOptions.put(QueryOptions.FACET_RANGE, "phylop:0:1:0.3;caddScaled:0:30:2");

        Query query = new Query();
        // query for chromosome 2
        query.put("region", "22");
        System.out.println(query.toString());

        // execute
        executeFacetedQuery(query, queryOptions);
    }

    @Test
    public void testParsing() {
        //facetFieldInclude();
        //facetField();
        facetNestedFields();
        //facetRanges();
        //facetFieldAndRange();
    }

}
@@ -2504,32 +2504,34 @@
 se3%5D%22);%0A        
+//
 queryOptions.put
@@ -2563,24 +2563,113 @@
 e%5BINDEL,SNV%5D
+;genes%5BCNTN5,CTNNA3%5D%22);%0A        queryOptions.put(QueryOptions.FACET, %22genes%5BCNTN5,CTNNA3%5D
 %22);%0A%0A       
@@ -2726,32 +2726,32 @@
 hromosome 2%0A
-
         
 query.put(%22r
@@ -2730,32 +2730,34 @@
 osome 2%0A        
+//
 query.put(%22regio
@@ -4685,34 +4685,32 @@
 ing() %7B%0A        
-//
 facetFieldInclud
@@ -4742,24 +4742,26 @@
 ();%0A        
+//
 facetNestedF

update Solr faceted query parser tests